[Article Series] The journey of learning ReScript as a TypeScript developer

Hey! :wave:

thanks to everyone jumping in and helping in previous days! My last article about learning ReScript is now online.

Hope you’ll like it,

Best!

4 Likes

Hey,

I just skimmed your last article and want to remark on the “recursive functions” part:

In fp you use very often recursion and probably you would prefer it over loops. Sometimes you also have to use it to prevent mutations, but in your example I have a simple iterative approach to print destinations:

let printDestinations = (destinationList) => {
  Js.Array2.forEach(destinationList, Js.log)
}

printDestinations(destinations)

And a small simplification for your recursive function:

let rec printDestinations = (destinationList) => {
  if Js.Array2.length(destinationList) == 0 {
    Js.log("")
  } else {
    Js.log(destinationList[0])

    // here is my change
    printDestinations(Js.Array2.sliceFrom(destinationList, 1))
  }
}

Well, in this example I don’t know, if I would say “Overall we can say our code is now cleaner and simpler to read and maintain”. Maybe you could write a more complex example where recursion really shines?!

1 Like

Nice!

If it’s for newcomers, might be a good opportunity to use the new Core library which may be more familiar to JS-devs.

1 Like

Hey, first thing first, thanks for taking the time to read through.

I definitely agree on the fact that there’re better way for iterating than using while and for, but I put them in the let readers know it is still an option and why they’re not used that much compared to recursion. Also thanks for pointing the sliceFrom API, didn’t know about it :pray: (I’ll update the example as soon as I can).

The comment about code readability was meant to compare recursion vs loop statements and the example was purely indicative. But I do agree that there’re way better scenarios, just I didn’t want to bring math in here as well :sweat_smile:.

Also, using Pattern Matching would’ve made everything nicer and more concise, but I’m forcing myself to create examples with the tools I already covered. I already made an exception for Js module, since I’m using it but I never properly introduced it except sometimes I leave links to the documentation.

Best!

Definitely aimed for newcomers, at least for now.

To be honest, I programmed with OCaml years ago but didn’t find any company using it so over years I switched to more “popular” choices.
Now that I’m starting getting some recognition in the company I’m currently working in, I want to give ReScript a try in production and this is why I started blogging about it. I will have a look into the new Core library, and eventually update snippets where necessary.

I’m quite rusty on some concepts, but I’ve to say that overall ReScript looks familiar to me. I’m really appreciating the language so far. Eventually I will start writing more idiomatically in a couple of months.

Edit: was looking at the documentation, but I didn’t find any mention about the Core library. Are you referring to Belt or is something planned for future releases?

Best!

5 Likes

One thing I would mention is that your recursive function printDestinations is in fact tail-recursive so the compiler can optimize it into an imperative while-loop which keeps the call stack from growing indefinitely.

1 Like

I would’ve like too, but I was afraid about mentioning tail recursion without explaining why is a big thing and how it compares to head recursion. I will think about it and see if I can fit it in somehow without getting too technical.

Trying to stay in the 10 minutes range while explaining some stuff is definitely challenging and it’s an exercise on its own (something I wasn’t prepared for :sweat_smile:).

Best!

Makes sense. I guess you can revisit that topic later on as well.

Sorry, updated my previous post with a link.

1 Like

I understand. Will definitely read your other articels. Maybe a good inspiration for a workshop I will prepare for my colleagues :slight_smile:

1 Like