You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MarioAriasC edited this page Oct 24, 2014
·
4 revisions
We use partial application when we want to "fix" some parameters of a given function, resulting in new functions
importorg.funktionale.partials.*//use explicit typing for clarity sakeval prefixAndPostfix: (String, String, String) ->String= {(prefix:String, x:String,postfix:String) ->"${prefix}${x}${postfix}"}
val prefixAndBang: (String, String) ->String= prefixAndPostfix.partially3("!")
val hello: (String) ->String= prefixAndBang.partially1("Hello, ")
assertEquals(hello("funKTionale"),"Hello, funKTionale!")
Although partiallyX is explicit probably you want something more natural. funKTionale comes with several overloads for the invoke function that let you create partially applied functions in a more natural way using named parameters
importorg.funktionale.partials.*//use explicit typing for clarity sakeval prefixAndPostfix: (String, String, String) ->String= {(prefix:String, x:String,postfix:String) ->"${prefix}${x}${postfix}"}
//passing just the third parameter will return a new function (String, String) -> Stringval prefixAndBang: (String, String) ->String= prefixAndPostfix(p3 ="!")
//passing just the first parameter will return a new function (String) -> Stringval hello: (String) ->String= prefixAndBang(p1 ="Hello, ")
assertEquals(hello("funKTionale"),"Hello, funKTionale!")