Skip to content

Commit

Permalink
Add Hashmap.every (#4513)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaureRC authored and effect-bot committed Mar 11, 2025
1 parent 4b82ee7 commit fd4c7bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-flies-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Add HashMap.every
14 changes: 14 additions & 0 deletions packages/effect/src/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,17 @@ 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

/**
* Checks if all entries in a hashmap meets a specific condition.
*
* @param self - The hashmap to check.
* @param predicate - The condition to test entries (value, key).
*
* @since 3.14.0
* @category elements
*/
export const every: {
<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.every
9 changes: 9 additions & 0 deletions packages/effect/src/internal/hashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,12 @@ export const some: {
return false
}
)

/** @internal */
export const every: {
<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 => !some(self, (a, k) => !predicate(a, k))
)
13 changes: 13 additions & 0 deletions packages/effect/test/HashMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ describe("HashMap", () => {
deepStrictEqual(HM.some(mapWith3LettersMax, (value, key) => value.length > 1 && key === 1), true)
})

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

deepStrictEqual(HM.every(mapWith3LettersMax, (value) => value.length > 2), false)
deepStrictEqual(pipe(mapWith3LettersMax, HM.every((value) => value.length > 2)), false)

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

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

deepStrictEqual(HM.every(mapWith3LettersMax, (value, key) => value.length >= 1 && key >= 0), 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 fd4c7bf

Please sign in to comment.