47 lines
1.1 KiB
Mojo
47 lines
1.1 KiB
Mojo
# probe: match on an Int and on a String: literal cases, alternatives with |, a capture with a guard, _ with a guard, a bare capture, no case matching, the first matching case winning, and a variable every case assigns
|
|
def kind(n: Int) -> String:
|
|
match n:
|
|
case 0:
|
|
return "zero"
|
|
case 1 | 2 | 3:
|
|
return "small"
|
|
case big if big > 100:
|
|
return "big " + String(big)
|
|
case _ if n % 2 == 0:
|
|
return "even"
|
|
case other:
|
|
return "odd " + String(other)
|
|
|
|
|
|
def word(s: String) -> Int:
|
|
match s:
|
|
case "a":
|
|
return 1
|
|
case "b" | "c":
|
|
return 2
|
|
case _:
|
|
return 0
|
|
|
|
|
|
def main():
|
|
for n in [0, 2, -1, 500, 8, 7]:
|
|
print(kind(n))
|
|
print(word("a"), word("c"), word("zz"))
|
|
var hits = 0
|
|
var k = 4
|
|
match k:
|
|
case 4:
|
|
hits += 1
|
|
case v if v > 0:
|
|
hits += 10
|
|
match k:
|
|
case 5:
|
|
hits += 100
|
|
var label: String
|
|
match k:
|
|
case 1:
|
|
label = "one"
|
|
case _:
|
|
label = "other"
|
|
print(hits, label)
|