mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Make echo example more fun
This commit is contained in:
parent
1ccf7f6b34
commit
8e0c2408c7
1 changed files with 17 additions and 2 deletions
|
@ -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 ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue