added matching example, made block example about blocks returning values, not all blocks
This commit is contained in:
parent
269cf8c13f
commit
6eca7fc6ab
|
@ -1,36 +1,29 @@
|
||||||
# Blocks
|
# Blocks
|
||||||
|
|
||||||
If statements
|
|
||||||
|
|
||||||
```gleam
|
|
||||||
if condition {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Case Patterns
|
|
||||||
|
|
||||||
```gleam
|
|
||||||
when color is {
|
|
||||||
Green -> "Success."
|
|
||||||
Blue -> "Warning."
|
|
||||||
Red -> "Error!"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Let bindings with blocks
|
Let bindings with blocks
|
||||||
|
|
||||||
```gleam
|
```gleam
|
||||||
let num = -5
|
let x = 3
|
||||||
let absNum = if num>=0 {num} else {-num}
|
|
||||||
|
|
||||||
let message = when color is {
|
let z = {
|
||||||
Green -> "Success."
|
let y = 2
|
||||||
Blue -> "Warning."
|
x + y
|
||||||
Red -> "Error!"
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A block can be thought of as calling an anonymous function with no arguments. They can be used anywhere a value is.
|
||||||
Since everything is secretly a function, the last statement in any block is implicitly its return.
|
Since everything is secretly a function, the last statement in any block is implicitly its return.
|
||||||
|
|
||||||
|
|
||||||
|
Where If with blocks
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
let name: Option(String) = someFunction()
|
||||||
|
let suffix = ""
|
||||||
|
when name is {
|
||||||
|
Some(s)->{
|
||||||
|
let combined = s + suffix
|
||||||
|
Some(combined)
|
||||||
|
}
|
||||||
|
None->None
|
||||||
|
}
|
||||||
|
```
|
|
@ -1 +1,11 @@
|
||||||
# Matching
|
# Matching
|
||||||
|
|
||||||
|
Case Patterns
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
when color is {
|
||||||
|
Green -> "Success."
|
||||||
|
Blue -> "Warning."
|
||||||
|
Red -> "Error!"
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue