From 19f7dc165774dc7521436816aaf8a9eec2ac75f1 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 15 Apr 2022 15:05:30 +0100 Subject: [PATCH] add brand (boolean rand) and brandBy --- packages/core/signal.mjs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/core/signal.mjs b/packages/core/signal.mjs index e8c66171..29a925f6 100644 --- a/packages/core/signal.mjs +++ b/packages/core/signal.mjs @@ -29,23 +29,22 @@ export const square2 = square._toBipolar(); export const tri = fastcat(isaw, saw); export const tri2 = fastcat(isaw2, saw2); - // random signals -const xorwise = x => { - const a = (x << 13) ^ x - const b = (a >> 17) ^ a - return (b << 5) ^ b -} +const xorwise = (x) => { + const a = (x << 13) ^ x; + const b = (a >> 17) ^ a; + return (b << 5) ^ b; +}; // stretch 300 cycles over the range of [0,2**29 == 536870912) then apply the xorshift algorithm -const _frac = x => x - Math.trunc(x); +const _frac = (x) => x - Math.trunc(x); -const timeToIntSeed = x => xorwise(Math.trunc(_frac(x / 300) * 536870912)); +const timeToIntSeed = (x) => xorwise(Math.trunc(_frac(x / 300) * 536870912)); -const intSeedToRand = x => (x % 536870912) / 536870912; +const intSeedToRand = (x) => (x % 536870912) / 536870912; -const timeToRand = x => intSeedToRand(timeToIntSeed(x)); +const timeToRand = (x) => intSeedToRand(timeToIntSeed(x)); const timeToRandsPrime = (seed, n) => { const result = []; @@ -53,9 +52,13 @@ const timeToRandsPrime = (seed, n) => { result.push(intSeedToRand(seed)); seed = xorwise(seed); } - return(result); -} + return result; +}; -const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n) +const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n); -export const rand = signal(timeToRand) +export const rand = signal(timeToRand); + +export const _brandBy = (p) => rand.fmap((x) => x < p); +export const brandBy = (pPat) => reify(pPat).fmap(_brandBy).innerJoin(); +export const brand = _brandBy(0.5);