Skip to content

Instantly share code, notes, and snippets.

@iluuu1994
Last active June 18, 2020 21:51
Show Gist options
  • Save iluuu1994/11ac292cf7daca8162798d08db219cd5 to your computer and use it in GitHub Desktop.
Save iluuu1994/11ac292cf7daca8162798d08db219cd5 to your computer and use it in GitHub Desktop.

match in other languages

Java

http://blog.codefx.org/java/switch-expressions/

result = switch (foo) {
    case bar -> baz;
    case bar2 -> baz2;
};

C#

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression

result = foo switch
{
    bar => baz,
    bar2 => baz2,
};

Kotlin

https://kotlinlang.org/docs/reference/control-flow.html#when-expression

result = when(foo) {
    bar -> baz
    bar2 -> baz2
}

Ruby

https://docs.ruby-lang.org/en/2.4.0/syntax/control_expressions_rdoc.html#label-case+Expression (not documented)

result = case foo
when bar
    baz
when bar2
    baz2
end

R

https://www.datamentor.io/r-programming/switch-function/

result <- switch(foo, bar = baz, bar2 = baz2)

Rust

https://doc.rust-lang.org/stable/rust-by-example/flow_control/match.html

result = match foo {
    foo => bar,
    foo2 => bar2,
};

Scala

https://docs.scala-lang.org/tour/pattern-matching.html

result = x foo {
    case bar => baz
    case bar2 => baz2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment