<A extends object>(self: A): numberGenerates a random hash value for an object and caches it.
When to use
Use to hash an object by reference identity instead of structural content.
Details
This function creates a random hash value for objects that don't have their own hash implementation. The hash value is cached using a WeakMap, so the same object will always return the same hash value during its lifetime.
Example (Hashing objects by reference)
import { Hash } from "effect"
const obj1 = { a: 1 }
const obj2 = { a: 1 }
// Same object always returns the same hash
console.log(Hash.random(obj1) === Hash.random(obj1)) // true
// Different objects get different hashes
console.log(Hash.random(obj1) === Hash.random(obj2)) // falseexport const const random: <A extends object>(
self: A
) => number
Generates a random hash value for an object and caches it.
When to use
Use to hash an object by reference identity instead of structural content.
Details
This function creates a random hash value for objects that don't have their own
hash implementation. The hash value is cached using a WeakMap, so the same object
will always return the same hash value during its lifetime.
Example (Hashing objects by reference)
import { Hash } from "effect"
const obj1 = { a: 1 }
const obj2 = { a: 1 }
// Same object always returns the same hash
console.log(Hash.random(obj1) === Hash.random(obj1)) // true
// Different objects get different hashes
console.log(Hash.random(obj1) === Hash.random(obj2)) // false
random: <function (type parameter) A in <A extends object>(self: A): numberA extends object>(self: A extends objectself: function (type parameter) A in <A extends object>(self: A): numberA) => number = (self: A extends objectself) => {
if (!const randomHashCache: WeakMap<
any,
number
>
randomHashCache.WeakMap<any, number>.has(key: any): booleanhas(self: A extends objectself)) {
const randomHashCache: WeakMap<
any,
number
>
randomHashCache.WeakMap<any, number>.set(key: any, value: number): WeakMap<any, number>Adds a new element with a specified key and value.
set(self: A extends objectself, const number: (n: number) => numberComputes a hash value for a number.
When to use
Use to hash a JavaScript number with Effect's numeric hash semantics.
Details
This function creates a hash value for numeric inputs, handling special cases
like NaN, Infinity, and -Infinity with distinct hash values. It uses bitwise operations to ensure good distribution
of hash values across different numeric inputs.
Example (Hashing numbers)
import { Hash } from "effect"
console.log(Hash.number(42)) // hash of 42
console.log(Hash.number(3.14)) // hash of 3.14
console.log(Hash.number(NaN)) // hash of "NaN"
console.log(Hash.number(Infinity)) // 0 (special case)
// Same numbers produce the same hash
console.log(Hash.number(100) === Hash.number(100)) // true
number(var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.random(): numberReturns a pseudorandom number between 0 and 1.
random() * var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.MAX_SAFE_INTEGER: numberThe value of the largest integer n such that n and n + 1 are both exactly representable as
a Number value.
The value of Number.MAX_SAFE_INTEGER is 9007199254740991 2^53 − 1.
MAX_SAFE_INTEGER)))
}
return const randomHashCache: WeakMap<
any,
number
>
randomHashCache.WeakMap<any, number>.get(key: any): number | undefinedget(self: A extends objectself)!
}