Skip to content

Commit

Permalink
Format stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jan 23, 2024
1 parent 0c4b1af commit f7f84a0
Show file tree
Hide file tree
Showing 37 changed files with 1,758 additions and 1,906 deletions.
33 changes: 15 additions & 18 deletions stdlib/array.gr
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ provide let make: (Number, a) => Array<a> = (length: Number, item: a) => {
* @since v0.1.0
*/
@unsafe
provide let init: (Number, Number => a) => Array<a> =
(
length: Number,
fn: Number => a,
) => {
provide let init: (Number, Number => a) => Array<a> = (
length: Number,
fn: Number => a,
) => {
from WasmI32 use { (+), (*), (<) }
checkLength(length)
let length = coerceNumberToWasmI32(length)
Expand Down Expand Up @@ -793,8 +792,10 @@ provide let filteri = (fn, array) => {
*/
provide let unique = array => {
// TODO(#1651): improve performance
filteri((el, index) =>
findIndex(value => value == el, array) == Some(index), array)
filteri(
(el, index) => findIndex(value => value == el, array) == Some(index),
array
)
}

/**
Expand Down Expand Up @@ -1859,11 +1860,9 @@ provide module Immutable {
*/

provide let product = (array1, array2) => {
fromList(
reduceRight((x1, list) => {
reduceRight((x2, list) => [(x1, x2), ...list], list, array2)
}, [], array1)
)
fromList(reduceRight((x1, list) => {
reduceRight((x2, list) => [(x1, x2), ...list], list, array2)
}, [], array1))
}

/**
Expand Down Expand Up @@ -2011,12 +2010,10 @@ provide module Immutable {
let begin = clampIndex(array.length, start)
let end = clampIndex(array.length, end)
let mut i = array.length
fromList(
reduceRight((x, acc) => {
i -= 1
if (i >= begin && i < end) [x, ...acc] else acc
}, [], array)
)
fromList(reduceRight((x, acc) => {
i -= 1
if (i >= begin && i < end) [x, ...acc] else acc
}, [], array))
}

/**
Expand Down
40 changes: 16 additions & 24 deletions stdlib/bigint.gr
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ provide let abs = (num: BigInt) => {
*/
@unsafe
provide let (+) = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

/**
Expand All @@ -102,9 +101,8 @@ provide let (+) = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let (-) = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

/**
Expand All @@ -119,9 +117,8 @@ provide let (-) = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let (*) = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

// For further reading on Truncated vs. Floored division: https://en.wikipedia.org/wiki/Modulo_operation
Expand All @@ -139,9 +136,8 @@ provide let (*) = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let (/) = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.quot(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.quot(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

/**
Expand All @@ -156,9 +152,8 @@ provide let (/) = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let rem = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.rem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.rem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

/**
Expand Down Expand Up @@ -191,9 +186,8 @@ provide let quotRem = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let gcd = (num1: BigInt, num2: BigInt) => {
WasmI32.toGrain(
BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
): BigInt
WasmI32.toGrain(BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
BigInt
}

/**
Expand Down Expand Up @@ -428,9 +422,8 @@ provide let (^) = (num1: BigInt, num2: BigInt) => {
*/
@unsafe
provide let clz = (num: BigInt) => {
WasmI32.toGrain(
DS.newInt32(BI.countLeadingZeros(WasmI32.fromGrain(num)))
): Int32
WasmI32.toGrain(DS.newInt32(BI.countLeadingZeros(WasmI32.fromGrain(num)))):
Int32
}

/**
Expand All @@ -443,9 +436,8 @@ provide let clz = (num: BigInt) => {
*/
@unsafe
provide let ctz = (num: BigInt) => {
WasmI32.toGrain(
DS.newInt64(BI.countTrailingZeros(WasmI32.fromGrain(num)))
): Int64
WasmI32.toGrain(DS.newInt64(BI.countTrailingZeros(WasmI32.fromGrain(num)))):
Int64
}

/**
Expand Down
18 changes: 9 additions & 9 deletions stdlib/buffer.gr
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ let addUint32help = (value, buffer) => {
* @since v0.4.0
*/
provide let make = initialSize => {
if (initialSize < 0)
throw Exception.InvalidArgument("Buffers size must be >= 0")
if (initialSize < 0) throw Exception.InvalidArgument(
"Buffers size must be >= 0",
)
{ len: 0, initialSize, data: Bytes.make(initialSize) }
}

Expand Down Expand Up @@ -378,13 +379,12 @@ provide let addStringSlice = (start: Number, end, string, buffer) => {
* @since v0.4.0
*/
@unsafe
provide let addBytesSlice =
(
start: Number,
length: Number,
bytes: Bytes,
buffer: Buffer,
) => {
provide let addBytesSlice = (
start: Number,
length: Number,
bytes: Bytes,
buffer: Buffer,
) => {
if (length != 0) {
from WasmI32 use { (-), (<), (>), (>=) }

Expand Down
17 changes: 8 additions & 9 deletions stdlib/bytes.gr
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ provide let slice = (start: Number, length: Number, bytes: Bytes) => {
let length = coerceNumberToWasmI32(length)
if (start + length > size) {
throw Exception.InvalidArgument(
"The given index and length do not specify a valid range of bytes"
"The given index and length do not specify a valid range of bytes",
)
}
let dst = allocateBytes(length)
Expand Down Expand Up @@ -258,14 +258,13 @@ provide let resize = (left: Number, right: Number, bytes: Bytes) => {
* @since v0.3.2
*/
@unsafe
provide let move =
(
srcIndex: Number,
dstIndex: Number,
length: Number,
src: Bytes,
dst: Bytes,
) => {
provide let move = (
srcIndex: Number,
dstIndex: Number,
length: Number,
src: Bytes,
dst: Bytes,
) => {
from WasmI32 use { (+), (>) }
let srcIndexOrig = srcIndex
let dstIndexOrig = dstIndex
Expand Down
4 changes: 2 additions & 2 deletions stdlib/char.gr
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ provide let toString = (char: Char) => {
offset = 0xF0n
}
let string = allocateString(count + 1n)
WasmI32.store8(string, (usv >>> 6n * count) + offset, 8n)
WasmI32.store8(string, (usv >>> (6n * count)) + offset, 8n)

let mut n = 0n
while (count > 0n) {
n += 1n
let temp = usv >>> 6n * (count - 1n)
let temp = usv >>> (6n * (count - 1n))
WasmI32.store8(string + n, 0x80n | temp & 0x3Fn, 8n)
count -= 1n
}
Expand Down
13 changes: 5 additions & 8 deletions stdlib/hash.gr
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,12 @@ let rec hashOne = (val, depth) => {
} else if ((val & Tags._GRAIN_NUMBER_TAG_MASK) != 0n) {
hash32(val)
} else if (
(val & Tags._GRAIN_GENERIC_TAG_MASK) == Tags._GRAIN_GENERIC_HEAP_TAG_TYPE
(val & Tags._GRAIN_GENERIC_TAG_MASK) ==
Tags._GRAIN_GENERIC_HEAP_TAG_TYPE
) {
let heapPtr = val
match (WasmI32.load(heapPtr, 0n)) {
t when (
t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG
) => {
t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => {
let length = WasmI32.load(heapPtr, 4n)
let extra = length % 4n
let l = length - extra
Expand Down Expand Up @@ -215,11 +214,9 @@ let rec hashOne = (val, depth) => {
},
}
},
t when (
t == Tags._GRAIN_INT32_HEAP_TAG ||
t when t == Tags._GRAIN_INT32_HEAP_TAG ||
t == Tags._GRAIN_FLOAT32_HEAP_TAG ||
t == Tags._GRAIN_UINT32_HEAP_TAG
) => {
t == Tags._GRAIN_UINT32_HEAP_TAG => {
hash32(WasmI32.load(heapPtr, 4n))
},
t when t == Tags._GRAIN_UINT64_HEAP_TAG => {
Expand Down
6 changes: 4 additions & 2 deletions stdlib/int32.gr
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ let rec expBySquaring = (y, x, n) => {
* @since v0.6.0
*/
provide let (**) = (base, power) => {
if (power < 0l) return expBySquaring(1l, 1l / base, power * -1l)
else return expBySquaring(1l, base, power)
if (power < 0l)
return expBySquaring(1l, 1l / base, power * -1l)
else
return expBySquaring(1l, base, power)
}
6 changes: 4 additions & 2 deletions stdlib/int64.gr
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ let rec expBySquaring = (y, x, n) => {
* @since v0.6.0
*/
provide let (**) = (base, power) => {
if (power < 0L) return expBySquaring(1L, 1L / base, power * -1L)
else return expBySquaring(1L, base, power)
if (power < 0L)
return expBySquaring(1L, 1L / base, power * -1L)
else
return expBySquaring(1L, base, power)
}
40 changes: 27 additions & 13 deletions stdlib/list.gr
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,10 @@ provide let filteri = (fn, list) => {
match (list) {
[] => [],
[first, ...rest] =>
if (fn(first, index)) [first, ...iter(fn, rest, index + 1)]
else iter(fn, rest, index + 1),
if (fn(first, index))
[first, ...iter(fn, rest, index + 1)]
else
iter(fn, rest, index + 1),
}
}
iter(fn, list, 0)
Expand Down Expand Up @@ -469,8 +471,10 @@ provide let rec insert = (index, value, list) => {
match (list) {
[] => if (index == 0) [value] else fail "insert index is out-of-bounds",
[first, ...rest] =>
if (index == 0) [value, ...list]
else [first, ...insert(index - 1, value, rest)],
if (index == 0)
[value, ...list]
else
[first, ...insert(index - 1, value, rest)],
}
}
}
Expand Down Expand Up @@ -516,8 +520,10 @@ provide let part = (count, list) => {
[] =>
if (count > 0) fail "part count is out-of-bounds" else (list1, list2),
[first, ...rest] =>
if (count > 0) iter([first, ...list1], rest, count - 1)
else (list1, list2),
if (count > 0)
iter([first, ...list1], rest, count - 1)
else
(list1, list2),
}
}
let (pt1, pt2) = iter([], list, count)
Expand Down Expand Up @@ -556,8 +562,10 @@ provide let rotate = (count, list) => {
match (list2) {
[] => if (count > 0) None else Some((list1, list2)),
[first, ...rest] =>
if (count > 0) iter([first, ...list1], rest, count - 1)
else Some((list1, list2)),
if (count > 0)
iter([first, ...list1], rest, count - 1)
else
Some((list1, list2)),
}
}
let res = iter([], list, count)
Expand Down Expand Up @@ -587,8 +595,10 @@ provide let unique = list => {
match (list) {
[] => reverse(acc),
[first, ...rest] =>
if (contains(first, acc)) iter(rest, acc)
else iter(rest, [first, ...acc]),
if (contains(first, acc))
iter(rest, acc)
else
iter(rest, [first, ...acc]),
}
}
iter(list, [])
Expand Down Expand Up @@ -662,9 +672,13 @@ provide let zipWith = (fn, list1, list2) => {
* @since v0.5.3
*/
provide let unzip = list => {
reduceRight(((first, second), (firstUnzipped, secondUnzipped)) => {
([first, ...firstUnzipped], [second, ...secondUnzipped])
}, ([], []), list)
reduceRight(
((first, second), (firstUnzipped, secondUnzipped)) => {
([first, ...firstUnzipped], [second, ...secondUnzipped])
},
([], []),
list
)
}

/**
Expand Down
Loading

0 comments on commit f7f84a0

Please sign in to comment.