Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.keysconsteffect/TxHashMap.ts:860
<K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<K>>

Returns an array of all keys in the TxHashMap.

Example (Reading keys)

import { Effect, Option, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  const userRoles = yield* TxHashMap.make(
    ["alice", "admin"],
    ["bob", "user"],
    ["charlie", "moderator"]
  )

  const usernames = yield* TxHashMap.keys(userRoles)
  console.log(usernames.sort()) // ["alice", "bob", "charlie"]

  // Useful for iteration
  for (const username of usernames) {
    const role = yield* TxHashMap.get(userRoles, username)
    if (role._tag === "Some") {
      console.log(`${username}: ${role.value}`)
    }
  }
})
combinators
export const keys = <K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<K>> =>
  Effect.gen(function*() {
    const map = yield* TxRef.get(self.ref)
    return Array.from(HashMap.keys(map))
  })