Skip to content

Commit

Permalink
Merge pull request #7463 from smores56/auto-snake-case
Browse files Browse the repository at this point in the history
Move builtins to snake_case
  • Loading branch information
smores56 authored Jan 8, 2025
2 parents 83c78b0 + 68e82b8 commit 96fc573
Show file tree
Hide file tree
Showing 398 changed files with 18,022 additions and 12,349 deletions.
8 changes: 4 additions & 4 deletions crates/cli/tests/benchmarks/AStar.roc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ initial_model = \start -> {
cheapest_open : (position -> F64), Model position -> Result position {} where position implements Hash & Eq
cheapest_open = \cost_fn, model ->
model.open_set
|> Set.toList
|> List.keepOks(
|> Set.to_list
|> List.keep_oks(
\position ->
when Dict.get(model.costs, position) is
Err(_) -> Err({})
Expand All @@ -33,7 +33,7 @@ cheapest_open = \cost_fn, model ->
|> Quicksort.sort_by(.cost)
|> List.first
|> Result.map(.position)
|> Result.mapErr(\_ -> {})
|> Result.map_err(\_ -> {})

reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq
reconstruct_path = \came_from, goal ->
Expand All @@ -52,7 +52,7 @@ update_cost = \current, neighbor, model ->
distance_to =
reconstruct_path(new_came_from, neighbor)
|> List.len
|> Num.toFrac
|> Num.to_frac

new_model =
{ model &
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/benchmarks/Base64.roc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from_bytes = \bytes ->
# base 64 encoding from a string
from_str : Str -> Result Str [InvalidInput]
from_str = \str ->
from_bytes(Str.toUtf8(str))
from_bytes(Str.to_utf8(str))

# base64-encode bytes to the original
to_bytes : Str -> Result (List U8) [InvalidInput]
Expand All @@ -27,7 +27,7 @@ to_str : Str -> Result Str [InvalidInput]
to_str = \str ->
when to_bytes(str) is
Ok(bytes) ->
when Str.fromUtf8(bytes) is
when Str.from_utf8(bytes) is
Ok(v) ->
Ok(v)

Expand Down
36 changes: 18 additions & 18 deletions crates/cli/tests/benchmarks/Base64/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ loop_help = \{ remaining, string } ->
if remaining >= 3 then
Bytes.Decode.map3(Bytes.Decode.u8, Bytes.Decode.u8, Bytes.Decode.u8, \x, y, z ->
a : U32
a = Num.intCast(x)
a = Num.int_cast(x)
b : U32
b = Num.intCast(y)
b = Num.int_cast(y)
c : U32
c = Num.intCast(z)
combined = Num.bitwiseOr(Num.bitwiseOr(Num.shiftLeftBy(a, 16), Num.shiftLeftBy(b, 8)), c)
c = Num.int_cast(z)
combined = Num.bitwise_or(Num.bitwise_or(Num.shift_left_by(a, 16), Num.shift_left_by(b, 8)), c)

Loop({
remaining: remaining - 3,
Expand All @@ -31,24 +31,24 @@ loop_help = \{ remaining, string } ->
Bytes.Decode.map2(Bytes.Decode.u8, Bytes.Decode.u8, \x, y ->

a : U32
a = Num.intCast(x)
a = Num.int_cast(x)
b : U32
b = Num.intCast(y)
combined = Num.bitwiseOr(Num.shiftLeftBy(a, 16), Num.shiftLeftBy(b, 8))
b = Num.int_cast(y)
combined = Num.bitwise_or(Num.shift_left_by(a, 16), Num.shift_left_by(b, 8))

Done(Str.concat(string, bits_to_chars(combined, 1))))
else
# remaining = 1
Bytes.Decode.map(Bytes.Decode.u8, \x ->

a : U32
a = Num.intCast(x)
a = Num.int_cast(x)

Done(Str.concat(string, bits_to_chars(Num.shiftLeftBy(a, 16), 2))))
Done(Str.concat(string, bits_to_chars(Num.shift_left_by(a, 16), 2))))

bits_to_chars : U32, Int * -> Str
bits_to_chars = \bits, missing ->
when Str.fromUtf8(bits_to_chars_help(bits, missing)) is
when Str.from_utf8(bits_to_chars_help(bits, missing)) is
Ok(str) -> str
Err(_) -> ""

Expand All @@ -63,23 +63,23 @@ bits_to_chars_help = \bits, missing ->
# with `0b111111` (which is 2^6 - 1 or 63) (so, 6 1s) to remove unwanted bits on the left.
# any 6-bit number is a valid base64 digit, so this is actually safe
p =
Num.shiftRightZfBy(bits, 18)
|> Num.intCast
Num.shift_right_zf_by(bits, 18)
|> Num.int_cast
|> unsafe_to_char

q =
Num.bitwiseAnd(Num.shiftRightZfBy(bits, 12), lowest6_bits_mask)
|> Num.intCast
Num.bitwise_and(Num.shift_right_zf_by(bits, 12), lowest6_bits_mask)
|> Num.int_cast
|> unsafe_to_char

r =
Num.bitwiseAnd(Num.shiftRightZfBy(bits, 6), lowest6_bits_mask)
|> Num.intCast
Num.bitwise_and(Num.shift_right_zf_by(bits, 6), lowest6_bits_mask)
|> Num.int_cast
|> unsafe_to_char

s =
Num.bitwiseAnd(bits, lowest6_bits_mask)
|> Num.intCast
Num.bitwise_and(bits, lowest6_bits_mask)
|> Num.int_cast
|> unsafe_to_char

equals : U8
Expand Down
30 changes: 15 additions & 15 deletions crates/cli/tests/benchmarks/Base64/Encode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ InvalidChar : U8
to_bytes : Str -> List U8
to_bytes = \str ->
str
|> Str.toUtf8
|> Str.to_utf8
|> encode_chunks
|> Bytes.Encode.sequence
|> Bytes.Encode.encode
Expand Down Expand Up @@ -73,18 +73,18 @@ encode_characters = \a, b, c, d ->
n2 = unsafe_convert_char(b)

x : U32
x = Num.intCast(n1)
x = Num.int_cast(n1)

y : U32
y = Num.intCast(n2)
y = Num.int_cast(n2)

if d == equals then
if c == equals then
n = Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12))
n = Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12))

# masking higher bits is not needed, Encode.unsignedInt8 ignores higher bits
b1 : U8
b1 = Num.intCast(Num.shiftRightBy(n, 16))
b1 = Num.int_cast(Num.shift_right_by(n, 16))

Ok(Bytes.Encode.u8(b1))
else if !(is_valid_char(c)) then
Expand All @@ -93,12 +93,12 @@ encode_characters = \a, b, c, d ->
n3 = unsafe_convert_char(c)

z : U32
z = Num.intCast(n3)
z = Num.int_cast(n3)

n = Num.bitwiseOr(Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12)), Num.shiftLeftBy(z, 6))
n = Num.bitwise_or(Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12)), Num.shift_left_by(z, 6))

combined : U16
combined = Num.intCast(Num.shiftRightBy(n, 8))
combined = Num.int_cast(Num.shift_right_by(n, 8))

Ok(Bytes.Encode.u16(BE, combined))
else if !(is_valid_char(d)) then
Expand All @@ -108,22 +108,22 @@ encode_characters = \a, b, c, d ->
n4 = unsafe_convert_char(d)

z : U32
z = Num.intCast(n3)
z = Num.int_cast(n3)

w : U32
w = Num.intCast(n4)
w = Num.int_cast(n4)

n =
Num.bitwiseOr(
Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12)),
Num.bitwiseOr(Num.shiftLeftBy(z, 6), w),
Num.bitwise_or(
Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12)),
Num.bitwise_or(Num.shift_left_by(z, 6), w),
)

b3 : U8
b3 = Num.intCast(n)
b3 = Num.int_cast(n)

combined : U16
combined = Num.intCast(Num.shiftRightBy(n, 8))
combined = Num.int_cast(Num.shift_right_by(n, 8))

Ok(Bytes.Encode.sequence([Bytes.Encode.u16(BE, combined), Bytes.Encode.u8(b3)]))

Expand Down
136 changes: 71 additions & 65 deletions crates/cli/tests/benchmarks/Bytes/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,105 +7,111 @@ DecodeProblem : [OutOfBytes]
ByteDecoder a := State -> [Good State a, Bad DecodeProblem]

decode : List U8, ByteDecoder a -> Result a DecodeProblem
decode = \bytes, @ByteDecoder decoder ->
when decoder { bytes, cursor: 0 } is
Good _ value ->
Ok value
decode = \bytes, @ByteDecoder(decoder) ->
when decoder({ bytes, cursor: 0 }) is
Good(_, value) ->
Ok(value)

Bad e ->
Err e
Bad(e) ->
Err(e)

succeed : a -> ByteDecoder a
succeed = \value -> @ByteDecoder \state -> Good state value
succeed = \value -> @ByteDecoder(\state -> Good(state, value))

map : ByteDecoder a, (a -> b) -> ByteDecoder b
map = \@ByteDecoder decoder, transform ->
@ByteDecoder
map = \@ByteDecoder(decoder), transform ->
@ByteDecoder(
\state ->
when decoder state is
Good state1 value ->
Good state1 (transform value)
when decoder(state) is
Good(state1, value) ->
Good(state1, transform(value))

Bad e ->
Bad e
Bad(e) ->
Bad(e),
)

map2 : ByteDecoder a, ByteDecoder b, (a, b -> c) -> ByteDecoder c
map2 = \@ByteDecoder decoder1, @ByteDecoder decoder2, transform ->
@ByteDecoder
map2 = \@ByteDecoder(decoder1), @ByteDecoder(decoder2), transform ->
@ByteDecoder(
\state1 ->
when decoder1 state1 is
Good state2 a ->
when decoder2 state2 is
Good state3 b ->
Good state3 (transform a b)
when decoder1(state1) is
Good(state2, a) ->
when decoder2(state2) is
Good(state3, b) ->
Good(state3, transform(a, b))

Bad e ->
Bad e
Bad(e) ->
Bad(e)

Bad e ->
Bad e
Bad(e) ->
Bad(e),
)

map3 : ByteDecoder a, ByteDecoder b, ByteDecoder c, (a, b, c -> d) -> ByteDecoder d
map3 = \@ByteDecoder decoder1, @ByteDecoder decoder2, @ByteDecoder decoder3, transform ->
@ByteDecoder
map3 = \@ByteDecoder(decoder1), @ByteDecoder(decoder2), @ByteDecoder(decoder3), transform ->
@ByteDecoder(
\state1 ->
when decoder1 state1 is
Good state2 a ->
when decoder2 state2 is
Good state3 b ->
when decoder3 state3 is
Good state4 c ->
Good state4 (transform a b c)
when decoder1(state1) is
Good(state2, a) ->
when decoder2(state2) is
Good(state3, b) ->
when decoder3(state3) is
Good(state4, c) ->
Good(state4, transform(a, b, c))

Bad e ->
Bad e
Bad(e) ->
Bad(e)

Bad e ->
Bad e
Bad(e) ->
Bad(e)

Bad e ->
Bad e
Bad(e) ->
Bad(e),
)

after : ByteDecoder a, (a -> ByteDecoder b) -> ByteDecoder b
after = \@ByteDecoder decoder, transform ->
@ByteDecoder
after = \@ByteDecoder(decoder), transform ->
@ByteDecoder(
\state ->
when decoder state is
Good state1 value ->
(@ByteDecoder decoder1) = transform value
when decoder(state) is
Good(state1, value) ->
@ByteDecoder(decoder1) = transform(value)

decoder1 state1
decoder1(state1)

Bad e ->
Bad e
Bad(e) ->
Bad(e),
)

u8 : ByteDecoder U8
u8 = @ByteDecoder
u8 = @ByteDecoder(
\state ->
when List.get state.bytes state.cursor is
Ok b ->
Good { state & cursor: state.cursor + 1 } b
when List.get(state.bytes, state.cursor) is
Ok(b) ->
Good({ state & cursor: state.cursor + 1 }, b)

Err _ ->
Bad OutOfBytes
Err(_) ->
Bad(OutOfBytes),
)

Step state b : [Loop state, Done b]

loop : (state -> ByteDecoder (Step state a)), state -> ByteDecoder a
loop = \stepper, initial ->
@ByteDecoder
@ByteDecoder(
\state ->
loopHelp stepper initial state
loop_help(stepper, initial, state),
)

loopHelp = \stepper, accum, state ->
(@ByteDecoder stepper1) = stepper accum
loop_help = \stepper, accum, state ->
@ByteDecoder(stepper1) = stepper(accum)

when stepper1 state is
Good newState (Done value) ->
Good newState value
when stepper1(state) is
Good(new_state, Done(value)) ->
Good(new_state, value)

Good newState (Loop newAccum) ->
loopHelp stepper newAccum newState
Good(new_state, Loop(new_accum)) ->
loop_help(stepper, new_accum, new_state)

Bad e ->
Bad e
Bad(e) ->
Bad(e)
Loading

0 comments on commit 96fc573

Please sign in to comment.