-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.gleam
65 lines (57 loc) · 1.23 KB
/
cell.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//// src/cell.gleam
////
//// Module: cell
////
//// In this module, the Cell type and its functions are defined.
////
//// API:
//// - Cell
//// - new(Location, Bool) -> Cell
//// - is_alive(Cell) -> Bool
//// - is_dead(Cell) -> Bool
//// - toggle(Cell) -> Cell
//// - get_location(Cell) -> Location
//// Internal:
//// * None
// Local imports:
import location as loc
// Public:
/// Cell type definition.
/// A cell is either alive or dead.
/// It also has a location.
pub type Cell {
Alive(loc.Location)
Dead(loc.Location)
}
/// Cell constructor.
pub fn new(location: loc.Location, alive: Bool) -> Cell {
case alive {
True -> Alive(location)
False -> Dead(location)
}
}
/// Get if the cell is alive.
pub fn is_alive(cell: Cell) -> Bool {
case cell {
Alive(_location) -> True
Dead(_location) -> False
}
}
/// Get if the cell is dead.
pub fn is_dead(cell: Cell) -> Bool {
!is_alive(cell)
}
/// Toggle the state of the cell.
pub fn toggle(cell: Cell) -> Cell {
case cell {
Alive(location) -> Dead(location)
Dead(location) -> Alive(location)
}
}
/// Get the location of the cell.
pub fn get_location(cell: Cell) -> loc.Location {
case cell {
Alive(location) -> location
Dead(location) -> location
}
}