Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active February 9, 2023 23:14
Show Gist options
  • Select an option

  • Save ruxo/5e1e84865bafd17e3a34 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/5e1e84865bafd17e3a34 to your computer and use it in GitHub Desktop.
Pairs useful operations
module RZ.Pair
let inline ofSame a = (a,a)
let inline of' a b = (a,b)
let inline with' b a = (a,b)
let inline sndWith a b = (a,b)
let inline call f (a,b) = f(a,b)
let inline map f (a,b) = (f a, f b)
let inline fapply (f,g) x = (f x, g x)
let inline cross (a,b) x = ((a,x), (b,x))
let inline pipe (g,f) x = f (g x)
let inline pipe2 (g,f) x = f x (g x)
let inline apply (f,g) (a,b) = (f a, g b) (* applicative functor *)
let inline swap (a,b) = (b,a)
// ES6
import R from 'ramda'
export function fst(a) { return a[0] }
export function snd(a) { return a[1] }
export function ofSame(a) { return [a,a] }
export function of(a,b) { return [a,b] }
export const map = R.curry(function PairsMap(f, a) {
return [f(a[0]), f(a[1])]
})
export const apply = R.curry(function PairsApply(af, a){
return [ af[0](a[0]), af[1](a[1]) ]
})
export const cross = R.curry(function PairsCross(a, x){
return [ [a[0], x], [a[1],x ] ]
})
export function swap(a) { return [a[1], a[0]] }
export const fapply = R.curry(function FuncApply(af, x){
return [ af[0](x), af[1](x) ]
})
export const pipe = R.curry(function PairsPipeCall(af, x){
return af[1]( af[0] (x))
})
export const pipe2 = R.curry(function PairsPipeCall2(af, x){
return af[1]( x, af[0] (x))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment