mirror of
https://github.com/roc-lang/roc.git
synced 2024-10-05 14:47:18 +03:00
84 lines
2.2 KiB
Plaintext
84 lines
2.2 KiB
Plaintext
interface Community
|
|
exposes [
|
|
Community,
|
|
empty,
|
|
addPerson,
|
|
addFriend,
|
|
Person,
|
|
walkFriendNames,
|
|
]
|
|
imports []
|
|
|
|
## Datatype representing a community for demonstration purposes in inspect-gui.roc and inspect-logging.roc
|
|
|
|
Community := {
|
|
people : List Person,
|
|
friends : List (Set U64),
|
|
}
|
|
implements [Inspect]
|
|
|
|
Person := {
|
|
firstName : Str,
|
|
lastName : Str,
|
|
age : U8,
|
|
hasBeard : Bool,
|
|
favoriteColor : Color,
|
|
}
|
|
implements [Inspect]
|
|
|
|
Color : [
|
|
Red,
|
|
Green,
|
|
Blue,
|
|
RGB (U8, U8, U8),
|
|
]
|
|
|
|
empty = @Community { people: [], friends: [] }
|
|
|
|
addPerson = \@Community { people, friends }, person ->
|
|
@Community {
|
|
people: List.append people (@Person person),
|
|
friends: List.append friends (Set.empty {}),
|
|
}
|
|
|
|
addFriend = \@Community { people, friends }, from, to ->
|
|
when (List.get friends from, List.get friends to) is
|
|
(Ok fromSet, Ok toSet) ->
|
|
@Community {
|
|
people,
|
|
friends: friends
|
|
|> List.set from (Set.insert fromSet to)
|
|
|> List.set to (Set.insert toSet from),
|
|
}
|
|
|
|
_ ->
|
|
@Community { people, friends }
|
|
|
|
walkFriendNames : Community, state, (state, Str, Set Str -> state) -> state
|
|
walkFriendNames = \@Community { people, friends }, s0, nextFn ->
|
|
(out, _) =
|
|
(s1, id), friendSet <- List.walk friends (s0, 0)
|
|
(@Person person) =
|
|
when List.get people id is
|
|
Ok v -> v
|
|
Err _ -> crash "Unknown Person"
|
|
personName =
|
|
person.firstName
|
|
|> Str.concat " "
|
|
|> Str.concat person.lastName
|
|
|
|
friendNames =
|
|
friendsSet, friendId <- Set.walk friendSet (Set.empty {})
|
|
(@Person friend) =
|
|
when List.get people friendId is
|
|
Ok v -> v
|
|
Err _ -> crash "Unknown Person"
|
|
friendName =
|
|
friend.firstName
|
|
|> Str.concat " "
|
|
|> Str.concat friend.lastName
|
|
Set.insert friendsSet friendName
|
|
|
|
(nextFn s1 personName friendNames, id + 1)
|
|
out
|