Skip to content

Instantly share code, notes, and snippets.

@asifmom
Last active April 7, 2019 06:24
Show Gist options
  • Save asifmom/64d172899f6ee24bfbc02a2a6fe4df86 to your computer and use it in GitHub Desktop.
Save asifmom/64d172899f6ee24bfbc02a2a6fe4df86 to your computer and use it in GitHub Desktop.
scala and ruby composer example
// Scala Example
def composer[A,B,C](f1:B=>C,f2:A=>B):A=>C={
(n:A)=>f1(f2(n))
}
def mul3(n:Int)={n*3}
def add1(n:Int)={n+1}
composer(mul3,add1)(2)
// Ruby Example
make_adder = ->(x){
->(n) {n+x}
}
compose= ->(f,g){
->(n){f.(g.(n))}
}
add1=make_adder.(1)
add1.(2)
mul3 = ->(n){n*3}
compose.(mul3,add1).(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment