fsprojects / fsprojects/FSharpPlus

Juxt

Open
#486 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement RFC
Dominant language
F#
Stars
941
Forks
106
PR merge metrics
No merged PRs in 30d

Description

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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the Operators module and inspect the existing sequence, item, uncurry, and uncurryN functions to match their naming and organization. Implement the proposed juxt2, juxt3, and juxt4 variants, then add coverage for the tuple-producing examples and confirm the project tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.