Created
June 28, 2015 17:11
-
-
Save rbobillot/923ea833b77f0914b7e4 to your computer and use it in GitHub Desktop.
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
def apply( f:List[Any => String], x:Any ):Any = f match { | |
case h :: t => apply( t, h(x) ) // applies function while iterating on the given functions List | |
case _ => x // if [end_of_list] -> return 'x' | |
} | |
def test( x:Any ) = { | |
val ts = (x:Any) => x.toString // function converting val to String (returns a String) | |
val br = (x:Any) => "[" + x + "]" // function putting val between brackets (returns a String) | |
val list = ts :: br :: Nil // list containing previous functions | |
apply(list, x) // well, you got it | |
} | |
List(42, 42.0, "TEST") // list containing [42 ; 42.0 ; "TEST"] | |
.map( test ) // applies 'test(x)' on each element of the list | |
.foreach( println ) // prints each element of the list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG this is so fucking cool and elegant <3
C language is much faster on execution,
but well, this functional code is way cooler to write <3 <3 <3