Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Created June 28, 2015 17:11
Show Gist options
  • Save rbobillot/923ea833b77f0914b7e4 to your computer and use it in GitHub Desktop.
Save rbobillot/923ea833b77f0914b7e4 to your computer and use it in GitHub Desktop.
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
@rbobillot
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment