diff --git a/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot b/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot index 7d2b6374f7..8ca1f50236 100644 --- a/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot +++ b/compiler/test/__snapshots__/provides.82c10ab4.0.snapshot @@ -12,21 +12,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_1199 (mut i32))) - (import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1197 (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$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_1199 (param i32 i32) (result i32))) - (import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1197 (param i32 i32) (result i32))) + (import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1200 (param i32 i32) (result i32))) + (import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1198 (param i32 i32) (result i32))) (global $GRAIN$TABLE_SIZE i32 (i32.const 1)) (memory $0 0) - (elem $elem (global.get $relocBase_0) $lam_lambda_1198) + (elem $elem (global.get $relocBase_0) $lam_lambda_1199) (export \"memory\" (memory $0)) (export \"_gmain\" (func $_gmain)) (export \"_start\" (func $_start)) (export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE)) - (func $lam_lambda_1198 (param $0 i32) (param $1 i32) (result i32) + (func $lam_lambda_1199 (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (local $4 i32) @@ -76,10 +76,10 @@ provides › provide12 ) ) ) - (return_call $print_1199 + (return_call $print_1200 (call $incRef_0 (global.get $GRAIN$EXPORT$incRef_0) - (global.get $print_1199) + (global.get $print_1200) ) (local.get $8) ) @@ -131,10 +131,10 @@ provides › provide12 ) ) ) - (return_call $apply_1197 + (return_call $apply_1198 (call $incRef_0 (global.get $GRAIN$EXPORT$incRef_0) - (global.get $apply_1197) + (global.get $apply_1198) ) (local.get $6) ) diff --git a/compiler/test/stdlib/map.test.gr b/compiler/test/stdlib/map.test.gr index cab66b7833..699f8c7732 100644 --- a/compiler/test/stdlib/map.test.gr +++ b/compiler/test/stdlib/map.test.gr @@ -269,30 +269,25 @@ assert Map.contains(Brick, fa) assert !Map.contains(Wood, fa) // Resizes the map when it grows -// TODO(#190): Don't use these internals, as they need to change after 190 is fixed let resize = Map.makeSized(1) -// (nodeCount, bucketLength) -assert Map.getInternalStats(resize) == (0, 1) +assert Map.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 } Map.set("🌾", 1, resize) Map.set("🐑", 1, resize) -// (nodeCount, bucketLength) -assert Map.getInternalStats(resize) == (2, 1) +assert Map.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 } Map.set("🧱", 1, resize) -// (nodeCount, bucketLength) -assert Map.getInternalStats(resize) == (3, 2) +assert Map.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 } // Regression tests for https://github.com/grain-lang/grain/issues/497 let largeMap = Map.fromArray(Array.init(128, i => (i, i))) -// (nodeCount, bucketLength) -assert Map.getInternalStats(largeMap) == (128, 64) +assert Map.getInternalStats(largeMap) == { currentSize: 128, bucketCount: 64 } // Map.filter() diff --git a/compiler/test/stdlib/set.test.gr b/compiler/test/stdlib/set.test.gr index 8b30859963..11e371e552 100644 --- a/compiler/test/stdlib/set.test.gr +++ b/compiler/test/stdlib/set.test.gr @@ -235,30 +235,25 @@ assert Array.contains(3, r) assert Array.contains(4, r) // Resizes the map when it grows -// TODO(#190): Don't use these internals, as they need to change after 190 is fixed let resize = Set.makeSized(1) -// (nodeCount, bucketLength) -assert Set.getInternalStats(resize) == (0, 1) +assert Set.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 } Set.add("🌾", resize) Set.add("🐑", resize) -// (nodeCount, bucketLength) -assert Set.getInternalStats(resize) == (2, 1) +assert Set.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 } Set.add("🧱", resize) -// (nodeCount, bucketLength) -assert Set.getInternalStats(resize) == (3, 2) +assert Set.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 } // Regression tests for https://github.com/grain-lang/grain/issues/497 let largeSet = Set.fromArray(Array.init(128, i => i)) -// (nodeCount, bucketLength) -assert Set.getInternalStats(largeSet) == (128, 64) +assert Set.getInternalStats(largeSet) == { currentSize: 128, bucketCount: 64 } module Immutable { from Set use { module Immutable as Set } diff --git a/stdlib/map.gr b/stdlib/map.gr index 60f78b405e..345728937c 100644 --- a/stdlib/map.gr +++ b/stdlib/map.gr @@ -31,6 +31,14 @@ abstract record Map { mut buckets: Array>>, } +/** + * Represents the internal state of a map. + */ +provide record InternalMapStats { + currentSize: Number, + bucketCount: Number, +} + /** * 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. * @@ -497,7 +505,6 @@ provide let reject = (predicate, map) => { filter((key, value) => !predicate(key, value), map) } -// TODO(#190): Should return a Record type instead of a Tuple /** * Provides data representing the internal state state of the map. * @@ -505,14 +512,15 @@ provide let reject = (predicate, map) => { * @returns The internal state of the map * * @since v0.2.0 + * @history v0.6.0: Return `InternalMapStats` record instead of a tuple */ provide let getInternalStats = map => { - (map.size, Array.length(map.buckets)) + { currentSize: map.size, bucketCount: Array.length(map.buckets) } } /** * An immutable map implementation. - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutablemap"` module */ diff --git a/stdlib/map.md b/stdlib/map.md index 5bd77315ac..5cb94b0b9b 100644 --- a/stdlib/map.md +++ b/stdlib/map.md @@ -25,6 +25,17 @@ Type declarations included in the Map module. type Map ``` +### Map.**InternalMapStats** + +```grain +record InternalMapStats { + currentSize: Number, + bucketCount: Number, +} +``` + +Represents the internal state of a map. + ## Values Functions and constants included in the Map module. @@ -502,13 +513,20 @@ Parameters: ### Map.**getInternalStats** -
-Added in 0.2.0 -No other changes yet. +
+Added in 0.2.0 + + + + + + + +
versionchanges
nextReturn `InternalMapStats` record instead of a tuple
```grain -getInternalStats : (map: Map) => (Number, Number) +getInternalStats : (map: Map) => InternalMapStats ``` Provides data representing the internal state state of the map. @@ -523,7 +541,7 @@ Returns: |type|description| |----|-----------| -|`(Number, Number)`|The internal state of the map| +|`InternalMapStats`|The internal state of the map| ## Map.Immutable diff --git a/stdlib/set.gr b/stdlib/set.gr index ed91e41550..0d723a519a 100644 --- a/stdlib/set.gr +++ b/stdlib/set.gr @@ -24,6 +24,14 @@ abstract record Set { mut buckets: Array>>, } +/** + * Represents the internal state of a set. + */ +provide record InternalSetStats { + currentSize: Number, + bucketCount: Number, +} + // 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. @@ -452,7 +460,6 @@ provide let intersect = (set1, set2) => { set } -// TODO(#190): Should return a Record type instead of a Tuple /** * Provides data representing the internal state state of the set. * @@ -460,14 +467,15 @@ provide let intersect = (set1, set2) => { * @returns The internal state of the set * * @since v0.3.0 + * @history v0.6.0: Return `InternalSetStats` record instead of a tuple */ provide let getInternalStats = set => { - (set.size, Array.length(set.buckets)) + { currentSize: set.size, bucketCount: Array.length(set.buckets) } } /** * An immutable set implementation. - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -493,7 +501,7 @@ provide module Immutable { /** * An empty set - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -514,7 +522,7 @@ provide module Immutable { * * @param set: The set to inspect * @returns The count of elements in the set - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -531,7 +539,7 @@ provide module Immutable { * * @param set: The set to inspect * @returns `true` if the given set is empty or `false` otherwise - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -617,7 +625,7 @@ provide module Immutable { * @param key: The value to add * @param set: The base set * @returns A new set containing the new element - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -641,7 +649,7 @@ provide module Immutable { * @param key: The value to search for * @param set: The set to search * @returns `true` if the set contains the given value or `false` otherwise - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -687,7 +695,7 @@ provide module Immutable { * @param key: The value to exclude * @param set: The set to exclude from * @returns A new set without the excluded element - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -710,7 +718,7 @@ provide module Immutable { * * @param fn: The iterator function to call with each element * @param set: The set to iterate - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -736,7 +744,7 @@ provide module Immutable { * @param init: The initial value to use for the accumulator on the first iteration * @param set: The set to iterate * @returns The final accumulator returned from `fn` - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -819,7 +827,7 @@ provide module Immutable { * @param fn: The predicate function to indicate which elements to exclude from the set, where returning `false` indicates the value should be excluded * @param set: The set to iterate * @returns A new set excluding the elements not fulfilling the predicate - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -846,7 +854,7 @@ provide module Immutable { * @param fn: The predicate function to indicate which elements to exclude from the set, where returning `true` indicates the value should be excluded * @param set: The set to iterate * @returns A new set excluding the elements fulfilling the predicate - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -861,7 +869,7 @@ provide module Immutable { * @param set1: The first set to combine * @param set2: The second set to combine * @returns A set containing all elements of both sets - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -883,7 +891,7 @@ provide module Immutable { * @param set1: The first set to combine * @param set2: The second set to combine * @returns A set containing only unshared elements from both sets - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -908,7 +916,7 @@ provide module Immutable { * @param set1: The first set to combine * @param set2: The second set to combine * @returns A set containing only shared elements from both sets - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -933,7 +941,7 @@ provide module Immutable { * * @param list: The list to convert * @returns A set containing all list values - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -947,7 +955,7 @@ provide module Immutable { * * @param set: The set to convert * @returns A list containing all set values - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -969,7 +977,7 @@ provide module Immutable { * * @param array: The array to convert * @returns A set containing all array values - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ @@ -983,7 +991,7 @@ provide module Immutable { * * @param set: The set to convert * @returns An array containing all set values - * + * * @since v0.6.0 * @history v0.5.4: Originally in `"immutableset"` module */ diff --git a/stdlib/set.md b/stdlib/set.md index ece3e76e19..7d6a443e69 100644 --- a/stdlib/set.md +++ b/stdlib/set.md @@ -25,6 +25,17 @@ Type declarations included in the Set module. type Set ``` +### Set.**InternalSetStats** + +```grain +record InternalSetStats { + currentSize: Number, + bucketCount: Number, +} +``` + +Represents the internal state of a set. + ## Values Functions and constants included in the Set module. @@ -482,13 +493,20 @@ Returns: ### Set.**getInternalStats** -
-Added in 0.3.0 -No other changes yet. +
+Added in 0.3.0 + + + + + + + +
versionchanges
nextReturn `InternalSetStats` record instead of a tuple
```grain -getInternalStats : (set: Set) => (Number, Number) +getInternalStats : (set: Set) => InternalSetStats ``` Provides data representing the internal state state of the set. @@ -503,7 +521,7 @@ Returns: |type|description| |----|-----------| -|`(Number, Number)`|The internal state of the set| +|`InternalSetStats`|The internal state of the set| ## Set.Immutable