Created
January 16, 2020 12:37
-
-
Save eoinahern/1dc5a031399f10df38243521eab6f0df to your computer and use it in GitHub Desktop.
playing with pure functions curried functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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