Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.findFirstconsteffect/TxHashMap.ts:1745
<K, V>(predicate: (value: V, key: K) => boolean): (
  self: TxHashMap<K, V>
) => Effect.Effect<Option.Option<[K, V]>>
<K, V>(
  self: TxHashMap<K, V>,
  predicate: (value: V, key: K) => boolean
): Effect.Effect<Option.Option<[K, V]>>

Finds the first entry in the TxHashMap that matches the given predicate. Returns the key-value pair as a tuple wrapped in an Option.

Example (Finding the first matching entry)

import { Effect, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  // Create a task priority map
  const tasks = yield* TxHashMap.make(
    ["task1", { priority: 1, assignee: "alice", completed: false }],
    ["task2", { priority: 3, assignee: "bob", completed: true }],
    ["task3", { priority: 2, assignee: "alice", completed: false }]
  )

  // Find first high-priority incomplete task
  const highPriorityTask = yield* TxHashMap.findFirst(
    tasks,
    (task) => task.priority >= 2 && !task.completed
  )

  if (highPriorityTask._tag === "Some") {
    const [taskId, task] = highPriorityTask.value
    console.log(`Found task: ${taskId}, priority: ${task.priority}`)
    // "Found task: task3, priority: 2"
  }

  // Find first task assigned to specific user
  const aliceTask = yield* tasks.pipe(
    TxHashMap.findFirst((task) => task.assignee === "alice")
  )

  if (aliceTask._tag === "Some") {
    console.log(`Alice's task: ${aliceTask.value[0]}`)
  }
})
combinators
export const findFirst: {
  <K, V>(
    predicate: (value: V, key: K) => boolean
  ): (self: TxHashMap<K, V>) => Effect.Effect<Option.Option<[K, V]>>
  <K, V>(
    self: TxHashMap<K, V>,
    predicate: (value: V, key: K) => boolean
  ): Effect.Effect<Option.Option<[K, V]>>
} = dual(
  2,
  <K, V>(
    self: TxHashMap<K, V>,
    predicate: (value: V, key: K) => boolean
  ): Effect.Effect<Option.Option<[K, V]>> =>
    TxRef.get(self.ref).pipe(Effect.map((map) => HashMap.findFirst(map, predicate)))
)