Skip to content

Commit

Permalink
Add HashMap.some (#4347)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaureRC authored and effect-bot committed Feb 5, 2025
1 parent 9e8b125 commit 9a1ae14
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-starfishes-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Add HashMap.some
14 changes: 14 additions & 0 deletions packages/effect/src/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,17 @@ export const findFirst: {
<K, A, B extends A>(self: HashMap<K, A>, predicate: (a: A, k: K) => a is B): Option<[K, B]>
<K, A>(self: HashMap<K, A>, predicate: (a: A, k: K) => boolean): Option<[K, A]>
} = HM.findFirst

/**
* Checks if any entry in a hashmap meets a specific condition.
*
* @param self - The hashmap to check.
* @param predicate - The condition to test entries (value, key).
*
* @since 3.13.0
* @category elements
*/
export const some: {
<K, A>(predicate: (a: NoInfer<A>, k: K) => boolean): (self: HashMap<K, A>) => boolean
<K, A>(self: HashMap<K, A>, predicate: (a: A, k: K) => boolean): boolean
} = HM.some
16 changes: 16 additions & 0 deletions packages/effect/src/internal/hashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,19 @@ export const findFirst: {
return Option.none()
}
)

/** @internal */
export const some: {
<K, A>(predicate: (a: NoInfer<A>, k: K) => boolean): (self: HM.HashMap<K, A>) => boolean
<K, A>(self: HM.HashMap<K, A>, predicate: (a: A, k: K) => boolean): boolean
} = Dual.dual(
2,
<K, A>(self: HM.HashMap<K, A>, predicate: (a: A, k: K) => boolean): boolean => {
for (const ka of self) {
if (predicate(ka[1], ka[0])) {
return true
}
}
return false
}
)
13 changes: 13 additions & 0 deletions packages/effect/test/HashMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ describe("HashMap", () => {
assertNone(HM.get(key(2))(result))
})

it("some", () => {
const mapWith3LettersMax = HM.make([0, "a"], [1, "bb"], [3, "ccc"])

deepStrictEqual(HM.some(mapWith3LettersMax, (value) => value.length > 3), false)
deepStrictEqual(pipe(mapWith3LettersMax, HM.some((value) => value.length > 3)), false)

deepStrictEqual(HM.some(mapWith3LettersMax, (value) => value.length > 1), true)

deepStrictEqual(HM.some(mapWith3LettersMax, (value, key) => value.length > 1 && key === 0), false)

deepStrictEqual(HM.some(mapWith3LettersMax, (value, key) => value.length > 1 && key === 1), true)
})

it("reduce", () => {
const map1 = HM.make([key(0), value("a")], [key(1), value("b")])
const result1 = pipe(map1, HM.reduce("", (acc, { s }) => acc.length > 0 ? `${acc},${s}` : s))
Expand Down

0 comments on commit 9a1ae14

Please sign in to comment.