Skip to content

Instantly share code, notes, and snippets.

@eoinahern
Created January 16, 2020 12:37
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 eoinahern/1dc5a031399f10df38243521eab6f0df to your computer and use it in GitHub Desktop.
Save eoinahern/1dc5a031399f10df38243521eab6f0df to your computer and use it in GitHub Desktop.
playing with pure functions curried functions
fun main() {
val addOne : (Int) -> Int = { it + 1 }
val doubleStuff : (Int) -> Int = { it * 2}
val myFunc = composer(addOne, doubleStuff)
println(myFunc(3))
val add : (Int) -> (Int)-> Int= { a -> {b -> a + b}}
val num = add(1)(2)
println(num)
// not a curried function
val valCompose : ((Int) -> Int, (Int)-> Int) -> (Int) -> Int = { x,y -> { x(y(it))} }
println(valCompose(addOne, doubleStuff)(2))
//pure function curried form
val myCompose : ((Int) -> Int) -> ((Int) -> Int) -> (Int) -> Int = { x -> { y -> { z -> x(y(z))}} }
println("curried pure function")
println(myCompose(addOne)(doubleStuff)(2))
}
fun composer(f :(Int) -> Int, g : (Int)-> Int) : (Int)-> Int {
return { x -> f(g(x))}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment