Skip to content

Commit

Permalink
✨ Add HashMap.some with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LaureRC committed Jan 27, 2025
1 parent 2358caf commit 6b9ce25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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 @@ -284,6 +284,19 @@ describe("HashMap", () => {
deepStrictEqual(HM.get(key(2))(result), Option.none())
})

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 6b9ce25

Please sign in to comment.