Skip to content

Commit

Permalink
Add HashMap.toValues and HashSet.toValues (#4317)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinassefranche authored and tim-smart committed Feb 14, 2025
1 parent adc60e8 commit 7080b67
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-bees-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Add `HashMap.toValues` and `HashSet.toValues` getters
8 changes: 8 additions & 0 deletions packages/effect/src/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ export const keySet: <K, V>(self: HashMap<K, V>) => HashSet<K> = keySet_.keySet
*/
export const values: <K, V>(self: HashMap<K, V>) => IterableIterator<V> = HM.values

/**
* Returns an `Array` of the values within the `HashMap`.
*
* @since 3.13.0
* @category getters
*/
export const toValues = <K, V>(self: HashMap<K, V>): Array<V> => Array.from(values(self))

/**
* Returns an `IterableIterator` of the entries within the `HashMap`.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/effect/src/HashSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ export const isSubset: {
*/
export const values: <A>(self: HashSet<A>) => IterableIterator<A> = HS.values

/**
* Returns an `Array` of the values within the `HashSet`.
*
* @since 3.13.0
* @category getters
*/
export const toValues = <A>(self: HashSet<A>): Array<A> => Array.from(values(self))

/**
* Calculates the number of values in the `HashSet`.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/internal/keyedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const makeImpl = <K, A, E, R>(
}
})
const activePools: Effect.Effect<Array<Pool.Pool<A, E>>> = core.suspend(() =>
core.forEachSequential(Array.from(HashMap.values(MutableRef.get(map))), (value) => {
core.forEachSequential(HashMap.toValues(MutableRef.get(map)), (value) => {
switch (value._tag) {
case "Complete": {
return core.succeed(value.pool)
Expand Down
7 changes: 7 additions & 0 deletions packages/effect/test/HashMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ describe("HashMap", () => {
deepStrictEqual(result, [value("a"), value("b")])
})

it("toValues", () => {
const map = HM.make([key(0), value("a")], [key(1), value("b")])
const result = HM.toValues(map)

deepStrictEqual(result, [value("a"), value("b")])
})

it("entries", () => {
const map = HM.make([key(0), value("a")], [key(1), value("b")])
const result = Array.from(HM.entries(map))
Expand Down
8 changes: 8 additions & 0 deletions packages/effect/test/HashSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ describe("HashSet", () => {
deepStrictEqual(result, [value(0), value(1), value(2)])
})

it("toValues", () => {
const hashSet = makeTestHashSet(0, 1, 2)

const result = HashSet.toValues(hashSet)

deepStrictEqual(result, [value(0), value(1), value(2)])
})

it("pipe()", () => {
strictEqual(
HashSet.empty<string>().pipe(HashSet.add("value"), HashSet.size),
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/test/utils/cache/WatchableLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const makeEffect = <Key, Value, Error = never>(
const assertAllCleaned = () =>
Effect.flatMap(createdResources(), (resources) =>
resourcesCleaned(
Chunk.flatten(Chunk.unsafeFromArray(Array.from(HashMap.values(resources))))
Chunk.flatten(Chunk.unsafeFromArray(HashMap.toValues(resources)))
))
const assertAllCleanedForKey = (key: Key) =>
Effect.flatMap(createdResources(), (resources) =>
Expand Down

0 comments on commit 7080b67

Please sign in to comment.