mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 19:32:17 +00:00
roc format examples
This commit is contained in:
parent
7ed35eca50
commit
7908e8c176
32 changed files with 231 additions and 232 deletions
|
@ -5,8 +5,8 @@ quicksort = \list, low, high ->
|
|||
when partition low high list is
|
||||
Pair partitionIndex partitioned ->
|
||||
partitioned
|
||||
|> quicksort low (partitionIndex - 1)
|
||||
|> quicksort (partitionIndex + 1) high
|
||||
|> quicksort low (partitionIndex - 1)
|
||||
|> quicksort (partitionIndex + 1) high
|
||||
|
||||
|
||||
swap : Nat, Nat, List a -> List a
|
||||
|
@ -14,8 +14,8 @@ swap = \i, j, list ->
|
|||
when Pair (List.get list i) (List.get list j) is
|
||||
Pair (Ok atI) (Ok atJ) ->
|
||||
list
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|
||||
_ ->
|
||||
[]
|
||||
|
|
|
@ -7,8 +7,8 @@ quicksort = \originalList ->
|
|||
when partition low high list is
|
||||
Pair partitionIndex partitioned ->
|
||||
partitioned
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
else
|
||||
list
|
||||
|
||||
|
@ -18,8 +18,8 @@ quicksort = \originalList ->
|
|||
when Pair (List.get list i) (List.get list j) is
|
||||
Pair (Ok atI) (Ok atJ) ->
|
||||
list
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|
||||
_ ->
|
||||
[]
|
||||
|
|
|
@ -25,8 +25,8 @@ const ROC_LIST_MAP: &str = indoc::indoc!(
|
|||
r#"
|
||||
app "bench" provides [main] to "./platform"
|
||||
|
||||
main : List I64 -> Nat
|
||||
main = \list ->
|
||||
main : List I64 -> Nat
|
||||
main = \list ->
|
||||
list
|
||||
|> List.map (\x -> x + 2)
|
||||
|> List.len
|
||||
|
@ -37,11 +37,11 @@ const ROC_LIST_MAP_WITH_INDEX: &str = indoc::indoc!(
|
|||
r#"
|
||||
app "bench" provides [main] to "./platform"
|
||||
|
||||
main : List I64 -> Nat
|
||||
main = \list ->
|
||||
main : List I64 -> Nat
|
||||
main = \list ->
|
||||
list
|
||||
|> List.mapWithIndex (\x, _ -> x + 2)
|
||||
|> List.len
|
||||
|> List.mapWithIndex (\x, _ -> x + 2)
|
||||
|> List.len
|
||||
"#
|
||||
);
|
||||
|
||||
|
|
|
@ -572,8 +572,8 @@ fn quicksort_help() {
|
|||
(Pair partitionIndex partitioned) = Pair 0 []
|
||||
|
||||
partitioned
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
else
|
||||
list
|
||||
|
||||
|
@ -591,8 +591,8 @@ fn quicksort_swap() {
|
|||
when Pair (List.get list 0) (List.get list 0) is
|
||||
Pair (Ok atI) (Ok atJ) ->
|
||||
list
|
||||
|> List.set 0 atJ
|
||||
|> List.set 0 atI
|
||||
|> List.set 0 atJ
|
||||
|> List.set 0 atI
|
||||
|
||||
_ ->
|
||||
[]
|
||||
|
|
|
@ -14,8 +14,8 @@ quicksortHelp = \list, low, high ->
|
|||
when partition low high list is
|
||||
Pair partitionIndex partitioned ->
|
||||
partitioned
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
|> quicksortHelp low (partitionIndex - 1)
|
||||
|> quicksortHelp (partitionIndex + 1) high
|
||||
else
|
||||
list
|
||||
|
||||
|
@ -50,8 +50,8 @@ swap = \i, j, list ->
|
|||
when Pair (List.get list i) (List.get list j) is
|
||||
Pair (Ok atI) (Ok atJ) ->
|
||||
list
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|
||||
_ ->
|
||||
# to prevent a decrement on list
|
||||
|
|
|
@ -21,17 +21,17 @@ initialModel = \start -> {
|
|||
cheapestOpen : (position -> F64), Model position -> Result position {}
|
||||
cheapestOpen = \costFn, model ->
|
||||
model.openSet
|
||||
|> Set.toList
|
||||
|> List.keepOks
|
||||
(\position ->
|
||||
when Dict.get model.costs position is
|
||||
Err _ -> Err {}
|
||||
Ok cost -> Ok { cost: cost + costFn position, position }
|
||||
)
|
||||
|> Quicksort.sortBy .cost
|
||||
|> List.first
|
||||
|> Result.map .position
|
||||
|> Result.mapErr (\_ -> {})
|
||||
|> Set.toList
|
||||
|> List.keepOks
|
||||
(\position ->
|
||||
when Dict.get model.costs position is
|
||||
Err _ -> Err {}
|
||||
Ok cost -> Ok { cost: cost + costFn position, position }
|
||||
)
|
||||
|> Quicksort.sortBy .cost
|
||||
|> List.first
|
||||
|> Result.map .position
|
||||
|> Result.mapErr (\_ -> {})
|
||||
|
||||
reconstructPath : Dict position position, position -> List position
|
||||
reconstructPath = \cameFrom, goal ->
|
||||
|
@ -49,8 +49,8 @@ updateCost = \current, neighbor, model ->
|
|||
|
||||
distanceTo =
|
||||
reconstructPath newCameFrom neighbor
|
||||
|> List.len
|
||||
|> Num.toFrac
|
||||
|> List.len
|
||||
|> Num.toFrac
|
||||
|
||||
newModel =
|
||||
{ model &
|
||||
|
|
|
@ -63,23 +63,23 @@ bitsToCharsHelp = \bits, missing ->
|
|||
# any 6-bit number is a valid base64 digit, so this is actually safe
|
||||
p =
|
||||
Num.shiftRightZfBy 18 bits
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|
||||
q =
|
||||
Num.bitwiseAnd (Num.shiftRightZfBy 12 bits) lowest6BitsMask
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|
||||
r =
|
||||
Num.bitwiseAnd (Num.shiftRightZfBy 6 bits) lowest6BitsMask
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|
||||
s =
|
||||
Num.bitwiseAnd bits lowest6BitsMask
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|> Num.intCast
|
||||
|> unsafeToChar
|
||||
|
||||
equals : U8
|
||||
equals = 61
|
||||
|
|
|
@ -8,15 +8,15 @@ InvalidChar : U8
|
|||
toBytes : Str -> List U8
|
||||
toBytes = \str ->
|
||||
str
|
||||
|> Str.toUtf8
|
||||
|> encodeChunks
|
||||
|> Bytes.Encode.sequence
|
||||
|> Bytes.Encode.encode
|
||||
|> Str.toUtf8
|
||||
|> encodeChunks
|
||||
|> Bytes.Encode.sequence
|
||||
|> Bytes.Encode.encode
|
||||
|
||||
encodeChunks : List U8 -> List Encoder
|
||||
encodeChunks = \bytes ->
|
||||
List.walk bytes { output: [], accum: None } folder
|
||||
|> encodeResidual
|
||||
|> encodeResidual
|
||||
|
||||
coerce : Nat, a -> a
|
||||
coerce = \_, x -> x
|
||||
|
|
|
@ -49,7 +49,7 @@ encode = \encoder ->
|
|||
output = List.repeat 0 (getWidth encoder)
|
||||
|
||||
encodeHelp encoder 0 output
|
||||
|> .output
|
||||
|> .output
|
||||
|
||||
encodeHelp : Encoder, Nat, List U8 -> { output : List U8, offset : Nat }
|
||||
encodeHelp = \encoder, offset, output ->
|
||||
|
@ -80,13 +80,13 @@ encodeHelp = \encoder, offset, output ->
|
|||
when endianness is
|
||||
BE ->
|
||||
output
|
||||
|> List.set (offset + 0) a
|
||||
|> List.set (offset + 1) b
|
||||
|> List.set (offset + 0) a
|
||||
|> List.set (offset + 1) b
|
||||
|
||||
LE ->
|
||||
output
|
||||
|> List.set (offset + 0) b
|
||||
|> List.set (offset + 1) a
|
||||
|> List.set (offset + 0) b
|
||||
|> List.set (offset + 1) a
|
||||
|
||||
{
|
||||
output: newOutput,
|
||||
|
@ -104,13 +104,13 @@ encodeHelp = \encoder, offset, output ->
|
|||
when endianness is
|
||||
BE ->
|
||||
output
|
||||
|> List.set (offset + 0) a
|
||||
|> List.set (offset + 1) b
|
||||
|> List.set (offset + 0) a
|
||||
|> List.set (offset + 1) b
|
||||
|
||||
LE ->
|
||||
output
|
||||
|> List.set (offset + 0) b
|
||||
|> List.set (offset + 1) a
|
||||
|> List.set (offset + 0) b
|
||||
|> List.set (offset + 1) a
|
||||
|
||||
{
|
||||
output: newOutput,
|
||||
|
|
|
@ -14,10 +14,10 @@ main =
|
|||
optimized = eval (constFolding (reassoc e))
|
||||
|
||||
unoptimized
|
||||
|> Num.toStr
|
||||
|> Str.concat " & "
|
||||
|> Str.concat (Num.toStr optimized)
|
||||
|> Task.putLine
|
||||
|> Num.toStr
|
||||
|> Str.concat " & "
|
||||
|> Str.concat (Num.toStr optimized)
|
||||
|> Task.putLine
|
||||
|
||||
Expr : [
|
||||
Add Expr Expr,
|
||||
|
|
|
@ -13,7 +13,7 @@ main = closure1 {}
|
|||
closure1 : {} -> Task.Task {} []
|
||||
closure1 = \_ ->
|
||||
Task.succeed (foo toUnitBorrowed "a long string such that it's malloced")
|
||||
|> Task.map (\_ -> {})
|
||||
|> Task.map \_ -> {}
|
||||
|
||||
toUnitBorrowed = \x -> Str.countGraphemes x
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ main =
|
|||
f = pow x x
|
||||
|
||||
nest deriv n f # original koka n = 10
|
||||
|> Task.map (\_ -> {})
|
||||
|> Task.map \_ -> {}
|
||||
|
||||
nest : (I64, Expr -> IO Expr), I64, Expr -> IO Expr
|
||||
nest = \f, n, e -> Task.loop { s: n, f, m: n, x: e } nestHelp
|
||||
|
@ -160,8 +160,8 @@ deriv = \i, f ->
|
|||
fprime = d "x" f
|
||||
line =
|
||||
Num.toStr (i + 1)
|
||||
|> Str.concat " count: "
|
||||
|> Str.concat (Num.toStr (count fprime))
|
||||
|> Str.concat " count: "
|
||||
|> Str.concat (Num.toStr (count fprime))
|
||||
|
||||
Task.putLine line
|
||||
|> Task.after \_ -> Task.succeed fprime
|
||||
|> Task.after \_ -> Task.succeed fprime
|
||||
|
|
|
@ -9,8 +9,8 @@ main =
|
|||
Task.getInt
|
||||
\n ->
|
||||
queens n # original koka 13
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|
||||
ConsList a : [Nil, Cons a (ConsList a)]
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ show = \list ->
|
|||
else
|
||||
content =
|
||||
list
|
||||
|> List.map Num.toStr
|
||||
|> Str.joinWith ", "
|
||||
|> List.map Num.toStr
|
||||
|> Str.joinWith ", "
|
||||
|
||||
"[\(content)]"
|
||||
|
||||
|
@ -30,8 +30,8 @@ quicksortHelp = \list, order, low, high ->
|
|||
when partition low high list order is
|
||||
Pair partitionIndex partitioned ->
|
||||
partitioned
|
||||
|> quicksortHelp order low (Num.subSaturated partitionIndex 1)
|
||||
|> quicksortHelp order (partitionIndex + 1) high
|
||||
|> quicksortHelp order low (Num.subSaturated partitionIndex 1)
|
||||
|> quicksortHelp order (partitionIndex + 1) high
|
||||
else
|
||||
list
|
||||
|
||||
|
@ -68,8 +68,8 @@ swap = \i, j, list ->
|
|||
when Pair (List.get list i) (List.get list j) is
|
||||
Pair (Ok atI) (Ok atJ) ->
|
||||
list
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|> List.set i atJ
|
||||
|> List.set j atI
|
||||
|
||||
_ ->
|
||||
[]
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -52,8 +52,8 @@ main =
|
|||
val = fold (\_, v, r -> if v then r + 1 else r) head 0
|
||||
|
||||
val
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|
||||
Nil ->
|
||||
Task.putLine "fail"
|
||||
|
|
|
@ -20,8 +20,8 @@ main =
|
|||
val = fold (\_, v, r -> if v then r + 1 else r) m 0
|
||||
|
||||
val
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|> Num.toStr
|
||||
|> Task.putLine
|
||||
|
||||
boom : Str -> a
|
||||
boom = \_ -> boom ""
|
||||
|
|
|
@ -9,8 +9,8 @@ main =
|
|||
tree = insert 0 {} Empty
|
||||
|
||||
tree
|
||||
|> show
|
||||
|> Task.putLine
|
||||
|> show
|
||||
|> Task.putLine
|
||||
|
||||
show : RedBlackTree I64 {} -> Str
|
||||
show = \tree -> showRBTree tree Num.toStr (\{} -> "{}")
|
||||
|
|
|
@ -8,11 +8,11 @@ forever : Task val err -> Task * err
|
|||
forever = \task ->
|
||||
looper = \{} ->
|
||||
task
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok _ -> Step {}
|
||||
Err e -> Done (Err e)
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok _ -> Step {}
|
||||
Err e -> Done (Err e)
|
||||
|
||||
Effect.loop {} looper
|
||||
|
||||
|
@ -20,12 +20,12 @@ loop : state, (state -> Task [Step state, Done done] err) -> Task done err
|
|||
loop = \state, step ->
|
||||
looper = \current ->
|
||||
step current
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|
||||
Effect.loop state looper
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ update = \model, event ->
|
|||
tick : Model -> Model
|
||||
tick = \model ->
|
||||
model
|
||||
|> moveBall
|
||||
|> moveBall
|
||||
|
||||
moveBall : Model -> Model
|
||||
moveBall = \model ->
|
||||
|
@ -99,12 +99,12 @@ render = \model ->
|
|||
\index ->
|
||||
col =
|
||||
Num.rem index numCols
|
||||
|> Num.toF32
|
||||
|> Num.toF32
|
||||
|
||||
row =
|
||||
index
|
||||
// numCols
|
||||
|> Num.toF32
|
||||
// numCols
|
||||
|> Num.toF32
|
||||
|
||||
red = col / Num.toF32 numCols
|
||||
green = row / Num.toF32 numRows
|
||||
|
|
|
@ -90,8 +90,8 @@ translate = \child, toChild, toParent ->
|
|||
Button config label ->
|
||||
onPress = \parentState, event ->
|
||||
toChild parentState
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|
||||
Button { onPress } (translate label toChild toParent)
|
||||
|
||||
|
@ -136,8 +136,8 @@ list = \renderChild, parent, toChildren, toParent ->
|
|||
toChild
|
||||
\par, ch ->
|
||||
toChildren par
|
||||
|> List.set ch index
|
||||
|> toParent
|
||||
|> List.set ch index
|
||||
|> toParent
|
||||
|
||||
renderChild newChild
|
||||
|
||||
|
@ -165,8 +165,8 @@ translateOrDrop = \child, toChild, toParent ->
|
|||
when toChild parentState is
|
||||
Ok newChild ->
|
||||
newChild
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|
||||
Err _ ->
|
||||
# The child was removed from the list before this onPress handler resolved.
|
||||
|
@ -182,7 +182,7 @@ translateOrDrop = \child, toChild, toParent ->
|
|||
when toChild parentState is
|
||||
Ok newChild ->
|
||||
renderChild newChild
|
||||
|> translateOrDrop toChild toParent
|
||||
|> translateOrDrop toChild toParent
|
||||
|
||||
Err _ ->
|
||||
None
|
||||
|
|
|
@ -21,7 +21,7 @@ InterpreterErrors : [BadUtf8, DivByZero, EmptyStack, InvalidBooleanValue, Invali
|
|||
main : Str -> Task {} []
|
||||
main = \filename ->
|
||||
interpretFile filename
|
||||
|> Task.onFail (\StringErr e -> Stdout.line "Ran into problem:\n\(e)\n")
|
||||
|> Task.onFail \StringErr e -> Stdout.line "Ran into problem:\n\(e)\n"
|
||||
|
||||
interpretFile : Str -> Task {} [StringErr Str]
|
||||
interpretFile = \filename ->
|
||||
|
@ -70,19 +70,19 @@ interpretFile = \filename ->
|
|||
isDigit : U8 -> Bool
|
||||
isDigit = \char ->
|
||||
char
|
||||
>= 0x30 # `0`
|
||||
&& char
|
||||
<= 0x39 # `0`
|
||||
>= 0x30 # `0`
|
||||
&& char
|
||||
<= 0x39 # `0`
|
||||
isWhitespace : U8 -> Bool
|
||||
isWhitespace = \char ->
|
||||
char
|
||||
== 0xA # new line
|
||||
|| char
|
||||
== 0xB # carriage return
|
||||
|| char
|
||||
== 0x20 # space
|
||||
|| char
|
||||
== 0x9 # tab
|
||||
== 0xA # new line
|
||||
|| char
|
||||
== 0xB # carriage return
|
||||
|| char
|
||||
== 0x20 # space
|
||||
|| char
|
||||
== 0x9 # tab
|
||||
interpretCtx : Context -> Task Context InterpreterErrors
|
||||
interpretCtx = \ctx ->
|
||||
Task.loop ctx interpretCtxLoop
|
||||
|
|
|
@ -9,8 +9,8 @@ Variable := U8
|
|||
totalCount : Nat
|
||||
totalCount =
|
||||
0x7A # "z"
|
||||
- 0x61 # "a"
|
||||
+ 1
|
||||
- 0x61 # "a"
|
||||
+ 1
|
||||
|
||||
toStr : Variable -> Str
|
||||
toStr = \@Variable char ->
|
||||
|
@ -22,10 +22,9 @@ fromUtf8 : U8 -> Result Variable [InvalidVariableUtf8]
|
|||
fromUtf8 = \char ->
|
||||
if
|
||||
char
|
||||
>= 0x61 # "a"
|
||||
&& char
|
||||
<= 0x7A
|
||||
# "z"
|
||||
>= 0x61 # "a"
|
||||
&& char
|
||||
<= 0x7A # "z"
|
||||
then
|
||||
Ok (@Variable char)
|
||||
else
|
||||
|
|
|
@ -13,8 +13,8 @@ chunk = \@Handle handle -> Effect.after (Effect.getFileBytes handle) Task.succee
|
|||
open : Str -> Task.Task Handle *
|
||||
open = \path ->
|
||||
Effect.openFile path
|
||||
|> Effect.map (\id -> @Handle id)
|
||||
|> Effect.after Task.succeed
|
||||
|> Effect.map (\id -> @Handle id)
|
||||
|> Effect.after Task.succeed
|
||||
|
||||
close : Handle -> Task.Task {} *
|
||||
close = \@Handle handle -> Effect.after (Effect.closeFile handle) Task.succeed
|
||||
|
|
|
@ -8,12 +8,12 @@ loop : state, (state -> Task [Step state, Done done] err) -> Task done err
|
|||
loop = \state, step ->
|
||||
looper = \current ->
|
||||
step current
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|
||||
Effect.loop state looper
|
||||
|
||||
|
|
|
@ -90,8 +90,8 @@ translate = \child, toChild, toParent ->
|
|||
Button config label ->
|
||||
onPress = \parentState, event ->
|
||||
toChild parentState
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|
||||
Button { onPress } (translate label toChild toParent)
|
||||
|
||||
|
@ -136,8 +136,8 @@ list = \renderChild, parent, toChildren, toParent ->
|
|||
toChild
|
||||
\par, ch ->
|
||||
toChildren par
|
||||
|> List.set ch index
|
||||
|> toParent
|
||||
|> List.set ch index
|
||||
|> toParent
|
||||
|
||||
renderChild newChild
|
||||
|
||||
|
@ -165,8 +165,8 @@ translateOrDrop = \child, toChild, toParent ->
|
|||
when toChild parentState is
|
||||
Ok newChild ->
|
||||
newChild
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|> config.onPress event
|
||||
|> Action.map \c -> toParent parentState c
|
||||
|
||||
Err _ ->
|
||||
# The child was removed from the list before this onPress handler resolved.
|
||||
|
@ -182,7 +182,7 @@ translateOrDrop = \child, toChild, toParent ->
|
|||
when toChild parentState is
|
||||
Ok newChild ->
|
||||
renderChild newChild
|
||||
|> translateOrDrop toChild toParent
|
||||
|> translateOrDrop toChild toParent
|
||||
|
||||
Err _ ->
|
||||
None
|
||||
|
|
|
@ -226,37 +226,37 @@ append = \@Path prefix, @Path suffix ->
|
|||
NoInteriorNul suffixBytes ->
|
||||
# Neither prefix nor suffix had interior nuls, so the answer won't either
|
||||
List.append prefixBytes suffixBytes
|
||||
|> NoInteriorNul
|
||||
|> NoInteriorNul
|
||||
|
||||
ArbitraryBytes suffixBytes ->
|
||||
List.append prefixBytes suffixBytes
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
FromStr suffixStr ->
|
||||
# Append suffixStr by writing it to the end of prefixBytes
|
||||
Str.writeUtf8 suffixStr prefixBytes (List.len prefixBytes)
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
ArbitraryBytes prefixBytes ->
|
||||
when suffix is
|
||||
ArbitraryBytes suffixBytes | NoInteriorNul suffixBytes ->
|
||||
List.append prefixBytes suffixBytes
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
FromStr suffixStr ->
|
||||
# Append suffixStr by writing it to the end of prefixBytes
|
||||
Str.writeUtf8 suffixStr prefixBytes (List.len prefixBytes)
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
FromStr prefixStr ->
|
||||
when suffix is
|
||||
ArbitraryBytes suffixBytes | NoInteriorNul suffixBytes ->
|
||||
List.append (Str.toUtf8 prefixStr) suffixBytes
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
FromStr suffixStr ->
|
||||
Str.append prefixStr suffixStr
|
||||
|> FromStr
|
||||
|> FromStr
|
||||
|
||||
@Path content
|
||||
|
||||
|
@ -267,11 +267,11 @@ appendStr = \@Path prefix, suffixStr ->
|
|||
NoInteriorNul prefixBytes | ArbitraryBytes prefixBytes ->
|
||||
# Append suffixStr by writing it to the end of prefixBytes
|
||||
Str.writeUtf8 suffixStr prefixBytes (List.len prefixBytes)
|
||||
|> ArbitraryBytes
|
||||
|> ArbitraryBytes
|
||||
|
||||
FromStr prefixStr ->
|
||||
Str.append prefixStr suffixStr
|
||||
|> FromStr
|
||||
|> FromStr
|
||||
|
||||
@Path content
|
||||
|
||||
|
@ -354,9 +354,9 @@ withExtension = \@Path path, extension ->
|
|||
Err NotFound -> list
|
||||
|
||||
beforeDot
|
||||
|> List.reserve (1 + List.len bytes)
|
||||
|> List.append '.'
|
||||
|> List.concat bytes
|
||||
|> List.reserve (1 + List.len bytes)
|
||||
|> List.append '.'
|
||||
|> List.concat bytes
|
||||
|
||||
FromStr str ->
|
||||
beforeDot =
|
||||
|
@ -365,9 +365,9 @@ withExtension = \@Path path, extension ->
|
|||
Err NotFound -> str
|
||||
|
||||
beforeDot
|
||||
|> Str.reserve (1 + Str.byteCount str)
|
||||
|> Str.append "."
|
||||
|> Str.concat str
|
||||
|> Str.reserve (1 + Str.byteCount str)
|
||||
|> Str.append "."
|
||||
|> Str.concat str
|
||||
|
||||
# NOTE: no withExtensionBytes because it's too narrow. If you really need to get some
|
||||
# non-Unicode in there, do it with
|
||||
|
|
|
@ -5,5 +5,5 @@ interface Stdin
|
|||
line : Task Str * [Read [Stdin]*]*
|
||||
line =
|
||||
Effect.getLine
|
||||
|> Effect.map Ok
|
||||
|> InternalTask.fromEffect
|
||||
|> Effect.map Ok
|
||||
|> InternalTask.fromEffect
|
||||
|
|
|
@ -5,4 +5,4 @@ interface Stdout
|
|||
line : Str -> Task {} * [Write [Stdout]*]*
|
||||
line = \str ->
|
||||
Effect.map (Effect.putLine str) (\_ -> Ok {})
|
||||
|> InternalTask.fromEffect
|
||||
|> InternalTask.fromEffect
|
||||
|
|
|
@ -8,40 +8,40 @@ forever : Task val err fx -> Task * err fx
|
|||
forever = \task ->
|
||||
looper = \{} ->
|
||||
task
|
||||
|> InternalTask.toEffect
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok _ -> Step {}
|
||||
Err e -> Done (Err e)
|
||||
|> InternalTask.toEffect
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok _ -> Step {}
|
||||
Err e -> Done (Err e)
|
||||
|
||||
Effect.loop {} looper
|
||||
|> InternalTask.fromEffect
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
loop : state, (state -> Task [Step state, Done done] err fx) -> Task done err fx
|
||||
loop = \state, step ->
|
||||
looper = \current ->
|
||||
step current
|
||||
|> InternalTask.toEffect
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|> InternalTask.toEffect
|
||||
|> Effect.map
|
||||
\res ->
|
||||
when res is
|
||||
Ok (Step newState) -> Step newState
|
||||
Ok (Done result) -> Done (Ok result)
|
||||
Err e -> Done (Err e)
|
||||
|
||||
Effect.loop state looper
|
||||
|> InternalTask.fromEffect
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
succeed : val -> Task val * *
|
||||
succeed = \val ->
|
||||
Effect.always (Ok val)
|
||||
|> InternalTask.fromEffect
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
fail : err -> Task * err *
|
||||
fail = \val ->
|
||||
Effect.always (Err val)
|
||||
|> InternalTask.fromEffect
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
attempt : Task a b fx, (Result a b -> Task c d fx) -> Task c d fx
|
||||
attempt = \task, transform ->
|
||||
|
|
|
@ -104,17 +104,17 @@ append = \@Url urlStr, suffixUnencoded ->
|
|||
Ok { before, after } ->
|
||||
bytes =
|
||||
Str.countUtf8Bytes before
|
||||
+ 1 # for "/"
|
||||
+ Str.countUtf8Bytes suffix
|
||||
+ 1 # for "?"
|
||||
+ Str.countUtf8Bytes after
|
||||
+ 1 # for "/"
|
||||
+ Str.countUtf8Bytes suffix
|
||||
+ 1 # for "?"
|
||||
+ Str.countUtf8Bytes after
|
||||
|
||||
before
|
||||
|> Str.reserve bytes
|
||||
|> appendHelp suffix
|
||||
|> Str.concat "?"
|
||||
|> Str.concat after
|
||||
|> @Url
|
||||
|> Str.reserve bytes
|
||||
|> appendHelp suffix
|
||||
|> Str.concat "?"
|
||||
|> Str.concat after
|
||||
|> @Url
|
||||
|
||||
Err NotFound ->
|
||||
# There wasn't a query, but there might still be a fragment
|
||||
|
@ -122,17 +122,17 @@ append = \@Url urlStr, suffixUnencoded ->
|
|||
Ok { before, after } ->
|
||||
bytes =
|
||||
Str.countUtf8Bytes before
|
||||
+ 1 # for "/"
|
||||
+ Str.countUtf8Bytes suffix
|
||||
+ 1 # for "#"
|
||||
+ Str.countUtf8Bytes after
|
||||
+ 1 # for "/"
|
||||
+ Str.countUtf8Bytes suffix
|
||||
+ 1 # for "#"
|
||||
+ Str.countUtf8Bytes after
|
||||
|
||||
before
|
||||
|> Str.reserve bytes
|
||||
|> appendHelp suffix
|
||||
|> Str.concat "#"
|
||||
|> Str.concat after
|
||||
|> @Url
|
||||
|> Str.reserve bytes
|
||||
|> appendHelp suffix
|
||||
|> Str.concat "#"
|
||||
|> Str.concat after
|
||||
|> @Url
|
||||
|
||||
Err NotFound ->
|
||||
# No query and no fragment, so just append it
|
||||
|
@ -169,8 +169,8 @@ appendHelp = \prefix, suffix ->
|
|||
else
|
||||
# Neither is empty, but neither has a "/", so add one in between.
|
||||
prefix
|
||||
|> Str.concat "/"
|
||||
|> Str.concat suffix
|
||||
|> Str.concat "/"
|
||||
|> Str.concat suffix
|
||||
|
||||
## Internal helper. This is intentionally unexposed so that you don't accidentally
|
||||
## double-encode things. If you really want to percent-encode an arbitrary string,
|
||||
|
@ -194,30 +194,30 @@ percentEncode = \input ->
|
|||
# Spec for percent-encoding: https://www.ietf.org/rfc/rfc3986.txt
|
||||
if
|
||||
(byte >= 97 && byte <= 122) # lowercase ASCII
|
||||
|| (byte >= 65 && byte <= 90) # uppercase ASCII
|
||||
|| (byte >= 48 && byte <= 57) # digit
|
||||
|| (byte >= 65 && byte <= 90) # uppercase ASCII
|
||||
|| (byte >= 48 && byte <= 57) # digit
|
||||
then
|
||||
# This is the most common case: an unreserved character,
|
||||
# which needs no encoding in a path
|
||||
Str.appendScalar output (Num.toU32 byte)
|
||||
|> Result.withDefault "" # this will never fail
|
||||
|> Result.withDefault "" # this will never fail
|
||||
else
|
||||
when byte is
|
||||
46 # '.'
|
||||
| 95 # '_'
|
||||
| 126 # '~'
|
||||
| 150 -> # '-'
|
||||
| 95 # '_'
|
||||
| 126 # '~'
|
||||
| 150 -> # '-'
|
||||
# These special characters can all be unescaped in paths
|
||||
Str.appendScalar output (Num.toU32 byte)
|
||||
|> Result.withDefault "" # this will never fail
|
||||
|> Result.withDefault "" # this will never fail
|
||||
|
||||
_ ->
|
||||
# This needs encoding in a path
|
||||
suffix =
|
||||
Str.toUtf8 percentEncoded
|
||||
|> List.sublist { len: 3, start: 3 * Num.toNat byte }
|
||||
|> Str.fromUtf8
|
||||
|> Result.withDefault "" # This will never fail
|
||||
|> List.sublist { len: 3, start: 3 * Num.toNat byte }
|
||||
|> Str.fromUtf8
|
||||
|> Result.withDefault "" # This will never fail
|
||||
|
||||
Str.concat output suffix
|
||||
|
||||
|
@ -251,20 +251,20 @@ appendParam = \@Url urlStr, key, value ->
|
|||
|
||||
bytes =
|
||||
Str.countUtf8Bytes withoutFragment
|
||||
+ 1 # for "?" or "&"
|
||||
+ Str.countUtf8Bytes encodedKey
|
||||
+ 1 # for "="
|
||||
+ Str.countUtf8Bytes encodedValue
|
||||
+ Str.countUtf8Bytes afterQuery
|
||||
+ 1 # for "?" or "&"
|
||||
+ Str.countUtf8Bytes encodedKey
|
||||
+ 1 # for "="
|
||||
+ Str.countUtf8Bytes encodedValue
|
||||
+ Str.countUtf8Bytes afterQuery
|
||||
|
||||
withoutFragment
|
||||
|> Str.reserve bytes
|
||||
|> Str.concat (if hasQuery (@Url withoutFragment) then "&" else "?")
|
||||
|> Str.concat encodedKey
|
||||
|> Str.concat "="
|
||||
|> Str.concat encodedValue
|
||||
|> Str.concat afterQuery
|
||||
|> @Url
|
||||
|> Str.reserve bytes
|
||||
|> Str.concat (if hasQuery (@Url withoutFragment) then "&" else "?")
|
||||
|> Str.concat encodedKey
|
||||
|> Str.concat "="
|
||||
|> Str.concat encodedValue
|
||||
|> Str.concat afterQuery
|
||||
|> @Url
|
||||
|
||||
## Replaces the URL's [query](https://en.wikipedia.org/wiki/URL#Syntax)—the part after
|
||||
## the `?`, if it has one, but before any `#` it might have.
|
||||
|
@ -300,16 +300,16 @@ withQuery = \@Url urlStr, queryStr ->
|
|||
else
|
||||
bytes =
|
||||
Str.countUtf8Bytes beforeQuery
|
||||
+ 1 # for "?"
|
||||
+ Str.countUtf8Bytes queryStr
|
||||
+ Str.countUtf8Bytes afterQuery
|
||||
+ 1 # for "?"
|
||||
+ Str.countUtf8Bytes queryStr
|
||||
+ Str.countUtf8Bytes afterQuery
|
||||
|
||||
beforeQuery
|
||||
|> Str.reserve bytes
|
||||
|> Str.concat "?"
|
||||
|> Str.concat queryStr
|
||||
|> Str.concat afterQuery
|
||||
|> @Url
|
||||
|> Str.reserve bytes
|
||||
|> Str.concat "?"
|
||||
|> Str.concat queryStr
|
||||
|> Str.concat afterQuery
|
||||
|> @Url
|
||||
|
||||
## Returns the URL's [query](https://en.wikipedia.org/wiki/URL#Syntax)—the part after
|
||||
## the `?`, if it has one, but before any `#` it might have.
|
||||
|
@ -348,7 +348,7 @@ hasQuery = \@Url urlStr ->
|
|||
# TODO use Str.contains once it exists. It should have a "fast path"
|
||||
# with SIMD iteration if the string is small enough to fit in a SIMD register.
|
||||
Str.toUtf8 urlStr
|
||||
|> List.contains (Num.toU8 '?')
|
||||
|> List.contains (Num.toU8 '?')
|
||||
|
||||
## Returns the URL's [fragment](https://en.wikipedia.org/wiki/URL#Syntax)—the part after
|
||||
## the `#`, if it has one.
|
||||
|
@ -418,7 +418,7 @@ hasFragment = \@Url urlStr ->
|
|||
# TODO use Str.contains once it exists. It should have a "fast path"
|
||||
# with SIMD iteration if the string is small enough to fit in a SIMD register.
|
||||
Str.toUtf8 urlStr
|
||||
|> List.contains (Num.toU8 '#')
|
||||
|> List.contains (Num.toU8 '#')
|
||||
|
||||
strWithCapacity : Nat -> Str
|
||||
strWithCapacity = \cap ->
|
||||
|
|
|
@ -21,13 +21,13 @@ echo = \shout ->
|
|||
List.repeat spaceInUtf8 length
|
||||
|
||||
shout
|
||||
|> Str.toUtf8
|
||||
|> List.mapWithIndex
|
||||
(\_, i ->
|
||||
length = (List.len (Str.toUtf8 shout) - i)
|
||||
phrase = (List.split (Str.toUtf8 shout) length).before
|
||||
|> 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 ""
|
||||
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