Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HashMap.some #4347

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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
@@ -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
@@ -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))