Skip to content

Instantly share code, notes, and snippets.

@mariogleichmann
Last active December 29, 2015 05:59
Show Gist options
  • Save mariogleichmann/7626098 to your computer and use it in GitHub Desktop.
Save mariogleichmann/7626098 to your computer and use it in GitHub Desktop.
Pointless Programming, using nested function composition while referring to an Argument more than one time
// connecting / pairing the codomain of two functions
def connectF[A,B,C]( f : A => B, g : A => C ) : A => (B,C) = a => ( f(a), g(a) )
// using codomain pairing / and function composition inline
implicit class FUtils[A,B]( f : A => B ){
def &[C]( g : A => C ) = connectF( f, g )
def o[C]( g : C => A ) : C => B = f compose g
}
// connecting / pairing two functions (convert into a 'Bi-Function') ...
// (a) Non-Pointless-way - this compiles:
def pairF[A,B,C,D]( f : A => B, g : C => D ) : (A,C) => (B,D) = (x,y) => ( f(x), g(y) )
// (b) Pointless way - this doesn't compile
// two helper functions, extracting the first / second component of a Pair
def fst[A,B]( p : (A,B) ) :A = p._1
def snd[A,B]( p : (A,B) ) :B = p._2
// WON'T COMPILE:
//def pairF2[A,B,C,D]( f : A => B, g : C => D ) : (A,C) => (B,D) = ( f o fst ) & ( g o snd )
@igstan
Copy link

igstan commented Nov 26, 2013

This one compiles:

def pairF2[A,B,C,D](f: A => B, g: C => D): ((A,C)) => (B,D) =
  (f o (fst: ((A,C)) => A)) & (g o snd)

@igstan
Copy link

igstan commented Nov 26, 2013

Or this one, in order to respect your initial signature:

def pairF2[A,B,C,D](f: A => B, g: C => D): (A,C) => (B,D) =
  Function.untupled( (f o (fst: ((A,C)) => A)) & (g o snd) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment