My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

TIL: Split strings in (BuckleScript) ReasonML

Heechul Ryu's photo
Heechul Ryu
·Apr 13, 2020

I like ReasonML and currently writing code in it to bring Functional Programming approach in a "JavaScript" project.

It's quite a reasonable language and there are a lot of good things in it. However, nothing's perfect and ReasonML can't be an exception to that.

Few tricky things you might encounter would be finding out "basic" things that you took it for granted in other languages or with libraries.

Example 1: Getting a list of numbers with a given range like Python range

I couldn't find a good answer for this so I wrote a basic implementation at here

Example 2: Splitting a string into a list

It's not in String module. Hmm... that's surprising but let me keep looking.

There is another module like Str module and it has the split function but it doesn't work. You will see this error message below when you try to use this module in your ReasonML with BuckleScript code.

The module or file Str can't be found.
Are you trying to use the standard library's Str?
If you're compiling to JavaScript, use Js.Re instead.
Otherwise, add str.cma to your ocamlc/ocamlopt command.

Alright, I googled more and that led me to see a thread like this while searching for solutions.

Reading the thread carefully and also visiting links of third-party libraries, I get the impression these things are missing in OCaml standard library level which ReasonML is probably depdenant on.

After a few more searches, I suddenly started thinking maybe Js module might have one.

And indeed I found Js.String has split function and works familiarly and nicely like below.

"s, t, r" |> Js.String.split(", ")
// ["s", "t", "r"]

That's technically Array type. So you might want to do this below if you want List type.

"s, t, r" |> Js.String.split(", ") |> Array.to_list

and too bad sketch.sh doesn't seem to support Js modules though.