Combo List Bind Values (no compiler warning)
Let's say I have a function that takes some parameters int
, but inside which I will use float32
.
I would rather not use the function float32 i
everywhere.
Instead, I want to do this:
let x = float32 x
let y = float32 y
let w = float32 w
let h = float32 h
To tighten it up a bit, I could do this:
let x, y, w, h = float32 x, float32 y, float32 w, float32 h
I would like to do this:
let [x;y;w;h] = List.map float32 [x;y;w;h]
This works, but I get a compiler warning Incomplete pattern matching on this expression
because there is no static check that rhs will have exactly 4 elements (might be empty, might have 1 element, might have a thousand).
I don't want to disable the compiler warning.
Is this just a bad idea? Should I ignore the compiler warning in this case, or is there some good, idiomatic way to do this?
source to share
You can define a display function specifically for 4 tuples:
let map4 f (x, y, w, h) = (f x, f y, f w, f h)
It is of type f:('a -> 'b) -> x:'a * y:'a * w:'a * h:'a -> 'b * 'b * 'b * 'b
. Note that all elements in a tuple are considered the same.
Sample Usage (FSI):
> let x, y, w, h = map4 float32 (1, 2, 3, 4);;
val y : float32 = 2.0f
val x : float32 = 1.0f
val w : float32 = 3.0f
val h : float32 = 4.0f
I'll leave this as an exercise for the reader to implement map2
, map3
etc .;)
source to share