Skip to content

Commit

Permalink
base: module-based API
Browse files Browse the repository at this point in the history
  • Loading branch information
pveber committed Nov 20, 2018
1 parent 2b2b804 commit ad10dbd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 135 deletions.
74 changes: 41 additions & 33 deletions lib/base/OCamlR_base.ml
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
open OCamlR

module Stubs = OCamlR_base_stubs
type any

module LL = OCamlR_base_stubs
module Stubs = OCamlR_base_generated_stubs

let ( |? ) o f = match o with
| Some x -> Some (f x)
| None -> None

let length l = R.int_of_t (Stubs.length l)

let dim x =
Stubs.dim x
|> R.ints_of_t
|> (function
| [| x ; y |] -> x, y
| _ -> assert false)

let subset x i = Stubs.subset x (R.int i)
let subset_ii x i j = Stubs.subset_ii x (R.int i) (R.int j)
let subset2_i x i = Stubs.subset2_i x (R.int i)
let subset2_s x s = Stubs.subset2_s x (R.string s)

(* let rle_k encode decode xs = *)
(* let o = Stubs.rle (encode xs) in *)
(* R.ints_of_t (o ## lengths), decode (o ## values) *)

(* let rle : *)
(* type s. s R.scalar_format -> s list -> (int list * s list) = *)
(* fun format xs -> *)
(* match format with *)
(* | R.Integer -> rle_k R.ints R.ints_of_t xs *)
(* | R.Real -> rle_k R.floats R.floats_of_t xs *)
(* | R.Logical -> rle_k R.bools R.bools_of_t xs *)
(* | R.String -> rle_k R.strings R.strings_of_t xs *)

let sample x n ?replace ?prob () =
let subset x i = LL.subset x (R.int i)
let subset_ii x i j = LL.subset_ii x (R.int i) (R.int j)
let subset2_i x i = LL.subset2_i x (R.int i)
let subset2_s x s = LL.subset2_s x (R.string s)

module Environment = struct
type t = any R.t
let create () = Stubs.new'env ()
let unsafe_get env ~class_ x =
let y = LL.subset2_s env (R.string x) in
let cls = R.classes (y : _ R.t :> R.sexp) in
if List.mem class_ cls then
Some y
else None
end

module Dataframe_common = struct
let dim x =
match Stubs.dim'data'frame ~x () |> R.ints_of_t with
| [| i ; j |] -> (i, j)
| _ -> assert false
end

module Dataframe = struct
type t = any R.t

let of_env env x =
Environment.unsafe_get env ~class_:"data.frame" x

include Dataframe_common
end

let sample ?replace ?prob ~size x =
Stubs.sample
x
(R.int n)
~x:(R.floats x)
~size:(R.int size)
?replace:(replace |? R.bool)
?prob
?prob:(prob |? R.floats)
()

|> R.floats_of_t


(* class data'frame x = object *)
Expand Down
118 changes: 21 additions & 97 deletions lib/base/OCamlR_base.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,32 @@

open OCamlR

val length : < length : R.integer R.t ; .. > R.t -> int
val dim : < dim : R.integers R.t ; .. > R.t -> int * int
module Environment : sig
type t

val subset : < subset : 'b. R.integer R.t -> 'b R.t ; .. > R.t -> int -> 'b R.t
val subset_ii : < subset_ii : 'b. R.integer R.t -> R.integer R.t -> 'b R.t ; .. > R.t -> int -> int -> 'b R.t
val subset2_s : < subset2_s : 'b. R.string_ R.t -> 'b R.t ; .. > R.t -> string -> 'b R.t
val subset2_i : < subset2_i : 'b. R.integer R.t -> 'b R.t ; .. > R.t -> int -> 'b R.t
val create : unit -> t
(** wrapper for [new.env] *)
end

(* val rle : 'a R.scalar_format -> 'a list -> (int list * 'a list) *)
module Dataframe : sig
type t
val of_env : Environment.t -> string -> t option
val dim : t -> int * int
end

(** Sampling function. *)
val sample :
(< length : R.integer R.t ; subset : 'b. R.integer R.t -> 'b R.t ; .. > as 'c) R.t ->
int ->
?replace:bool ->
?prob:R.reals R.t ->
unit ->
'c R.t

(* (\** Lapply function, somewhat like List.map.*\) *)
(* val lapply : 'a list R.t -> 'b R.t -> 'c list R.t *)


(* class array_ : array_ R.t -> object *)
(* inherit R.s3 *)
(* method dim : float list R.t *)
(* end *)

(* class matrix : matrix R.t -> object *)
(* inherit array_ *)
(* method floats : float array array *)
(* end *)

(* val matrix : ?byrow:bool -> nrow:int -> ncol:int -> float list -> matrix R.t *)

(* val matrix_by_rows : float list list -> matrix R.t *)


(* class type ['a] listing = object *)
(* method subset2_s : 'b. string -> 'b R.t *)
(* method subset2 : 'b. int -> 'b R.t *)
(* method length : int R.t *)
(* method ty : 'a *)
(* end *)

(* val to_list : 'a list #listing R.t -> 'a R.t list *)

(* class type ['a] dataframe = object *)
(* inherit ['a] listing *)
(* method subset_ii : 'b. int -> int -> 'b R.t *)
(* method dim : float list R.t *)
(* end *)


(* (\* type 'a compound = private < component : 'b. string -> 'b R.t ; .. > *\) *)
(* (\* val component : 'a compound R.t -> string -> 'b R.t *\) *)

(* (\* (\\** Virtual class for R list S3 objects. *\\) *\) *)
(* (\* class ['a] listing : 'a listing R.t -> object *\) *)
(* (\* inherit R.s3 *\) *)
(* (\* method component : 'b. string -> 'b R.t *\) *)
(* (\* method names : string list *\) *)
(* (\* method ty : 'a compound *\) *)
(* (\* end *\) *)

(* (\* val listing : 'a listing R.t -> 'a listing *\) *)

(* (\* (\\** Virtual class for R data frame S3 objects. *\\) *\) *)
(* (\* class ['a] dataframe : 'a dataframe R.t -> object *\) *)
(* (\* inherit ['a] listing *\) *)
(* (\* method row_names : string list *\) *)
(* (\* method column : 'a. int -> 'a R.t *\) *)
(* (\* method element : 'a. int -> int -> 'a R.t *\) *)
(* (\* end *\) *)

(* (\* val dataframe : 'a dataframe R.t -> 'a dataframe *\) *)

(* (\* (\\** Virtual class for dates in R. *\\) *\) *)
(* (\* class date : date R.t -> object *\) *)
(* (\* inherit R.s3 *\) *)
(* (\* method as_float : float *\) *)
(* (\* method as_date : CalendarLib.Calendar.Date.t *\) *)
(* (\* end *\) *)

(* (\* type 'a compound *\) *)

















?prob:float array ->
size:int ->
float array ->
float array

(** {2 Low-level access}
Use with great care!
*)

val subset : _ R.t -> int -> 'b R.t
val subset_ii : _ R.t -> int -> int -> 'b R.t
val subset2_s : _ R.t -> string -> 'b R.t
val subset2_i : _ R.t -> int -> 'b R.t
10 changes: 5 additions & 5 deletions lib/base/OCamlR_base_stubs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ open OCamlR

open OCamlR_base_types

val length : < length : R.integer R.t ; .. > R.t -> R.integer R.t
val length : _ R.t -> R.integer R.t

val subset : < subset : 'b. R.integer R.t -> 'b R.t ; .. > R.t -> R.integer R.t -> 'a R.t
val subset : _ R.t -> R.integer R.t -> 'a R.t
val subset_ii :
< subset_ii : 'b. R.integer R.t -> R.integer R.t -> 'b R.t ; .. > R.t ->
_ R.t ->
R.integer R.t ->
R.integer R.t ->
'b R.t
val subset2_s : < subset2_s : 'b. R.string_ R.t -> 'b R.t ; .. > R.t -> R.string_ R.t -> 'a R.t
val subset2_i : < subset2_i : 'b. R.integer R.t -> 'b R.t ; .. > R.t -> R.integer R.t -> 'a R.t
val subset2_s : _ R.t -> R.string_ R.t -> 'a R.t
val subset2_i : _ R.t -> R.integer R.t -> 'a R.t
val dim : < dim : R.integers R.t ; .. > R.t -> R.integers R.t

module Matrix : sig
Expand Down
5 changes: 5 additions & 0 deletions lib/base/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
(public_name ocaml-r.base)
(wrapped false)
(libraries ocaml-r.interpreter))

(rule
(targets OCamlR_base_generated_stubs.ml)
(deps (:exe ../../stubgen/stubgen.exe))
(action (with-stdout-to %{targets} (run %{exe} base))))

0 comments on commit ad10dbd

Please sign in to comment.