Skip to content

Juxt #486

Open
Open
Juxt#486
@OlegAlexander

Description

@OlegAlexander

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions