[beginner] map, reduce, filter in ReasonML


#1

I feel really stupid asking this question, but I’ve been through the docs / StackOverflow and the forums and haven’t found a solution!

I’m completely new to ReasonML, and I’m coming from a JS background. I’m having real trouble finding a way to map / reduce / filter an array / list / tuple. If someone could point me in the right direction so that I can continue my ReasonML adventure, that would be great!


#2
/* https://bucklescript.github.io/bucklescript/api/Belt.html */
module A = Belt.Array;
module L = Belt.List;

let myArray = A.rangeBy(1, 10, ~step=1);
Js.log(myArray);

let myList = L.fromArray(myArray);
Js.log(myList);

let isEven = i => i mod 2 == 0;
A.keep(myArray, isEven)->Js.log;

L.keep(myList, j => j mod 2 == 0)->Js.log;

let double = i => i * 2;
A.map(myArray, double)->Js.log;

L.map(myList, ( * )(2))->Js.log;

let add = (i, j) => i + j;
A.reduce(myArray, 0, add)->Js.log;

L.reduce(myList, 0, (+))->Js.log;
$ node src/Demo.bs.js
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 1,
  [ 2,
    [ 3, [ 4, [ 5, [ 6, [ 7, [ 8, [ 9, [ 10, 0 ] ] ] ] ] ] ] ] ] ]
[ 2, 4, 6, 8, 10 ]
[ 2, [ 4, [ 6, [ 8, [ 10, 0 ] ] ] ] ]
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]
[ 2,
  [ 4,
    [ 6, [ 8, [ 10, [ 12, [ 14, [ 16, [ 18, [ 20, 0 ] ] ] ] ] ] ] ] ] ]
55
55