Skip to content

Commit 5189800

Browse files
Adds HashMap.hasBy (#4915)
Co-authored-by: Tim <hello@timsmart.co>
1 parent 0552674 commit 5189800

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

.changeset/fine-groups-change.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Add HashMap.hasBy helper
6+
7+
```ts
8+
import { HashMap } from "effect"
9+
10+
const hm = HashMap.make([1, 'a'])
11+
HashMap.hasBy(hm, (value, key) => value === 'a' && key === 1); // -> true
12+
HashMap.hasBy(hm, (value) => value === 'b'); // -> false
13+
14+
```

packages/effect/src/HashMap.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ export const hasHash: {
194194
<K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): boolean
195195
} = HM.hasHash
196196

197+
/**
198+
* Checks if an element matching the given predicate exists in the given `HashMap`.
199+
*
200+
* @example
201+
* ```ts
202+
* import { HashMap } from "effect"
203+
*
204+
* const hm = HashMap.make([1, 'a'])
205+
* HashMap.hasBy(hm, (value, key) => value === 'a' && key === 1); // -> true
206+
* HashMap.hasBy(hm, (value) => value === 'b'); // -> false
207+
*
208+
* ```
209+
*
210+
* @since 3.16.0
211+
* @category elements
212+
*/
213+
export const hasBy: {
214+
<K, V>(predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): (self: HashMap<K, V>) => boolean
215+
<K, V>(self: HashMap<K, V>, predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): boolean
216+
} = HM.hasBy
217+
197218
/**
198219
* Sets the specified key to the specified value using the internal hashing
199220
* function.

packages/effect/src/internal/hashMap.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ export const hasHash = Dual.dual<
287287
<K, V, K1 extends K>(self: HM.HashMap<K, V>, key: K1, hash: number) => boolean
288288
>(3, (self, key, hash) => Option.isSome(getHash(self, key, hash)))
289289

290+
/** @internal */
291+
export const hasBy = Dual.dual<
292+
<K, V>(predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean) => (self: HM.HashMap<K, V>) => boolean,
293+
<K, V>(self: HM.HashMap<K, V>, predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean) => boolean
294+
>(2, (self, predicate) => Option.isSome(findFirst(self, predicate)))
295+
290296
/** @internal */
291297
export const set = Dual.dual<
292298
<K, V>(key: K, value: V) => (self: HM.HashMap<K, V>) => HM.HashMap<K, V>,

packages/effect/test/HashMap.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ describe("HashMap", () => {
9494
assertFalse(HM.hasHash(key(1), Hash.hash(key(0)))(map))
9595
})
9696

97+
it("hasBy", () => {
98+
const map = HM.make([key(0), value("a")])
99+
100+
assertTrue(HM.hasBy(map, (v) => Equal.equals(v, value("a"))))
101+
assertTrue(HM.hasBy(map, (_, k) => Equal.equals(k, key(0))))
102+
assertTrue(pipe(map, HM.hasBy((v) => Equal.equals(v, value("a")))))
103+
assertFalse(HM.hasBy(map, (v) => Equal.equals(v, value("b"))))
104+
assertFalse(HM.hasBy(map, (_, k) => Equal.equals(k, key(1))))
105+
assertFalse(pipe(map, HM.hasBy((v) => Equal.equals(v, value("b")))))
106+
})
107+
97108
it("get", () => {
98109
const map = HM.make([key(0), value("a")])
99110

0 commit comments

Comments
 (0)