Skip to content

Commit

Permalink
chore(stdlib)!: Use default arguments in more of stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko committed Jan 29, 2024
1 parent 6701170 commit 3e4b120
Show file tree
Hide file tree
Showing 28 changed files with 425 additions and 701 deletions.
20 changes: 10 additions & 10 deletions compiler/test/__snapshots__/provides.82c10ab4.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ provides › provide12
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1200 (mut i32)))
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1198 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1198 (mut i32)))
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1196 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1200 (param i32 i32 i32) (result i32)))
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1198 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1198 (param i32 i32 i32) (result i32)))
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1196 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 1))
(memory $0 0)
(elem $elem (global.get $relocBase_0) $lam_lambda_1199)
(elem $elem (global.get $relocBase_0) $lam_lambda_1197)
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $lam_lambda_1199 (param $0 i32) (param $1 i32) (result i32)
(func $lam_lambda_1197 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
Expand Down Expand Up @@ -112,10 +112,10 @@ provides › provide12
)
)
)
(return_call $print_1200
(return_call $print_1198
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $print_1200)
(global.get $print_1198)
)
(local.get $9)
(local.get $8)
Expand Down Expand Up @@ -168,10 +168,10 @@ provides › provide12
)
)
)
(return_call $apply_1198
(return_call $apply_1196
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $apply_1198)
(global.get $apply_1196)
)
(local.get $6)
)
Expand Down
12 changes: 6 additions & 6 deletions compiler/test/stdlib/array.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ assert Array.slice(1, end=4, [>]) == [>]
// Array.sort
// Numbers
let arr = [> 5, 3, 4, 6, 2, 1]
Array.sort(compare, arr)
Array.sort(arr)
assert arr == [> 1, 2, 3, 4, 5, 6]
// Empty
let arr = [>]
Array.sort(compare, arr)
Array.sort(arr)
assert arr == [>]
// Strings
let compareLengths = (left, right) => {
Expand All @@ -384,7 +384,7 @@ let compareLengths = (left, right) => {
}
}
let arr = [> "a", "abcde", "abc", "ab", "abcd", "a"]
let result = Array.sort(compareLengths, arr)
let result = Array.sort(compare=compareLengths, arr)
assert arr == [> "a", "a", "ab", "abc", "abcd", "abcde"]
// Returns void
assert result == void
Expand Down Expand Up @@ -814,11 +814,11 @@ module Immutable {
// Array.sort
// Numbers
let arr = fromList([5, 3, 4, 6, 2, 1])
let result = Array.sort(compare, arr)
let result = Array.sort(compare=compare, arr)
assert result == fromList([1, 2, 3, 4, 5, 6])
// Empty
let arr = fromList([])
let result = Array.sort(compare, arr)
let result = Array.sort(arr)
assert result == fromList([])
// Strings
let compareLengths = (left, right) => {
Expand All @@ -829,7 +829,7 @@ module Immutable {
}
}
let arr = fromList(["a", "abcde", "abc", "ab", "abcd", "a"])
let result = Array.sort(compareLengths, arr)
let result = Array.sort(compare=compareLengths, arr)
assert result == fromList(["a", "a", "ab", "abc", "abcd", "abcde"])

// Array.rotate
Expand Down
10 changes: 6 additions & 4 deletions compiler/test/stdlib/list.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ assert join(", ", ["a", "b", "c"]) == "a, b, c"
assert join(", ", []) == ""

// List.sort
assert sort(compare, [3, 5, 2, 4, 1]) == [1, 2, 3, 4, 5]
assert sort(compare, []) == []
assert sort([3, 5, 2, 4, 1]) == [1, 2, 3, 4, 5]
assert sort([]) == []
let compareLengths = (left, right) => {
match ((String.length(left), String.length(right))) {
(left, right) when left > right => 1,
Expand All @@ -265,5 +265,7 @@ let compareLengths = (left, right) => {
}
}
let list = ["a", "abcde", "abc", "ab", "abcd", "a"]
assert sort(compareLengths, list) == ["a", "a", "ab", "abc", "abcd", "abcde"]
assert sort(compareLengths, ["a", "a", "a", "a"]) == ["a", "a", "a", "a"]
assert sort(compare=compareLengths, list) ==
["a", "a", "ab", "abc", "abcd", "abcde"]
assert sort(compare=compareLengths, ["a", "a", "a", "a"]) ==
["a", "a", "a", "a"]
2 changes: 1 addition & 1 deletion compiler/test/stdlib/map.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ assert !Map.contains(Wood, fa)

// Resizes the map when it grows

let resize = Map.makeSized(1)
let resize = Map.make(size=1)

assert Map.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }

Expand Down
14 changes: 7 additions & 7 deletions compiler/test/stdlib/path.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ List.forEach(({ pathStr, expParent, expStr, expName }: ParseDirTestData) => {
assert fs("") == fs(".")
assert fs(".") == fs("./")
assert fs("dir") != fs("dir/")
assert Path.toPlatformString(fs("C:/Users/me/"), Path.Windows) ==
assert Path.toString(fs("C:/Users/me/"), platform=Path.Windows) ==
"C:\\Users\\me\\"
assert Path.toPlatformString(
Path.fromPlatformString("C:\\Users/me\\", Path.Windows),
Path.Windows
assert Path.toString(
Path.fromString("C:\\Users/me\\", platform=Path.Windows),
platform=Path.Windows
) ==
"C:\\Users\\me\\"
assert Path.toPlatformString(
Path.fromPlatformString(".\\dir/me\\", Path.Windows),
Path.Posix
assert Path.toString(
Path.fromString(".\\dir/me\\", platform=Path.Windows),
platform=Path.Posix
) ==
"./dir/me/"

Expand Down
30 changes: 14 additions & 16 deletions compiler/test/stdlib/priorityqueue.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ let lotsOfVals = [>
let numVals = Array.length(lotsOfVals)

let sortedVals = Array.copy(lotsOfVals)
Array.sort(compare, sortedVals)
Array.sort(sortedVals)

let mut pq = PriorityQueue.make((a, b) => a - b)
let mut maxPq = PriorityQueue.make((a, b) => b - a)
let mut pq = PriorityQueue.make()
let mut maxPq = PriorityQueue.make(compare=(a, b) => b - a)

assert PriorityQueue.size(pq) == 0
assert PriorityQueue.isEmpty(pq)
Expand Down Expand Up @@ -72,17 +72,15 @@ assert PriorityQueue.size(pq) == 0
assert PriorityQueue.peek(pq) == None

let sortedList = Array.toList(sortedVals)
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
) ==
assert PriorityQueue.drain(PriorityQueue.fromList(Array.toList(lotsOfVals))) ==
sortedList
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
PriorityQueue.fromList(Array.toList(lotsOfVals), compare=(a, b) => b - a)
) ==
List.reverse(sortedList)

assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare) ==
PriorityQueue.fromArray(lotsOfVals, compare)
assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare=compare) ==
PriorityQueue.fromArray(lotsOfVals)

module Immutable {
from PriorityQueue use { module Immutable as PriorityQueue }
Expand Down Expand Up @@ -113,10 +111,10 @@ module Immutable {
let numVals = Array.length(lotsOfVals)

let sortedVals = Array.copy(lotsOfVals)
Array.sort(compare, sortedVals)
Array.sort(sortedVals)

let mut pq = PriorityQueue.make((a, b) => a - b)
let mut maxPq = PriorityQueue.make((a, b) => b - a)
let mut pq = PriorityQueue.make()
let mut maxPq = PriorityQueue.make(compare=(a, b) => b - a)

assert PriorityQueue.size(pq) == 0
assert PriorityQueue.isEmpty(pq)
Expand Down Expand Up @@ -166,14 +164,14 @@ module Immutable {
assert PriorityQueue.drain(pqWithAll) == sortedList
assert PriorityQueue.drain(maxPqWithAll) == List.reverse(sortedList)
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
PriorityQueue.fromList(Array.toList(lotsOfVals))
) ==
sortedList
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
PriorityQueue.fromList(Array.toList(lotsOfVals), compare=(a, b) => b - a)
) ==
List.reverse(sortedList)

assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare) ==
PriorityQueue.fromArray(lotsOfVals, compare)
assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare=compare) ==
PriorityQueue.fromArray(lotsOfVals)
}
16 changes: 8 additions & 8 deletions compiler/test/stdlib/queue.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ assert Queue.size(queue) == 0
assert Queue.peek(queue) == None

// test that expansion works
let queue = Queue.makeSized(3)
let queue = Queue.make(size=3)
Queue.push(0, queue)
Queue.push(1, queue)
Queue.push(2, queue)
Expand All @@ -47,14 +47,14 @@ assert Queue.pop(queue) == Some(1)
assert Queue.pop(queue) == Some(2)
assert Queue.pop(queue) == Some(3)
assert Queue.pop(queue) == None
let queue = Queue.makeSized(0)
let queue = Queue.make(size=0)
Queue.push(0, queue)
let queue2 = Queue.makeSized(1)
let queue2 = Queue.make(size=1)
Queue.push(0, queue2)
assert queue == queue2

// Queue.toList
let queue = Queue.makeSized(3)
let queue = Queue.make(size=3)
assert Queue.toList(queue) == []
Queue.push(0, queue)
Queue.push(1, queue)
Expand All @@ -71,7 +71,7 @@ Queue.push(5, queue)
assert Queue.toList(queue) == [2, 3, 3, 4, 5]

// Queue.fromList
let queue = Queue.makeSized(0)
let queue = Queue.make(size=0)
assert Queue.fromList([]) == queue
Queue.push(0, queue)
Queue.push(1, queue)
Expand All @@ -80,7 +80,7 @@ Queue.push(3, queue)
assert Queue.fromList([0, 1, 2, 3]) == queue

// test that the "circular" behavior of the circular queue works as expected
let queue = Queue.makeSized(4)
let queue = Queue.make(size=4)
let push = x => () => Queue.push(x, queue)
let pop = () => ignore(Queue.pop(queue))
let actions = [
Expand Down Expand Up @@ -153,13 +153,13 @@ Queue.pop(queue4)
assert Queue.toArray(queue4) == [> 2, 3]

// Queue.fromArray
let qa1 = Queue.makeSized(8)
let qa1 = Queue.make(size=8)
Queue.push(1, qa1)
Queue.push(2, qa1)
Queue.push(3, qa1)
Queue.push(4, qa1)
assert Queue.fromArray([> 1, 2, 3, 4]) == qa1
assert Queue.fromArray([>]) == Queue.makeSized(16)
assert Queue.fromArray([>]) == Queue.make(size=16)

// Queue.equal
from Queue use { (==) }
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/stdlib/set.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ assert Array.contains(4, r)

// Resizes the map when it grows

let resize = Set.makeSized(1)
let resize = Set.make(size=1)

assert Set.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }

Expand Down
6 changes: 3 additions & 3 deletions compiler/test/stdlib/stack.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Stack.clear(stack)
assert Stack.size(stack) == 0
assert Stack.peek(stack) == None

let stack = Stack.makeSized(4)
let stack = Stack.make(size=4)

Stack.push(1, stack)
Stack.push(2, stack)
Expand All @@ -50,9 +50,9 @@ assert Stack.pop(stack) == Some(2)
assert Stack.pop(stack) == Some(1)
assert Stack.pop(stack) == None

let stack = Stack.makeSized(0)
let stack = Stack.make(size=0)
Stack.push(0, stack)
let stack2 = Stack.makeSized(1)
let stack2 = Stack.make(size=1)
Stack.push(0, stack2)
assert stack == stack2

Expand Down
Loading

0 comments on commit 3e4b120

Please sign in to comment.