From a76a361b6bc124d18801d21b415486f51557d156 Mon Sep 17 00:00:00 2001 From: Philippe Veber Date: Wed, 21 Nov 2018 12:05:54 +0100 Subject: [PATCH] base: new S3 module, access to underlying R value --- lib/base/OCamlR_base.ml | 24 ++++++++++++++---------- lib/base/OCamlR_base.mli | 10 ++++++++++ lib/base/dune | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/base/OCamlR_base.ml b/lib/base/OCamlR_base.ml index bd543c0..7c1ad64 100644 --- a/lib/base/OCamlR_base.ml +++ b/lib/base/OCamlR_base.ml @@ -1,6 +1,5 @@ open OCamlR - -type any +open OCamlR_wraputils module Stubs = OCamlR_base_stubs module Stubs2 = OCamlR_base_stubs2 @@ -14,8 +13,17 @@ let subset_ii x i j = Stubs2.subset_ii x (R.int i) (R.int j) let subset2_i x i = Stubs2.subset2_i x (R.int i) let subset2_s x s = Stubs2.subset2_s x (R.string s) +module S3 = struct + type t = t R.t + let r x = x + let _class_ = + let symbol = R.symbol "class" in + fun (x : t) -> + R.strings_of_t (R.eval symbol [ R.arg ident x ]) +end + module Environment = struct - type t = any R.t + include S3 let create () = Stubs.new'env () let unsafe_get env ~class_ x = let y = Stubs2.subset2_s env (R.string x) in @@ -25,20 +33,16 @@ module Environment = struct else None end -module Dataframe_common = struct +module Dataframe = struct + include S3 + 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 = diff --git a/lib/base/OCamlR_base.mli b/lib/base/OCamlR_base.mli index a9ed44a..1ddf0dc 100644 --- a/lib/base/OCamlR_base.mli +++ b/lib/base/OCamlR_base.mli @@ -2,8 +2,16 @@ open OCamlR +module S3 : sig + type t + + val r : t -> t R.t + val _class_ : t -> string array +end + module Environment : sig type t + include module type of S3 with type t := t val create : unit -> t (** wrapper for [new.env] *) @@ -11,6 +19,8 @@ end module Dataframe : sig type t + include module type of S3 with type t := t + val of_env : Environment.t -> string -> t option val dim : t -> int * int end diff --git a/lib/base/dune b/lib/base/dune index 542a701..00908b5 100644 --- a/lib/base/dune +++ b/lib/base/dune @@ -2,7 +2,7 @@ (name OCamlR_base) (public_name ocaml-r.base) (wrapped false) - (libraries ocaml-r.interpreter)) + (libraries ocaml-r.interpreter ocaml-r.wraputils)) (rule (targets OCamlR_base_stubs.ml)