In F#, how do I map/lift a function where the result is curried?

54 views Asked by At

I have a struct constructor that takes a tuple, but I need to construct it with two arrays. My current solution is running into a problem while trying to map the function. The currying goes away. Hopefully there is an alternative way of doing this.

type Example =
  struct
    val X : int
    val Y : int
    new(x: int, y: int) = { X = x; Y = y} // constructor is (int * int -> Example)
  end
let foo = fun x y -> Example(x, y) // to curry; foo is now (int -> int -> Example)
let bar = Array.map foo // oh no; it changed to (int array-> (int -> Example) array)
// how can it be (int array -> int array -> Example array) instead?
// this is my imagined implementation
let a = seq{0 .. 100} // these sequences are filler for this demonstration
let b = seq{0 .. 100}
let myArray = bar a b // where myArray is (Example array)
1

There are 1 answers

0
UpTide On

I found it. The solution to this is map2.

Swapping let bar = Array.map foo to let bar = Array.map2 foo allows the use of let myArray = bar a b where bar is int array -> int array -> Example array

Related Questions in F#