<A>(arr: Iterable<A>): numberComputes a hash value for an iterable by hashing all of its elements.
When to use
Use to hash the values yielded by an iterable with Effect hash semantics.
Details
The implementation folds element hashes from the seed 6151 with XOR and
then optimizes the final hash.
Gotchas
A hash is not an equality proof. Because this implementation uses XOR, reordered inputs can produce the same hash.
Example (Hashing arrays)
import { Hash } from "effect"
const arr1 = [1, 2, 3]
const arr2 = [1, 2, 3]
const arr3 = [3, 2, 1]
console.log(Hash.array(arr1)) // hash of [1, 2, 3]
console.log(Hash.array(arr2)) // same hash as arr1
console.log(Hash.array(arr3)) // may match reordered inputs
console.log(Hash.array(arr1) === Hash.array(arr2)) // true
console.log(Hash.array(arr1) === Hash.array(arr3)) // trueexport const const array: <A>(
arr: Iterable<A>
) => number
Computes a hash value for an iterable by hashing all of its elements.
When to use
Use to hash the values yielded by an iterable with Effect hash semantics.
Details
The implementation folds element hashes from the seed 6151 with XOR and
then optimizes the final hash.
Gotchas
A hash is not an equality proof. Because this implementation uses XOR,
reordered inputs can produce the same hash.
Example (Hashing arrays)
import { Hash } from "effect"
const arr1 = [1, 2, 3]
const arr2 = [1, 2, 3]
const arr3 = [3, 2, 1]
console.log(Hash.array(arr1)) // hash of [1, 2, 3]
console.log(Hash.array(arr2)) // same hash as arr1
console.log(Hash.array(arr3)) // may match reordered inputs
console.log(Hash.array(arr1) === Hash.array(arr2)) // true
console.log(Hash.array(arr1) === Hash.array(arr3)) // true
array: <function (type parameter) A in <A>(arr: Iterable<A>): numberA>(arr: Iterable<A>arr: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(arr: Iterable<A>): numberA>) => number = const iterableWith: (
seed: number,
f: (el: any) => number
) => (iter: Iterable<any>) => number
iterableWith(6151, const hash: <A>(self: A) => numberComputes a hash value for any given value.
When to use
Use to compute an Effect hash for primitives, collections, and hashable
objects.
Details
This function can hash primitives (numbers, strings, booleans, etc.) as well as
objects, arrays, and other complex data structures. It automatically handles
different types and provides a consistent hash value for equivalent inputs.
Gotchas
Objects being hashed must be treated as immutable after their first hash
computation. Hash results are cached, so mutating an object after hashing will
lead to stale cached values and broken hash-based operations. For mutable
objects, implement a custom Hash interface that hashes the object reference
rather than its content.
Example (Hashing different values)
import { Hash } from "effect"
// Hash primitive values
console.log(Hash.hash(42)) // numeric hash
console.log(Hash.hash("hello")) // string hash
console.log(Hash.hash(true)) // boolean hash
// Hash objects and arrays
console.log(Hash.hash({ name: "John", age: 30 }))
console.log(Hash.hash([1, 2, 3]))
console.log(Hash.hash({ id: "user-1", roles: ["admin", "editor"] }))
hash)