diff --git a/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot b/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot index e1b67d7b26..977ab98929 100644 --- a/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot +++ b/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot @@ -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) @@ -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) @@ -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) ) diff --git a/compiler/test/stdlib/array.test.gr b/compiler/test/stdlib/array.test.gr index b0177050ef..ebfa22d022 100644 --- a/compiler/test/stdlib/array.test.gr +++ b/compiler/test/stdlib/array.test.gr @@ -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) => { @@ -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 @@ -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) => { @@ -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 diff --git a/compiler/test/stdlib/list.test.gr b/compiler/test/stdlib/list.test.gr index 63805f0011..faf68504a5 100644 --- a/compiler/test/stdlib/list.test.gr +++ b/compiler/test/stdlib/list.test.gr @@ -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, @@ -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"] diff --git a/compiler/test/stdlib/map.test.gr b/compiler/test/stdlib/map.test.gr index 699f8c7732..76233e1336 100644 --- a/compiler/test/stdlib/map.test.gr +++ b/compiler/test/stdlib/map.test.gr @@ -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 } diff --git a/compiler/test/stdlib/path.test.gr b/compiler/test/stdlib/path.test.gr index 33ea7d5b8e..e38d55d0be 100644 --- a/compiler/test/stdlib/path.test.gr +++ b/compiler/test/stdlib/path.test.gr @@ -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/" diff --git a/compiler/test/stdlib/priorityqueue.test.gr b/compiler/test/stdlib/priorityqueue.test.gr index 469924ad22..61d8ccb9d6 100644 --- a/compiler/test/stdlib/priorityqueue.test.gr +++ b/compiler/test/stdlib/priorityqueue.test.gr @@ -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) @@ -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 } @@ -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) @@ -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) } diff --git a/compiler/test/stdlib/queue.test.gr b/compiler/test/stdlib/queue.test.gr index b7a43d68c0..e4933106c8 100644 --- a/compiler/test/stdlib/queue.test.gr +++ b/compiler/test/stdlib/queue.test.gr @@ -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) @@ -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) @@ -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) @@ -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 = [ @@ -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 { (==) } diff --git a/compiler/test/stdlib/set.test.gr b/compiler/test/stdlib/set.test.gr index 11e371e552..afd97a14b5 100644 --- a/compiler/test/stdlib/set.test.gr +++ b/compiler/test/stdlib/set.test.gr @@ -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 } diff --git a/compiler/test/stdlib/stack.test.gr b/compiler/test/stdlib/stack.test.gr index 57f460d26b..3df7a38d65 100644 --- a/compiler/test/stdlib/stack.test.gr +++ b/compiler/test/stdlib/stack.test.gr @@ -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) @@ -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 diff --git a/compiler/test/stdlib/string.test.gr b/compiler/test/stdlib/string.test.gr index 6bdb54e922..f32a60969f 100644 --- a/compiler/test/stdlib/string.test.gr +++ b/compiler/test/stdlib/string.test.gr @@ -282,15 +282,15 @@ assert processBytes(String.encode("𐐷", String.UTF32_LE)) == [> 0x37us, 0x04us assert processBytes(String.encodeAt("𐐷", String.UTF16_BE, Bytes.make(12), 5)) == [> 0x00us, 0x00us, 0x00us, 0x00us, 0x00us, 0xD8us, 0x01us, 0xDCus, 0x37us, 0x00us, 0x00us, 0x00us] // test that BOM is prepended: // formatter-ignore -assert processBytes(String.encodeWithBom("𐐷", String.UTF8)) == [> 0xEFus, 0xBBus, 0xBFus, 0xF0us, 0x90us, 0x90us, 0xB7us] +assert processBytes(String.encode("𐐷", String.UTF8, includeBom=true)) == [> 0xEFus, 0xBBus, 0xBFus, 0xF0us, 0x90us, 0x90us, 0xB7us] // formatter-ignore -assert processBytes(String.encodeWithBom("𐐷", String.UTF16_BE)) == [> 0xFEus, 0xFFus, 0xD8us, 0x01us, 0xDCus, 0x37us] +assert processBytes(String.encode("𐐷", String.UTF16_BE, includeBom=true)) == [> 0xFEus, 0xFFus, 0xD8us, 0x01us, 0xDCus, 0x37us] // formatter-ignore -assert processBytes(String.encodeWithBom("𐐷", String.UTF16_LE)) == [> 0xFFus, 0xFEus, 0x01us, 0xD8us, 0x37us, 0xDCus] +assert processBytes(String.encode("𐐷", String.UTF16_LE, includeBom=true)) == [> 0xFFus, 0xFEus, 0x01us, 0xD8us, 0x37us, 0xDCus] // formatter-ignore -assert processBytes(String.encodeWithBom("𐐷", String.UTF32_BE)) == [> 0x00us, 0x00us, 0xFEus, 0xFFus, 0x00us, 0x01us, 0x04us, 0x37us] +assert processBytes(String.encode("𐐷", String.UTF32_BE, includeBom=true)) == [> 0x00us, 0x00us, 0xFEus, 0xFFus, 0x00us, 0x01us, 0x04us, 0x37us] // formatter-ignore -assert processBytes(String.encodeWithBom("𐐷", String.UTF32_LE)) == [> 0xFFus, 0xFEus, 0x00us, 0x00us, 0x37us, 0x04us, 0x01us, 0x00us] +assert processBytes(String.encode("𐐷", String.UTF32_LE, includeBom=true)) == [> 0xFFus, 0xFEus, 0x00us, 0x00us, 0x37us, 0x04us, 0x01us, 0x00us] // formatter-ignore assert processBytes(String.encode(emojis, String.UTF16_BE)) == [> 0x00us, 0x77us, 0x00us, 0x65us, 0x00us, 0x20us, 0x00us, 0x66us, 0x00us, 0x6fus, 0x00us, 0x75us, 0x00us, 0x6eus, 0x00us, 0x64us, 0x00us, 0x20us, 0x00us, 0x61us, 0x00us, 0x20us, 0x00us, 0x70us, 0x00us, 0x61us, 0x00us, 0x74us, 0x00us, 0x68us, 0xd8us, 0x3dus, 0xdeus, 0x80us, 0x00us, 0x20us, 0x00us, 0x74us, 0x00us, 0x6fus, 0x00us, 0x20us, 0x00us, 0x67us, 0x00us, 0x72us, 0x00us, 0x65us, 0x00us, 0x61us, 0x00us, 0x74us, 0x00us, 0x6eus, 0x00us, 0x65us, 0x00us, 0x73us, 0x00us, 0x73us, 0xd8us, 0x3cus, 0xdfus, 0xc5us, 0x00us, 0x62us, 0x00us, 0x75us, 0x00us, 0x74us, 0x00us, 0x20us, 0x00us, 0x74us, 0x00us, 0x68us, 0x00us, 0x65us, 0x00us, 0x20us, 0x00us, 0x67us, 0x00us, 0x72us, 0x00us, 0x69us, 0x00us, 0x6eus, 0x00us, 0x64us, 0x00us, 0x20us, 0x00us, 0x6eus, 0x00us, 0x65us, 0x00us, 0x76us, 0x00us, 0x65us, 0x00us, 0x72us, 0x00us, 0x20us, 0x00us, 0x71us, 0x00us, 0x75us, 0x00us, 0x69us, 0x00us, 0x74us, 0x00us, 0x73us, 0xd8us, 0x3dus, 0xdeus, 0x24us, 0xd8us, 0x3dus, 0xdcus, 0xaaus, 0xd8us, 0x3cus, 0xdfus, 0xfcus, 0x00us, 0x20us, 0x00us, 0x6bus, 0x00us, 0x65us, 0x00us, 0x65us, 0x00us, 0x70us, 0x00us, 0x20us, 0x00us, 0x6dus, 0x00us, 0x69us, 0x00us, 0x6cus, 0x00us, 0x6cus, 0x00us, 0x69us, 0x00us, 0x6eus, 0x00us, 0x67us, 0xd8us, 0x3dus, 0xdcus, 0xafus, 0xd8us, 0x3dus, 0xddus, 0x25us, 0xd8us, 0x3dus, 0xdeus, 0x08us] @@ -362,80 +362,116 @@ assert String.decodeRange( emojis // keepBom should be no-op, since there is no BOM -assert String.decodeRangeKeepBom( +assert String.decodeRange( String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51), String.UTF8, 51, - 98 + 98, + keepBom=true ) == emojis -assert String.decodeRangeKeepBom( +assert String.decodeRange( String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51), String.UTF16_LE, 51, - 164 + 164, + keepBom=true ) == emojis -assert String.decodeRangeKeepBom( +assert String.decodeRange( String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51), String.UTF16_BE, 51, - 164 + 164, + keepBom=true ) == emojis -assert String.decodeRangeKeepBom( +assert String.decodeRange( String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51), String.UTF32_LE, 51, - 296 + 296, + keepBom=true ) == emojis -assert String.decodeRangeKeepBom( +assert String.decodeRange( String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51), String.UTF32_BE, 51, - 296 + 296, + keepBom=true ) == emojis // but, when we include it, it should preserve it: -assert String.decodeRangeKeepBom( - String.encodeAtWithBom(emojis, String.UTF16_LE, Bytes.make(500), 51), +assert String.decodeRange( + String.encodeAt( + emojis, + String.UTF16_LE, + Bytes.make(500), + 51, + includeBom=true + ), String.UTF16_LE, 51, - 166 + 166, + keepBom=true ) != emojis // BOM-skipping should be default: assert String.decodeRange( - String.encodeAtWithBom(emojis, String.UTF8, Bytes.make(500), 51), + String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51, includeBom=true), String.UTF8, 51, 101 ) == emojis assert String.decodeRange( - String.encodeAtWithBom(emojis, String.UTF16_LE, Bytes.make(500), 51), + String.encodeAt( + emojis, + String.UTF16_LE, + Bytes.make(500), + 51, + includeBom=true + ), String.UTF16_LE, 51, 166 ) == emojis assert String.decodeRange( - String.encodeAtWithBom(emojis, String.UTF16_BE, Bytes.make(500), 51), + String.encodeAt( + emojis, + String.UTF16_BE, + Bytes.make(500), + 51, + includeBom=true + ), String.UTF16_BE, 51, 166 ) == emojis assert String.decodeRange( - String.encodeAtWithBom(emojis, String.UTF32_BE, Bytes.make(500), 51), + String.encodeAt( + emojis, + String.UTF32_BE, + Bytes.make(500), + 51, + includeBom=true + ), String.UTF32_BE, 51, 300 ) == emojis assert String.decodeRange( - String.encodeAtWithBom(emojis, String.UTF32_LE, Bytes.make(500), 51), + String.encodeAt( + emojis, + String.UTF32_LE, + Bytes.make(500), + 51, + includeBom=true + ), String.UTF32_LE, 51, 300 diff --git a/stdlib/array.gr b/stdlib/array.gr index b717208cf4..390ccb2e11 100644 --- a/stdlib/array.gr +++ b/stdlib/array.gr @@ -1033,22 +1033,23 @@ provide let slice = (start, end=length(array), array) => { * Sorts an array in-place. * * Ordering is calculated using a comparator function which takes two array elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller. - * @param comp: The comparator function used to indicate sort order + * @param compare: The comparator function used to indicate sort order * @param array: The array to be sorted * * @example * let arr = [> 3, 2, 4, 1] - * Array.sort((a, b) => a - b, arr) + * Array.sort(compare=(a, b) => a - b, arr) * assert arr == [> 1, 2, 3, 4] * * @since v0.4.5 + * @history v0.6.0: Made `compare` a default argument */ -provide let sort = (comp, array) => { +provide let sort = (compare=compare, array) => { let partition = (low, high) => { let pivot = array[high] let mut i = low - 1 for (let mut j = low; j < high; j += 1) { - if (comp(array[j], pivot) < 0) { + if (compare(array[j], pivot) < 0) { i += 1 let temp = array[i] array[i] = array[j] @@ -2322,21 +2323,21 @@ provide module Immutable { * Sorts the given array based on a given comparator function. * * Ordering is calculated using a comparator function which takes two array elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller. - * @param comp: The comparator function used to indicate sort order + * @param compare: The comparator function used to indicate sort order * @param array: The array to be sorted * @returns The sorted array * * @example * from Array use { module Immutable } * let arr = Immutable.fromList([2, 3, 1, 4]) - * assert Immutable.sort((a, b) => a - b, arr) == Immutable.fromList([1, 2, 3, 4]) + * assert Immutable.sort(compare=(a, b) => a - b, arr) == Immutable.fromList([1, 2, 3, 4]) * * @since v0.6.0 - * @history v0.5.4: Originally in `"immutablearray"` module + * @history v0.5.4: Originally in `"immutablearray"` module, with `compare` being a required argument */ - provide let sort = (comp, array) => { - fromList(List.sort(comp, toList(array))) + provide let sort = (compare=compare, array) => { + fromList(List.sort(compare=compare, toList(array))) } /** diff --git a/stdlib/array.md b/stdlib/array.md index 487f37338c..edac5b574b 100644 --- a/stdlib/array.md +++ b/stdlib/array.md @@ -1466,13 +1466,20 @@ Array.slice(1, [> 1, 2, 3, 4]) == [> 2, 3, 4] ### Array.**sort** -
-Added in 0.4.5 -No other changes yet. +
+Added in 0.4.5 + + + + + + + +
versionchanges
nextMade `compare` a default argument
```grain -sort : (comp: ((a, a) => Number), array: Array) => Void +sort : (?compare: ((num1: a, num2: a) => Number), array: Array) => Void ``` Sorts an array in-place. @@ -1483,14 +1490,14 @@ Parameters: |param|type|description| |-----|----|-----------| -|`comp`|`(a, a) => Number`|The comparator function used to indicate sort order| +|`?compare`|`(num1: a, num2: a) => Number`|The comparator function used to indicate sort order| |`array`|`Array`|The array to be sorted| Examples: ```grain let arr = [> 3, 2, 4, 1] -Array.sort((a, b) => a - b, arr) +Array.sort(compare=(a, b) => a - b, arr) assert arr == [> 1, 2, 3, 4] ``` @@ -3063,14 +3070,15 @@ assert Immutable.slice(1, end=-1, arr) == Immutable.fromList(['b']) versionchanges -0.5.4Originally in `"immutablearray"` module +0.5.4Originally in `"immutablearray"` module, with `compare` being a required argument
```grain sort : - (comp: ((a, a) => Number), array: ImmutableArray) => ImmutableArray + (?compare: ((num1: a, num2: a) => Number), array: ImmutableArray) => + ImmutableArray ``` Sorts the given array based on a given comparator function. @@ -3081,7 +3089,7 @@ Parameters: |param|type|description| |-----|----|-----------| -|`comp`|`(a, a) => Number`|The comparator function used to indicate sort order| +|`?compare`|`(num1: a, num2: a) => Number`|The comparator function used to indicate sort order| |`array`|`ImmutableArray`|The array to be sorted| Returns: @@ -3095,7 +3103,7 @@ Examples: ```grain from Array use { module Immutable } let arr = Immutable.fromList([2, 3, 1, 4]) -assert Immutable.sort((a, b) => a - b, arr) == Immutable.fromList([1, 2, 3, 4]) +assert Immutable.sort(compare=(a, b) => a - b, arr) == Immutable.fromList([1, 2, 3, 4]) ``` #### Array.Immutable.**rotate** diff --git a/stdlib/list.gr b/stdlib/list.gr index 31e7d230c5..96b42ccf88 100644 --- a/stdlib/list.gr +++ b/stdlib/list.gr @@ -880,13 +880,14 @@ provide let rec revAppend = (list1, list2) => { * Sorts the given list based on a given comparator function. The resulting list is sorted in increasing order. * * Ordering is calculated using a comparator function which takes two list elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller. - * @param comp: The comparator function used to indicate sort order + * @param compare: The comparator function used to indicate sort order * @param list: The list to be sorted * @returns The sorted list * * @since v0.4.5 + * @history v0.6.0: Made `compare` a default argument */ -provide let sort = (comp, list) => { +provide let sort = (compare=compare, list) => { let rec merge = (left, right, list) => { match ((left, right)) { (_, []) => { @@ -896,7 +897,7 @@ provide let sort = (comp, list) => { revAppend(list, right) }, ([lhd, ...ltl], [rhd, ...rtl]) => { - if (comp(lhd, rhd) < 0) { + if (compare(lhd, rhd) < 0) { merge(ltl, right, append([lhd], list)) } else { merge(left, rtl, append([rhd], list)) diff --git a/stdlib/list.md b/stdlib/list.md index 0a7beb4b1c..da060bc4f2 100644 --- a/stdlib/list.md +++ b/stdlib/list.md @@ -1290,13 +1290,20 @@ Returns: ### List.**sort** -
-Added in 0.4.5 -No other changes yet. +
+Added in 0.4.5 + + + + + + + +
versionchanges
nextMade `compare` a default argument
```grain -sort : (comp: ((a, a) => Number), list: List
) => List +sort : (?compare: ((num1: a, num2: a) => Number), list: List) => List ``` Sorts the given list based on a given comparator function. The resulting list is sorted in increasing order. @@ -1307,7 +1314,7 @@ Parameters: |param|type|description| |-----|----|-----------| -|`comp`|`(a, a) => Number`|The comparator function used to indicate sort order| +|`?compare`|`(num1: a, num2: a) => Number`|The comparator function used to indicate sort order| |`list`|`List`|The list to be sorted| Returns: diff --git a/stdlib/map.gr b/stdlib/map.gr index 7eb54eb0e0..2da720e366 100644 --- a/stdlib/map.gr +++ b/stdlib/map.gr @@ -40,29 +40,25 @@ provide record InternalMapStats { } /** - * Creates a new empty map with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you won't need to care about the storage size of your map and can use `Map.make()` instead. + * Creates a new empty map with an initial storage of the given size. As + * values are added or removed, the internal storage may grow or shrink. + * Generally, you won't need to care about the storage size of your map and + * can use the default size. * * @param size: The initial storage size of the map * @returns An empty map with the given initial storage size * * @since v0.2.0 + * @history v0.6.0: Merged with `makeSized`; modified signature to accept size */ -provide let makeSized = size => { // TODO: This could take an `eq` function to custom comparisons +provide let make = + ( + size=16, + ) => { // TODO: This could take an `eq` function to custom comparisons let buckets = Array.make(size, None) { size: 0, buckets } } -/** - * Creates a new, empty map. - * - * @returns An empty map - * - * @since v0.2.0 - */ -provide let make = () => { - makeSized(16) -} - let getBucketIndex = (key, buckets) => { let bucketsLength = Array.length(buckets) let hashedKey = hash(key) diff --git a/stdlib/map.md b/stdlib/map.md index f9485d9418..e1e6bed141 100644 --- a/stdlib/map.md +++ b/stdlib/map.md @@ -40,24 +40,34 @@ Represents the internal state of a map. Functions and constants included in the Map module. -### Map.**makeSized** +### Map.**make** -
-Added in 0.2.0 -No other changes yet. +
+Added in 0.2.0 + + + + + + + +
versionchanges
nextMerged with `makeSized`; modified signature to accept size
```grain -makeSized : (size: Number) => Map +make : (?size: Number) => Map ``` -Creates a new empty map with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you won't need to care about the storage size of your map and can use `Map.make()` instead. +Creates a new empty map with an initial storage of the given size. As +values are added or removed, the internal storage may grow or shrink. +Generally, you won't need to care about the storage size of your map and +can use the default size. Parameters: |param|type|description| |-----|----|-----------| -|`size`|`Number`|The initial storage size of the map| +|`?size`|`Number`|The initial storage size of the map| Returns: @@ -65,25 +75,6 @@ Returns: |----|-----------| |`Map`|An empty map with the given initial storage size| -### Map.**make** - -
-Added in 0.2.0 -No other changes yet. -
- -```grain -make : () => Map -``` - -Creates a new, empty map. - -Returns: - -|type|description| -|----|-----------| -|`Map`|An empty map| - ### Map.**set**
diff --git a/stdlib/path.gr b/stdlib/path.gr index cdc8728480..d4b7eeeb23 100644 --- a/stdlib/path.gr +++ b/stdlib/path.gr @@ -366,23 +366,6 @@ let fromStringHelper = (pathStr, platform) => { } } -/** - * Parses a path string into a `Path`. Paths will be parsed as file paths - * rather than directory paths if there is ambiguity. - * - * @param pathStr: The string to parse as a path - * @returns The path wrapped with details encoded within the type - * - * @example fromString("/bin/") // an absolute Path referencing the directory /bin/ - * @example fromString("file.txt") // a relative Path referencing the file ./file.txt - * @example fromString(".") // a relative Path referencing the current directory - * - * @since v0.5.5 - */ -provide let fromString = pathStr => { - fromStringHelper(pathStr, Posix) -} - /** * Parses a path string into a `Path` using the path separators appropriate to * the given platform (`/` for `Posix` and either `/` or `\` for `Windows`). @@ -393,12 +376,15 @@ provide let fromString = pathStr => { * @param platform: The platform whose path separators should be used for parsing * @returns The path wrapped with details encoded within the type * - * @example fromPlatformString("/bin/", Posix) // an absolute Path referencing the directory /bin/ - * @example fromPlatformString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt + * @example fromString("file.txt") // a relative Path referencing the file ./file.txt + * @example fromString(".") // a relative Path referencing the current directory + * @example fromString("/bin/", Posix) // an absolute Path referencing the directory /bin/ + * @example fromString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt * * @since v0.5.5 + * @history v0.6.0: Merged with `fromPlatformString`; modified signature to accept platform */ -provide let fromPlatformString = (pathStr, platform) => { +provide let fromString = (pathStr, platform=Posix) => { fromStringHelper(pathStr, platform) } @@ -429,22 +415,6 @@ let toStringHelper = (path, platform) => { List.join(sep, segs) } -/** - * Converts the given `Path` into a string, using the `/` path separator. - * A trailing slash is added to directory paths. - * - * @param path: The path to convert to a string - * @returns A string representing the given path - * - * @example toString(fromString("/file.txt")) == "/file.txt" - * @example toString(fromString("dir/")) == "./dir/" - * - * @since v0.5.5 - */ -provide let toString = path => { - toStringHelper(pathInfo(path), Posix) -} - /** * Converts the given `Path` into a string, using the canonical path separator * appropriate to the given platform (`/` for `Posix` and `\` for `Windows`). @@ -454,12 +424,14 @@ provide let toString = path => { * @param platform: The `Platform` to use to represent the path as a string * @returns A string representing the given path * - * @example toPlatformString(fromString("dir/"), Posix) == "./dir/" - * @example toPlatformString(fromString("C:/file.txt"), Windows) == "C:\\file.txt" + * @example toString(fromString("/file.txt")) == "/file.txt" + * @example toString(fromString("dir/"), Posix) == "./dir/" + * @example toString(fromString("C:/file.txt"), Windows) == "C:\\file.txt" * * @since v0.5.5 + * @history v0.6.0: Merged with `toPlatformString`; modified signature to accept platform */ -provide let toPlatformString = (path, platform) => { +provide let toString = (path, platform=Posix) => { toStringHelper(pathInfo(path), platform) } diff --git a/stdlib/path.md b/stdlib/path.md index a0696e879e..7ccc9eba16 100644 --- a/stdlib/path.md +++ b/stdlib/path.md @@ -171,23 +171,33 @@ Functions and constants included in the Path module. ### Path.**fromString** -
-Added in 0.5.5 -No other changes yet. +
+Added in 0.5.5 + + + + + + + +
versionchanges
nextMerged with `fromPlatformString`; modified signature to accept platform
```grain -fromString : (pathStr: String) => Path +fromString : (pathStr: String, ?platform: Platform) => Path ``` -Parses a path string into a `Path`. Paths will be parsed as file paths -rather than directory paths if there is ambiguity. +Parses a path string into a `Path` using the path separators appropriate to +the given platform (`/` for `Posix` and either `/` or `\` for `Windows`). +Paths will be parsed as file paths rather than directory paths if there is +ambiguity. Parameters: |param|type|description| |-----|----|-----------| |`pathStr`|`String`|The string to parse as a path| +|`?platform`|`Platform`|The platform whose path separators should be used for parsing| Returns: @@ -197,10 +207,6 @@ Returns: Examples: -```grain -fromString("/bin/") // an absolute Path referencing the directory /bin/ -``` - ```grain fromString("file.txt") // a relative Path referencing the file ./file.txt ``` @@ -209,57 +215,34 @@ fromString("file.txt") // a relative Path referencing the file ./file.txt fromString(".") // a relative Path referencing the current directory ``` -### Path.**fromPlatformString** - -
-Added in 0.5.5 -No other changes yet. -
- ```grain -fromPlatformString : (pathStr: String, platform: Platform) => Path +fromString("/bin/", Posix) // an absolute Path referencing the directory /bin/ ``` -Parses a path string into a `Path` using the path separators appropriate to -the given platform (`/` for `Posix` and either `/` or `\` for `Windows`). -Paths will be parsed as file paths rather than directory paths if there is -ambiguity. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`pathStr`|`String`|The string to parse as a path| -|`platform`|`Platform`|The platform whose path separators should be used for parsing| - -Returns: - -|type|description| -|----|-----------| -|`Path`|The path wrapped with details encoded within the type| - -Examples: - ```grain -fromPlatformString("/bin/", Posix) // an absolute Path referencing the directory /bin/ -``` - -```grain -fromPlatformString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt +fromString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt ``` ### Path.**toString** -
-Added in 0.5.5 -No other changes yet. +
+Added in 0.5.5 + + + + + + + +
versionchanges
nextMerged with `toPlatformString`; modified signature to accept platform
```grain -toString : (path: Path) => String +toString : (path: Path, ?platform: Platform) => String ``` -Converts the given `Path` into a string, using the `/` path separator. +Converts the given `Path` into a string, using the canonical path separator +appropriate to the given platform (`/` for `Posix` and `\` for `Windows`). A trailing slash is added to directory paths. Parameters: @@ -267,6 +250,7 @@ Parameters: |param|type|description| |-----|----|-----------| |`path`|`Path`|The path to convert to a string| +|`?platform`|`Platform`|The `Platform` to use to represent the path as a string| Returns: @@ -281,45 +265,11 @@ toString(fromString("/file.txt")) == "/file.txt" ``` ```grain -toString(fromString("dir/")) == "./dir/" -``` - -### Path.**toPlatformString** - -
-Added in 0.5.5 -No other changes yet. -
- -```grain -toPlatformString : (path: Path, platform: Platform) => String -``` - -Converts the given `Path` into a string, using the canonical path separator -appropriate to the given platform (`/` for `Posix` and `\` for `Windows`). -A trailing slash is added to directory paths. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`path`|`Path`|The path to convert to a string| -|`platform`|`Platform`|The `Platform` to use to represent the path as a string| - -Returns: - -|type|description| -|----|-----------| -|`String`|A string representing the given path| - -Examples: - -```grain -toPlatformString(fromString("dir/"), Posix) == "./dir/" +toString(fromString("dir/"), Posix) == "./dir/" ``` ```grain -toPlatformString(fromString("C:/file.txt"), Windows) == "C:\\file.txt" +toString(fromString("C:/file.txt"), Windows) == "C:\\file.txt" ``` ### Path.**isDirectory** diff --git a/stdlib/priorityqueue.gr b/stdlib/priorityqueue.gr index 646224003f..f3cae2ee1e 100644 --- a/stdlib/priorityqueue.gr +++ b/stdlib/priorityqueue.gr @@ -76,34 +76,21 @@ let rec siftUp = (i, pq) => { * negative number if the first has less priority. * * Generally, you won't need to care about the storage size of your priority - * queue and can use `PriorityQueue.make()` instead. + * queue and can use the default size. * + * @param compare: The comparator function used to indicate priority order * @param size: The initial storage size of the priority queue - * @param comp: The comparator function used to indicate priority order * @returns An empty priority queue * * @since v0.5.3 - */ -provide let makeSized = (size, comp) => { - { size: 0, array: Array.make(size, None), comp } -} - -/** - * Creates a new priority queue with a comparator function, which is used to - * determine priority of elements. The comparator function takes two elements - * and must return 0 if both share priority, a positive number if the first - * has greater priority, and a negative number if the first has less priority. - * - * @param comp: The comparator function used to indicate priority order - * @returns An empty priority queue + * @history v0.6.0: Merged with `makeSized`; modified signature to accept size * - * @example PriorityQueue.make(compare) // creates a min priority queue of numbers using the compare pervasive + * @example PriorityQueue.make() // creates a min priority queue of numbers using the compare pervasive + * @example PriorityQueue.make(compare=compare, size=32) // creates a min priority queue of numbers using the compare pervasive and an initial size of 32 * @example PriorityQueue.make((a, b) => String.length(b) - String.length(a)) // creates a priority queue by string length (longest to shortest) - * - * @since v0.5.3 */ -provide let make = comp => { - makeSized(16, comp) +provide let make = (compare=compare, size=16) => { + { size: 0, array: Array.make(size, None), comp: compare } } /** @@ -220,15 +207,16 @@ provide let drain = pq => { * and a negative number if the first has less priority. * * @param array: An array of values used to initialize the priority queue - * @param comp: A comparator function used to assign priority to elements + * @param compare: A comparator function used to assign priority to elements * @returns A priority queue containing the elements from the array * * @since v0.5.4 + * @history v0.6.0: Made `compare` a default argument */ -provide let fromArray = (array, comp) => { +provide let fromArray = (array, compare=compare) => { let size = Array.length(array) let array = Array.map(x => Some(x), array) - let heap = { size, array, comp } + let heap = { size, array, comp: compare } for (let mut i = size - 1; i >= 0; i -= 1) { siftDown(i, heap) } @@ -243,14 +231,15 @@ provide let fromArray = (array, comp) => { * and a negative number if the first has less priority. * * @param list: A list of values used to initialize the priority queue - * @param comp: A comparator function used to assign priority to elements + * @param compare: A comparator function used to assign priority to elements * @returns A priority queue containing the elements from the list * * @since v0.5.3 + * @history v0.6.0: Made `compare` a default argument */ -provide let fromList = (list, comp) => { +provide let fromList = (list, compare=compare) => { let array = Array.fromList(list) - fromArray(array, comp) + fromArray(array, compare=compare) } /** @@ -306,18 +295,18 @@ provide module Immutable { * and must return 0 if both share priority, a positive number if the first * has greater priority, and a negative number if the first has less priority. * - * @param comp: The comparator function used to indicate priority order + * @param compare: The comparator function used to indicate priority order * @returns An empty priority queue * * @example PriorityQueue.Immutable.make(compare) // creates a min priority queue of numbers using the compare pervasive * @example PriorityQueue.Immutable.make((a, b) => String.length(b) - String.length(a)) // creates a priority queue by string length (longest to shortest) * * @since v0.6.0 - * @history v0.5.3: Originally in `"immutablepriorityqueue"` module + * @history v0.5.3: Originally in `"immutablepriorityqueue"` module with `compare` being a required argument */ - provide let make = comp => { - { comp, size: 0, root: None } + provide let make = (compare=compare) => { + { comp: compare, size: 0, root: None } } /** @@ -606,15 +595,15 @@ provide module Immutable { * and a negative number if the first has less priority. * * @param list: A list of values used to initialize the priority queue - * @param comp: A comparator function used to assign priority to elements + * @param compare: A comparator function used to assign priority to elements * @returns A priority queue containing the elements from the list * * @since v0.6.0 - * @history v0.5.3: Originally in `"immutablepriorityqueue"` module + * @history v0.5.3: Originally in `"immutablepriorityqueue"` module with `compare` being a required argument */ - provide let fromList = (list, comp) => { - List.reduce((pq, val) => push(val, pq), make(comp), list) + provide let fromList = (list, compare=compare) => { + List.reduce((pq, val) => push(val, pq), make(compare=compare), list) } /** @@ -625,14 +614,14 @@ provide module Immutable { * and a negative number if the first has less priority. * * @param array: An array of values used to initialize the priority queue - * @param comp: A comparator function used to assign priority to elements + * @param compare: A comparator function used to assign priority to elements * @returns A priority queue containing the elements from the array * * @since v0.6.0 - * @history v0.5.4: Originally in `"immutablepriorityqueue"` module + * @history v0.5.4: Originally in `"immutablepriorityqueue"` module with `compare` being a required argument */ - provide let fromArray = (array, comp) => { - Array.reduce((pq, val) => push(val, pq), make(comp), array) + provide let fromArray = (array, compare=compare) => { + Array.reduce((pq, val) => push(val, pq), make(compare=compare), array) } } diff --git a/stdlib/priorityqueue.md b/stdlib/priorityqueue.md index 1b2121345a..8d7febb18b 100644 --- a/stdlib/priorityqueue.md +++ b/stdlib/priorityqueue.md @@ -31,15 +31,22 @@ Mutable data structure which maintains a priority order for its elements. Functions and constants included in the PriorityQueue module. -### PriorityQueue.**makeSized** +### PriorityQueue.**make** -
-Added in 0.5.3 -No other changes yet. +
+Added in 0.5.3 + + + + + + + +
versionchanges
nextMerged with `makeSized`; modified signature to accept size
```grain -makeSized : (size: Number, comp: ((a, a) => Number)) => PriorityQueue
+make : (?compare: ((a, a) => Number), ?size: Number) => PriorityQueue ``` Creates a new priority queue with a given internal storage size and a @@ -49,14 +56,14 @@ priority, a positive number if the first has greater priority, and a negative number if the first has less priority. Generally, you won't need to care about the storage size of your priority -queue and can use `PriorityQueue.make()` instead. +queue and can use the default size. Parameters: |param|type|description| |-----|----|-----------| -|`size`|`Number`|The initial storage size of the priority queue| -|`comp`|`(a, a) => Number`|The comparator function used to indicate priority order| +|`?compare`|`(a, a) => Number`|The comparator function used to indicate priority order| +|`?size`|`Number`|The initial storage size of the priority queue| Returns: @@ -64,38 +71,14 @@ Returns: |----|-----------| |`PriorityQueue`|An empty priority queue| -### PriorityQueue.**make** - -
-Added in 0.5.3 -No other changes yet. -
+Examples: ```grain -make : (comp: ((a, a) => Number)) => PriorityQueue
+PriorityQueue.make() // creates a min priority queue of numbers using the compare pervasive ``` -Creates a new priority queue with a comparator function, which is used to -determine priority of elements. The comparator function takes two elements -and must return 0 if both share priority, a positive number if the first -has greater priority, and a negative number if the first has less priority. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`comp`|`(a, a) => Number`|The comparator function used to indicate priority order| - -Returns: - -|type|description| -|----|-----------| -|`PriorityQueue`|An empty priority queue| - -Examples: - ```grain -PriorityQueue.make(compare) // creates a min priority queue of numbers using the compare pervasive +PriorityQueue.make(compare=compare, size=32) // creates a min priority queue of numbers using the compare pervasive and an initial size of 32 ``` ```grain @@ -251,13 +234,21 @@ Returns: ### PriorityQueue.**fromArray** -
-Added in 0.5.4 -No other changes yet. +
+Added in 0.5.4 + + + + + + + +
versionchanges
nextMade `compare` a default argument
```grain -fromArray : (array: Array
, comp: ((a, a) => Number)) => PriorityQueue +fromArray : + (array: Array, ?compare: ((a, a) => Number)) => PriorityQueue ``` Constructs a new priority queue initialized with the elements in the array @@ -271,7 +262,7 @@ Parameters: |param|type|description| |-----|----|-----------| |`array`|`Array`|An array of values used to initialize the priority queue| -|`comp`|`(a, a) => Number`|A comparator function used to assign priority to elements| +|`?compare`|`(a, a) => Number`|A comparator function used to assign priority to elements| Returns: @@ -281,13 +272,20 @@ Returns: ### PriorityQueue.**fromList** -
-Added in 0.5.3 -No other changes yet. +
+Added in 0.5.3 + + + + + + + +
versionchanges
nextMade `compare` a default argument
```grain -fromList : (list: List
, comp: ((a, a) => Number)) => PriorityQueue +fromList : (list: List, ?compare: ((a, a) => Number)) => PriorityQueue ``` Constructs a new priority queue initialized with the elements in the list @@ -301,7 +299,7 @@ Parameters: |param|type|description| |-----|----|-----------| |`list`|`List`|A list of values used to initialize the priority queue| -|`comp`|`(a, a) => Number`|A comparator function used to assign priority to elements| +|`?compare`|`(a, a) => Number`|A comparator function used to assign priority to elements| Returns: @@ -370,13 +368,13 @@ An empty priority queue with the default `compare` comparator. versionchanges -0.5.3Originally in `"immutablepriorityqueue"` module +0.5.3Originally in `"immutablepriorityqueue"` module with `compare` being a required argument
```grain -make : (comp: ((a, a) => Number)) => PriorityQueue +make : (?compare: ((a, a) => Number)) => PriorityQueue ``` Creates a new priority queue with a comparator function, which is used to @@ -388,7 +386,7 @@ Parameters: |param|type|description| |-----|----|-----------| -|`comp`|`(a, a) => Number`|The comparator function used to indicate priority order| +|`?compare`|`(a, a) => Number`|The comparator function used to indicate priority order| Returns: @@ -611,13 +609,13 @@ Returns: versionchanges -0.5.3Originally in `"immutablepriorityqueue"` module +0.5.3Originally in `"immutablepriorityqueue"` module with `compare` being a required argument
```grain -fromList : (list: List, comp: ((a, a) => Number)) => PriorityQueue +fromList : (list: List, ?compare: ((a, a) => Number)) => PriorityQueue ``` Constructs a new priority queue initialized with the elements in the list @@ -631,7 +629,7 @@ Parameters: |param|type|description| |-----|----|-----------| |`list`|`List`|A list of values used to initialize the priority queue| -|`comp`|`(a, a) => Number`|A comparator function used to assign priority to elements| +|`?compare`|`(a, a) => Number`|A comparator function used to assign priority to elements| Returns: @@ -648,13 +646,14 @@ Returns: versionchanges -0.5.4Originally in `"immutablepriorityqueue"` module +0.5.4Originally in `"immutablepriorityqueue"` module with `compare` being a required argument
```grain -fromArray : (array: Array, comp: ((a, a) => Number)) => PriorityQueue +fromArray : + (array: Array, ?compare: ((a, a) => Number)) => PriorityQueue ``` Constructs a new priority queue initialized with the elements in the array @@ -668,7 +667,7 @@ Parameters: |param|type|description| |-----|----|-----------| |`array`|`Array`|An array of values used to initialize the priority queue| -|`comp`|`(a, a) => Number`|A comparator function used to assign priority to elements| +|`?compare`|`(a, a) => Number`|A comparator function used to assign priority to elements| Returns: diff --git a/stdlib/queue.gr b/stdlib/queue.gr index eaf16366f3..cca825c04b 100644 --- a/stdlib/queue.gr +++ b/stdlib/queue.gr @@ -28,29 +28,18 @@ abstract record Queue { /** * Creates a new queue with an initial storage of the given size. As values are * added or removed, the internal storage may grow or shrink. Generally, you - * won’t need to care about the storage size of your map and can use - * `Queue.make()` instead. + * won’t need to care about the storage size of your map and can use the + * default size. * * @param size: The initial storage size of the queue * @returns An empty queue * * @since v0.6.0 */ -provide let makeSized = size => { +provide let make = (size=16) => { { size: 0, array: Array.make(size, None), headIndex: 0, tailIndex: 0 } } -/** - * Creates a new queue. - * - * @returns An empty queue - * - * @since v0.6.0 - */ -provide let make = () => { - makeSized(16) -} - /** * Checks if the given queue contains no items. * @@ -165,7 +154,7 @@ provide let toList = queue => { * @since v0.6.0 */ provide let fromList = list => { - let queue = makeSized(List.length(list)) + let queue = make(size=List.length(list)) List.forEach(e => push(e, queue), list) queue } diff --git a/stdlib/queue.md b/stdlib/queue.md index cf7135f50c..a2707b2b5e 100644 --- a/stdlib/queue.md +++ b/stdlib/queue.md @@ -33,7 +33,7 @@ A mutable FIFO (first-in-first-out) data structure. Functions and constants included in the Queue module. -### Queue.**makeSized** +### Queue.**make**
Added in next @@ -41,38 +41,19 @@ No other changes yet.
```grain -makeSized : (size: Number) => Queue
+make : (?size: Number) => Queue ``` Creates a new queue with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you -won’t need to care about the storage size of your map and can use -`Queue.make()` instead. +won’t need to care about the storage size of your map and can use the +default size. Parameters: |param|type|description| |-----|----|-----------| -|`size`|`Number`|The initial storage size of the queue| - -Returns: - -|type|description| -|----|-----------| -|`Queue`|An empty queue| - -### Queue.**make** - -
-Added in next -No other changes yet. -
- -```grain -make : () => Queue
-``` - -Creates a new queue. +|`?size`|`Number`|The initial storage size of the queue| Returns: diff --git a/stdlib/set.gr b/stdlib/set.gr index 0d723a519a..435ca4317f 100644 --- a/stdlib/set.gr +++ b/stdlib/set.gr @@ -34,27 +34,21 @@ provide record InternalSetStats { // TODO: This could take an `eq` function to custom comparisons /** - * Creates a new empty set with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you won't need to care about the storage size of your set and can use `Set.make()` instead. + * Creates a new empty set with an initial storage of the given size. As + * values are added or removed, the internal storage may grow or shrink. + * Generally, you won't need to care about the storage size of your set and + * can use the default size. * * @param size: The initial storage size of the set * @returns An empty set with the given initial storage size * * @since v0.3.0 + * @history v0.6.0: Merged with `makeSized`; modified signature to accept size */ -provide let makeSized = size => { +provide let make = (size=16) => { let buckets = Array.make(size, None) { size: 0, buckets } } -/** - * Creates a new, empty set. - * - * @returns An empty set - * - * @since v0.3.0 - */ -provide let make = () => { - makeSized(16) -} let getBucketIndex = (key, buckets) => { let bucketsLength = Array.length(buckets) diff --git a/stdlib/set.md b/stdlib/set.md index 7d6a443e69..0c3236ebe5 100644 --- a/stdlib/set.md +++ b/stdlib/set.md @@ -40,24 +40,34 @@ Represents the internal state of a set. Functions and constants included in the Set module. -### Set.**makeSized** +### Set.**make** -
-Added in 0.3.0 -No other changes yet. +
+Added in 0.3.0 + + + + + + + +
versionchanges
nextMerged with `makeSized`; modified signature to accept size
```grain -makeSized : (size: Number) => Set
+make : (?size: Number) => Set ``` -Creates a new empty set with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you won't need to care about the storage size of your set and can use `Set.make()` instead. +Creates a new empty set with an initial storage of the given size. As +values are added or removed, the internal storage may grow or shrink. +Generally, you won't need to care about the storage size of your set and +can use the default size. Parameters: |param|type|description| |-----|----|-----------| -|`size`|`Number`|The initial storage size of the set| +|`?size`|`Number`|The initial storage size of the set| Returns: @@ -65,25 +75,6 @@ Returns: |----|-----------| |`Set`|An empty set with the given initial storage size| -### Set.**make** - -
-Added in 0.3.0 -No other changes yet. -
- -```grain -make : () => Set
-``` - -Creates a new, empty set. - -Returns: - -|type|description| -|----|-----------| -|`Set`|An empty set| - ### Set.**add**
diff --git a/stdlib/stack.gr b/stdlib/stack.gr index 365955c3c0..c8fa18ae1f 100644 --- a/stdlib/stack.gr +++ b/stdlib/stack.gr @@ -26,27 +26,18 @@ abstract record Stack { /** * Creates a new stack with an initial storage of the given size. As values are * added or removed, the internal storage may grow or shrink. Generally, you - * won’t need to care about the storage size of your map and can use - * `Stack.make()` instead. + * won’t need to care about the storage size of your map and can use the + * default size. * * @param size: The initial storage size of the stack * @returns An empty stack - * + * * @since v0.6.0 */ -provide let makeSized = size => { +provide let make = (size=16) => { { size: 0, array: Array.make(size, None) } } -/** - * Creates a new stack. - * - * @returns An empty stack - * - * @since v0.6.0 - */ -provide let make = () => makeSized(16) - /** * Checks if the given stack contains no items. * diff --git a/stdlib/stack.md b/stdlib/stack.md index 7c517c183a..3f69284eab 100644 --- a/stdlib/stack.md +++ b/stdlib/stack.md @@ -33,7 +33,7 @@ A mutable LIFO (last-in-first-out) data structure. Functions and constants included in the Stack module. -### Stack.**makeSized** +### Stack.**make**
Added in next @@ -41,38 +41,19 @@ No other changes yet.
```grain -makeSized : (size: Number) => Stack
+make : (?size: Number) => Stack ``` Creates a new stack with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you -won’t need to care about the storage size of your map and can use -`Stack.make()` instead. +won’t need to care about the storage size of your map and can use the +default size. Parameters: |param|type|description| |-----|----|-----------| -|`size`|`Number`|The initial storage size of the stack| - -Returns: - -|type|description| -|----|-----------| -|`Stack`|An empty stack| - -### Stack.**make** - -
-Added in next -No other changes yet. -
- -```grain -make : () => Stack
-``` - -Creates a new stack. +|`?size`|`Number`|The initial storage size of the stack| Returns: diff --git a/stdlib/string.gr b/stdlib/string.gr index 5381a2c37a..b10c022292 100644 --- a/stdlib/string.gr +++ b/stdlib/string.gr @@ -1371,39 +1371,23 @@ let encodeAtHelp = } /** - * Encodes the given string into a byte sequence at the supplied position, excluding any byte-order marker, using the encoding scheme provided. + * Encodes the given string into a byte sequence at the supplied position using the encoding scheme provided. * * @param string: The input string * @param encoding: The encoding to use * @param dest: The byte sequence that will be copied * @param destPos: The location in the byte sequence to write the output + * @param includeBom: Whether or not to include a byte order marker (false by default) * @returns A copy of the input bytes with the encoded string replaced at the given position * * @throws InvalidArgument(String): When `destPos` is not an integer * @throws InvalidArgument(String): When `destPos` is negative * * @since v0.4.0 + * @history v0.6.0: Added `includeBom` default argument */ -provide let encodeAt = (string, encoding, dest, destPos) => { - encodeAtHelp(string, encoding, false, dest, destPos) -} - -/** - * Encodes the given string into a byte sequence at the supplied position, including any byte-order marker, using the encoding scheme provided. - * - * @param string: The input string - * @param encoding: The encoding to use - * @param dest: The byte sequence that will be copied - * @param destPos: The location in the byte sequence to write the output - * @returns A copy of the input bytes with the encoded string replaced at the given position - * - * @throws InvalidArgument(String): When `destPos` is not an integer - * @throws InvalidArgument(String): When `destPos` is negative - * - * @since v0.4.0 - */ -provide let encodeAtWithBom = (string, encoding, dest, destPos) => { - encodeAtHelp(string, encoding, true, dest, destPos) +provide let encodeAt = (string, encoding, dest, destPos, includeBom=false) => { + encodeAtHelp(string, encoding, includeBom, dest, destPos) } @unsafe @@ -1426,29 +1410,18 @@ let encodeHelp = (string: String, encoding: Encoding, includeBom: Bool) => { } /** - * Encodes the given string using the given encoding scheme, excluding any byte-order marker. + * Encodes the given string using the given encoding scheme. * * @param string: The input string * @param encoding: The encoding to use + * @param includeBom: Whether or not to include a byte order marker (false by default) * @returns The byte representation of the string in the given encoding * * @since v0.4.0 + * @history v0.6.0: Added `includeBom` default argument */ -provide let encode = (string: String, encoding: Encoding) => { - encodeHelp(string, encoding, false) -} - -/** - * Encodes the given string using the given encoding scheme, including any byte-order marker. - * - * @param string: The input string - * @param encoding: The encoding to use - * @returns The byte representation of the string in the given encoding - * - * @since v0.4.0 - */ -provide let encodeWithBom = (string: String, encoding: Encoding) => { - encodeHelp(string, encoding, true) +provide let encode = (string: String, encoding: Encoding, includeBom=false) => { + encodeHelp(string, encoding, includeBom) } // Byte->String decoding and helper functions: @@ -1846,12 +1819,13 @@ let decodeRangeHelp = } /** - * Decodes the given byte sequence of the specified range into a string, excluding any byte-order marker, using encoding scheme provided. + * Decodes the given byte sequence of the specified range into a string using the encoding scheme provided. * * @param bytes: The input bytes * @param encoding: The encoding to use * @param start: The byte offset to begin decoding from * @param size: The maximum number of bytes to decode + * @param keepBom: Whether or not to include a byte order marker (false by default) * @returns The decoded string * * @throws InvalidArgument(String): When `start` is not an integer @@ -1860,6 +1834,7 @@ let decodeRangeHelp = * @throws InvalidArgument(String): When `size` is negative * * @since v0.4.0 + * @history v0.6.0: Added `keepBom` default argument */ provide let decodeRange = ( @@ -1867,34 +1842,9 @@ provide let decodeRange = encoding: Encoding, start: Number, size: Number, + keepBom=false, ) => { - decodeRangeHelp(bytes, encoding, true, start, size) -} - -/** - * Decodes the given byte sequence of the specified range into a string, including any byte-order marker, using encoding scheme provided. - * - * @param bytes: The input bytes - * @param encoding: The encoding to use - * @param start: The byte offset to begin decoding from - * @param size: The maximum number of bytes to decode - * @returns The decoded string - * - * @throws InvalidArgument(String): When `start` is not an integer - * @throws InvalidArgument(String): When `start` is negative - * @throws InvalidArgument(String): When `size` is not an integer - * @throws InvalidArgument(String): When `size` is negative - * - * @since v0.4.0 - */ -provide let decodeRangeKeepBom = - ( - bytes: Bytes, - encoding: Encoding, - start: Number, - size: Number, - ) => { - decodeRangeHelp(bytes, encoding, false, start, size) + decodeRangeHelp(bytes, encoding, !keepBom, start, size) } @unsafe @@ -1905,29 +1855,18 @@ let decodeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool) => { } /** - * Decodes the given byte sequence into a string using the given encoding scheme, excluding any byte-order marker. - * - * @param bytes: The input bytes - * @param encoding: The encoding to use - * @returns The decoded string - * - * @since v0.4.0 - */ -provide let decode = (bytes: Bytes, encoding: Encoding) => { - decodeHelp(bytes, encoding, true) -} - -/** - * Decodes the given byte sequence into a string using the given encoding scheme, including any byte-order marker. + * Decodes the given byte sequence into a string using the given encoding scheme. * * @param bytes: The input bytes * @param encoding: The encoding to use + * @param keepBom: Whether or not to include a byte order marker (false by default) * @returns The decoded string * * @since v0.4.0 + * @history v0.6.0: Added `keepBom` default argument */ -provide let decodeKeepBom = (bytes: Bytes, encoding: Encoding) => { - decodeHelp(bytes, encoding, false) +provide let decode = (bytes: Bytes, encoding: Encoding, keepBom=false) => { + decodeHelp(bytes, encoding, !keepBom) } /** diff --git a/stdlib/string.md b/stdlib/string.md index 98285ec0ec..70df77cabb 100644 --- a/stdlib/string.md +++ b/stdlib/string.md @@ -699,53 +699,25 @@ String.replaceAll("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌎🌎" ### String.**encodeAt** -
-Added in 0.4.0 -No other changes yet. +
+Added in 0.4.0 + + + + + + + +
versionchanges
nextAdded `includeBom` default argument
```grain encodeAt : - (string: String, encoding: Encoding, dest: Bytes, destPos: Number) => Bytes -``` - -Encodes the given string into a byte sequence at the supplied position, excluding any byte-order marker, using the encoding scheme provided. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`string`|`String`|The input string| -|`encoding`|`Encoding`|The encoding to use| -|`dest`|`Bytes`|The byte sequence that will be copied| -|`destPos`|`Number`|The location in the byte sequence to write the output| - -Returns: - -|type|description| -|----|-----------| -|`Bytes`|A copy of the input bytes with the encoded string replaced at the given position| - -Throws: - -`InvalidArgument(String)` - -* When `destPos` is not an integer -* When `destPos` is negative - -### String.**encodeAtWithBom** - -
-Added in 0.4.0 -No other changes yet. -
- -```grain -encodeAtWithBom : - (string: String, encoding: Encoding, dest: Bytes, destPos: Number) => Bytes + (string: String, encoding: Encoding, dest: Bytes, destPos: Number, + ?includeBom: Bool) => Bytes ``` -Encodes the given string into a byte sequence at the supplied position, including any byte-order marker, using the encoding scheme provided. +Encodes the given string into a byte sequence at the supplied position using the encoding scheme provided. Parameters: @@ -755,6 +727,7 @@ Parameters: |`encoding`|`Encoding`|The encoding to use| |`dest`|`Bytes`|The byte sequence that will be copied| |`destPos`|`Number`|The location in the byte sequence to write the output| +|`?includeBom`|`Bool`|Whether or not to include a byte order marker (false by default)| Returns: @@ -771,42 +744,23 @@ Throws: ### String.**encode** -
-Added in 0.4.0 -No other changes yet. -
- -```grain -encode : (string: String, encoding: Encoding) => Bytes -``` - -Encodes the given string using the given encoding scheme, excluding any byte-order marker. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`string`|`String`|The input string| -|`encoding`|`Encoding`|The encoding to use| - -Returns: - -|type|description| -|----|-----------| -|`Bytes`|The byte representation of the string in the given encoding| - -### String.**encodeWithBom** - -
-Added in 0.4.0 -No other changes yet. +
+Added in 0.4.0 + + + + + + + +
versionchanges
nextAdded `includeBom` default argument
```grain -encodeWithBom : (string: String, encoding: Encoding) => Bytes +encode : (string: String, encoding: Encoding, ?includeBom: Bool) => Bytes ``` -Encodes the given string using the given encoding scheme, including any byte-order marker. +Encodes the given string using the given encoding scheme. Parameters: @@ -814,6 +768,7 @@ Parameters: |-----|----|-----------| |`string`|`String`|The input string| |`encoding`|`Encoding`|The encoding to use| +|`?includeBom`|`Bool`|Whether or not to include a byte order marker (false by default)| Returns: @@ -823,55 +778,25 @@ Returns: ### String.**decodeRange** -
-Added in 0.4.0 -No other changes yet. +
+Added in 0.4.0 + + + + + + + +
versionchanges
nextAdded `keepBom` default argument
```grain decodeRange : - (bytes: Bytes, encoding: Encoding, start: Number, size: Number) => String -``` - -Decodes the given byte sequence of the specified range into a string, excluding any byte-order marker, using encoding scheme provided. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`bytes`|`Bytes`|The input bytes| -|`encoding`|`Encoding`|The encoding to use| -|`start`|`Number`|The byte offset to begin decoding from| -|`size`|`Number`|The maximum number of bytes to decode| - -Returns: - -|type|description| -|----|-----------| -|`String`|The decoded string| - -Throws: - -`InvalidArgument(String)` - -* When `start` is not an integer -* When `start` is negative -* When `size` is not an integer -* When `size` is negative - -### String.**decodeRangeKeepBom** - -
-Added in 0.4.0 -No other changes yet. -
- -```grain -decodeRangeKeepBom : - (bytes: Bytes, encoding: Encoding, start: Number, size: Number) => String + (bytes: Bytes, encoding: Encoding, start: Number, size: Number, + ?keepBom: Bool) => String ``` -Decodes the given byte sequence of the specified range into a string, including any byte-order marker, using encoding scheme provided. +Decodes the given byte sequence of the specified range into a string using the encoding scheme provided. Parameters: @@ -881,6 +806,7 @@ Parameters: |`encoding`|`Encoding`|The encoding to use| |`start`|`Number`|The byte offset to begin decoding from| |`size`|`Number`|The maximum number of bytes to decode| +|`?keepBom`|`Bool`|Whether or not to include a byte order marker (false by default)| Returns: @@ -899,42 +825,23 @@ Throws: ### String.**decode** -
-Added in 0.4.0 -No other changes yet. -
- -```grain -decode : (bytes: Bytes, encoding: Encoding) => String -``` - -Decodes the given byte sequence into a string using the given encoding scheme, excluding any byte-order marker. - -Parameters: - -|param|type|description| -|-----|----|-----------| -|`bytes`|`Bytes`|The input bytes| -|`encoding`|`Encoding`|The encoding to use| - -Returns: - -|type|description| -|----|-----------| -|`String`|The decoded string| - -### String.**decodeKeepBom** - -
-Added in 0.4.0 -No other changes yet. +
+Added in 0.4.0 + + + + + + + +
versionchanges
nextAdded `keepBom` default argument
```grain -decodeKeepBom : (bytes: Bytes, encoding: Encoding) => String +decode : (bytes: Bytes, encoding: Encoding, ?keepBom: Bool) => String ``` -Decodes the given byte sequence into a string using the given encoding scheme, including any byte-order marker. +Decodes the given byte sequence into a string using the given encoding scheme. Parameters: @@ -942,6 +849,7 @@ Parameters: |-----|----|-----------| |`bytes`|`Bytes`|The input bytes| |`encoding`|`Encoding`|The encoding to use| +|`?keepBom`|`Bool`|Whether or not to include a byte order marker (false by default)| Returns: