Higher Order Function signature


#1

I have a JS function that I want to convert to Reason syntax.

function hoc() {
  return (a, b) => {
    return a + b
  }
}

So this function once called returns another function that takes 2 arguments.

hoc() // returns new function

When called again it then takes the 2 arguments provided and adds them together.
I’m finding trouble converting it to reasonml because of the following issue.

If I have the function writen as follows in reasonml syntax

let hoc = () => {
  (a, b) => {
    a++b
  }
};

it gets converted to

function hoc(param, a, b) {
  return a + b;
}

which isn’t what I want. I want the HOC function when first called to take no parameters then take the rest later on

How do I solve this?


#2

This is caused by an optimization in BuckleScript, because OCaml is a curried language but JavaScript is not. You can specify explicitly that you want a function to be treated as uncurried by adding a dot to the beginning of the argument list:

let hoc = () => {
  (. a, b) => {
    a++b
  }
};

More about this here: https://bucklescript.github.io/docs/en/function#curry-uncurry


#3

Awesome, thanks for the tip, I kinda missed that