Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.hasByconsteffect/TxHashMap.ts:1686
<K, V>(predicate: (value: V, key: K) => boolean): (
  self: TxHashMap<K, V>
) => Effect.Effect<boolean>
<K, V>(
  self: TxHashMap<K, V>,
  predicate: (value: V, key: K) => boolean
): Effect.Effect<boolean>

Checks whether any entry in the TxHashMap matches the given predicate.

Example (Checking entries with a predicate)

import { Effect, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  // Create a user status map
  const currentTime = 1_700_000_000_000
  const userStatuses = yield* TxHashMap.make(
    ["alice", { status: "online", lastSeen: currentTime }],
    ["bob", { status: "offline", lastSeen: currentTime - 3_600_000 }],
    ["charlie", { status: "online", lastSeen: currentTime }]
  )

  // Check if any users are online
  const hasOnlineUsers = yield* TxHashMap.hasBy(
    userStatuses,
    (user) => user.status === "online"
  )
  console.log(hasOnlineUsers) // true

  // Check if any users have specific username pattern
  const hasAdminUser = yield* TxHashMap.hasBy(
    userStatuses,
    (user, username) => username.startsWith("admin")
  )
  console.log(hasAdminUser) // false

  // Data-last usage with pipe
  const hasRecentActivity = yield* userStatuses.pipe(
    TxHashMap.hasBy((user) => currentTime - user.lastSeen < 1_800_000) // 30 minutes
  )
  console.log(hasRecentActivity) // true
})
combinators
export const hasBy: {
  <K, V>(
    predicate: (value: V, key: K) => boolean
  ): (self: TxHashMap<K, V>) => Effect.Effect<boolean>
  <K, V>(
    self: TxHashMap<K, V>,
    predicate: (value: V, key: K) => boolean
  ): Effect.Effect<boolean>
} = dual(
  2,
  <K, V>(
    self: TxHashMap<K, V>,
    predicate: (value: V, key: K) => boolean
  ): Effect.Effect<boolean> => TxRef.get(self.ref).pipe(Effect.map((map) => HashMap.hasBy(map, predicate)))
)