To create a sequence of my class,
type MyInt(i:int) =
member this.i = i
[1;2;3] |> Seq.map(fun x->MyInt(x))
where fun x->MyInt(x) seems to be redundant. It would be better if I can write Seq.map(MyInt)
But I cannot. One workaround I can think of is to define a separate function
let myint x = MyInt(x)
[1;2;3] |> Seq.map(myint)
Is there a better way to do this?
In short, no.
Object constructors aren’t first-class functions in F#. This is one more reason to not use classes, discriminated unions is better to use here:
If you don’t like explicit lambdas, sequence expression looks nicer in your example:
Alternatively, your workaround is a nice solution.