Hello and thank you for creating F#+! I’d like to propose adding juxtapose functions to the Operators module. These functions are defined as follows:
let juxt2 f g x = f x, g x
let juxt3 f g h x = f x, g x, h x
let juxt4 f g h i x = f x, g x, h x, i x
// etc
This function is called juxt
in Clojure and the Python toolz library. I believe this function was first introduced by John Backus in his paper called Can programming be liberated from the von Neumann style? where it was called construction
.
In Haskell and F#+ there’s a similar function called sequence
. Unfortunately, it returns a list and not a tuple, forcing all the result types to be the same.
juxt
in combination with item
and uncurry
/uncurryN
make point-free programming with tuples easier. Here are some examples:
open FSharpPlus
// Basic examples
let square: float -> float =
juxt2 id id >> uncurry ( * )
let avg: list<float> -> float =
juxt2 List.sum (List.length >> float) >> uncurry (/)
// Grouping and ungrouping tuples
let ``(a,b),c`` (a,b,c) = (a,b),c
let ``~(a,b),c~`` ((a,b),c) = a,b,c
``(a,b),c``(1,2,3) = (juxt2 (juxt2 item1 item2) item3)(1,2,3) // ((1, 2), 3)
``~(a,b),c~``((1,2),3) = (juxt3 (item1 >> item1) (item1 >> item2) item2)((1,2),3) // (1, 2, 3)
Please let me know if this function already exists in F#+ (or F# for that matter) and I missed it. Thank you!
Issue Analytics
- State:
- Created a year ago
- Comments:9 (6 by maintainers)
Top GitHub Comments
I’ve just got a working generic
juxt
:which can be used like:
I think we should be able to further generalize this to support Arrow.
So the questions are:
juxt
or just makejuxt2
,juxt3
,juxt4
, …?Arrow<'T, 'U>
or should we only support'T -> 'U
?juxt
?fanoutN
?fanoutN
we should havefaninN
,too, which would requireleftN
,rightN
4X2X effortWhat do you think? @gusty @wallymathieu
Yes, I agree in that Arrow should be supported, mainly for consistency.