Skip to content

Commit

Permalink
Add RcMap.keys and MutableHashMap.keys lookup functions (#3559)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Tim Smart <tim.smart@arisechurch.com>
  • Loading branch information
3 people committed Sep 15, 2024
1 parent 812a4e8 commit 42a8f99
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .changeset/violet-suns-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"effect": minor
---

Added `RcMap.keys` and `MutableHashMap.keys`.

These functions allow you to get a list of keys currently stored in the underlying hash map.

```ts
const map = MutableHashMap.make([["a", "a"], ["b", "b"], ["c", "c"]])
const keys = MutableHashMap.keys(map) // ["a", "b", "c"]
```

```ts
Effect.gen(function* () {
const map = yield* RcMap.make({
lookup: (key) => Effect.succeed(key)
})

yield* RcMap.get(map, "a")
yield* RcMap.get(map, "b")
yield* RcMap.get(map, "c")

const keys = yield* RcMap.keys(map) // ["a", "b", "c"]
})
```
12 changes: 12 additions & 0 deletions packages/effect/src/MutableHashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ export const get: {
return getFromBucket(self, bucket, key)
})

/**
* @since 3.8.0
* @category elements
*/
export const keys = <K, V>(self: MutableHashMap<K, V>): Array<K> => {
const keys: Array<K> = []
for (const [key] of self) {
keys.push(key)
}
return keys
}

const getFromBucket = <K, V>(
self: MutableHashMap<K, V>,
bucket: NonEmptyArray<readonly [K & Equal.Equal, V]>,
Expand Down
6 changes: 6 additions & 0 deletions packages/effect/src/RcMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,9 @@ export const get: {
<K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<A, E, Scope.Scope>
<K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<A, E, Scope.Scope>
} = internal.get

/**
* @since 3.8.0
* @category combinators
*/
export const keys: <K, A, E>(self: RcMap<K, A, E>) => Effect.Effect<Array<K>, E> = internal.keys
8 changes: 8 additions & 0 deletions packages/effect/src/internal/rcMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,11 @@ export const get: {
)
}
)

/** @internal */
export const keys = <K, A, E>(self: RcMap.RcMap<K, A, E>): Effect<Array<K>> => {
const impl = self as RcMapImpl<K, A, E>
return core.suspend(() =>
impl.state._tag === "Closed" ? core.interrupt : core.succeed(MutableHashMap.keys(impl.state.map))
)
}
13 changes: 13 additions & 0 deletions packages/effect/test/MutableHashMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ describe("MutableHashMap", () => {
)
})

it("keys", () => {
const map = pipe(
HM.empty<Key, Value>(),
HM.set(key(0, 0), value(0, 0)),
HM.set(key(1, 1), value(1, 1))
)

expect(HM.keys(map)).toStrictEqual([
key(0, 0),
key(1, 1)
])
})

it("modifyAt", () => {
const map = pipe(
HM.empty<Key, Value>(),
Expand Down
13 changes: 13 additions & 0 deletions packages/effect/test/RcMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,17 @@ describe("RcMap", () => {
// no failure means a hit
assert.strictEqual(yield* RcMap.get(map, new Key({ id: 1 })), 1)
}))

it.scoped("keys lookup", () =>
Effect.gen(function*() {
const map = yield* RcMap.make({
lookup: (key: string) => Effect.succeed(key)
})

yield* RcMap.get(map, "foo")
yield* RcMap.get(map, "bar")
yield* RcMap.get(map, "baz")

assert.deepStrictEqual(yield* RcMap.keys(map), ["foo", "bar", "baz"])
}))
})

0 comments on commit 42a8f99

Please sign in to comment.