DEV Community

Discussion on: What are your favorite programming language syntax features?

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

Pattern matching like you see in OCaml variants (ReasonML, F#, etc.)

match x with
| 0 -> "Nothin\'"
| 1 -> "One"
| 2 | 3 -> "Deuce or trey!"
| i when i < 10 -> "Still a single digit!"
| _ -> "Big stuff!"

Elixir has something similar in guards:

def f(0), do: "Nothin\'"
def f(1), do: "One"
def f(x) when x == 2 or x == 3, do: "Deuce or trey!"
def f(x) when x < 10, do: "Still a single digit!"
def f(_x), do: "Big stuff!"
Collapse
 
niorad profile image
Antonio Radovcic

+1 for Guards.

Also I really enjoy Pipes, like

my_data
|> dedupe
|> reverse
|> skedoodle

instead of

skedoodle(reverse(dedupe(my_data)))

I think they are even coming to JS.