Created
January 23, 2011 09:45
-
-
Save mads-hartmann/791943 to your computer and use it in GitHub Desktop.
Pattern matching as a control structure
This file contains hidden or 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
/* | |
I want to use pattern matching in coffeescript as a more capable switch. It uses destructuring | |
assignments to figure out which of the cases that matches. So it can match on the entire | |
structure of the object instead of just an integer. | |
(* in F# *) | |
let list = [1;2;3;4] | |
match list with | |
| [] -> printfn "empty list" | |
| x :: xs -> printfn "first element is " + x | |
*/ | |
// in Scala | |
val list = 1 :: 2 :: 3 :: 4 :: Nil | |
list match { | |
case Nil => println("empty list") | |
case x :: xs => println("first element is " + x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Trevor,
Thanks for replying. I will take a look at it to see if it's possible to translate Scala's pattern-matching into javascript. I'm about to start my bachelor project where I will write a collections library for javascript in Coffeescript and pattern matching (scala style) would really help there.
Thanks,
Mads