Skip to content

Instantly share code, notes, and snippets.

@iszlai
Created June 17, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iszlai/f3af3da6eb0c8d3673d6 to your computer and use it in GitHub Desktop.
Save iszlai/f3af3da6eb0c8d3673d6 to your computer and use it in GitHub Desktop.
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