darklang / tablecloth

A standard library with the same API in F#, Rescript and OCaml
https://www.tablecloth.dev
MIT License
512 stars 45 forks source link

Js_of_ocaml support #2

Closed hhugo closed 3 years ago

hhugo commented 5 years ago

I don't think one need to differentiate 'native' and 'js_of_ocaml'. Just removing the dependency on Str will make the lib fully usable with js_of_ocaml.

Str is only used to split strings. One should be able to achieve the same thing with a bit of glue around String.Search_pattern.index_all

pbiggar commented 5 years ago

Great! Would love to see someone contribute the Str change!

I think there might be value in having two packages, even if they have the same contents, as this may let us specialize some implementations or types for js_of_ocaml. Though for our codebase, we don't really pay attention to the js_of_ocaml build, it "just works". WDYT?

hhugo commented 5 years ago

Having a single package allows one to build libraries without having to specialize for a specific target.

Without that, one would need to provide multiple copies for every library ? like library_for_doing_blah_js, library_for_doing_blah_native, .. ?

pbiggar commented 5 years ago

Yeah, good point. Ok, let's not have a separate js_of_ocaml lib until we have a good reason to do otherwise.

(If anyone is interested in fixing this, it can be fixed by removing the Str dependency, and updating the readme to indicate we don't need a js_of_ocaml version).

hhugo commented 5 years ago

Here is an inefficient POC.

let split ~(on : string) (s : string) : string list =
  let pat = String.Search_pattern.create on in
  let len = String.length on in
  String.Search_pattern.index_all pat ~may_overlap:false ~in_:s
  |> List.fold_map ~init:0 ~f:(fun x y -> y + len , (x,y))
  |> (fun (last,l) -> l @ [last, String.length s ])
  |> List.map ~f:(fun (x,y) -> String.sub ~pos:x ~len:(y - x) s)
hhugo commented 3 years ago

Str is now compatible with js_of_ocaml. Tablecloth should work out of the box with js_of_ocaml

pbiggar commented 3 years ago

Thanks @hhugo!