Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
BartoszMilewski - Composition is the Essence of Programming - Challenges
object Category {
//Implement, as best as you can, the identity function in your favorite language
// (or the second favorite, if your favorite language happens to be Haskell).
def id[A]( a:A ):A=a; //> id: [A](a: A)A
//Implement the composition function in your favorite language.
// It takes two functions as arguments and returns a function that is their composition.
def compose[A,B,C] ( f:A=>B , g:B=>C ):(A=>C)= x=> g(f(x)); // g.apply(f(x)); you get a function that takes A and returns C
//Write a program that tries to test that your composition function respects identity.
def testFunc(a:Int):Int=a+1 //> testFunc: (a: Int)Int
def h=compose[Int,Int,Int]( id,testFunc) //> h: => Int => Int
def h1=compose[Int,Int,Int]( testFunc,id) //> h1: => Int => Int
h(4) //> res0: Int = 5
h1(4) //> res0: Int = 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment