Make echo example more fun

This commit is contained in:
Jan Van Bruggen 2022-01-23 00:28:19 -07:00
parent 1ccf7f6b34
commit 8e0c2408c7

View file

@ -6,9 +6,24 @@ app "echo"
main : Task.Task {} []
main =
_ <- Task.await (Stdout.line "Shout into this cave and hear the echo!")
Task.loop {} (\{ } -> Task.map tick Step)
Task.loop {} (\_ -> Task.map tick Step)
tick : Task.Task {} []
tick =
shout <- Task.await Stdin.line
Stdout.line shout
Stdout.line (echo shout)
echo : Str -> Str
echo = \shout ->
silence = \length ->
spaceInUtf8 = 32
List.repeat length spaceInUtf8
shout
|> Str.toUtf8
|> List.mapWithIndex (\i, _ ->
length = (List.len (Str.toUtf8 shout) - i)
phrase = (List.split (Str.toUtf8 shout) length).before
List.concat (silence (if i == 0 then 2 * length else length)) phrase)
|> List.join
|> Str.fromUtf8
|> Result.withDefault ""