Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace gen

Index

Model Type aliases

Gen<T>: State<GenState, T>

The random generator monad

since

0.1.0

Type parameters

  • T

GenState: { newSeed: Seed; size: Size }

The state of the random generator monad.

since

0.1.0

Type declaration

  • newSeed: Seed
  • size: Size
Size: number

The meaning of size depends on the particular generator used.

since

0.1.0

constants Variables

seedMax: number
since

1.0.0

seedMin: number
since

1.0.0

instances Variables

Applicative: Applicative2<URI>
since

2.7.0

Functor: Functor2<URI>
since

2.7.0

Monad: Monad2<URI>
since

2.7.0

Constructors Functions

  • arrayOf<T>(gen: Gen<T>): Gen<T[]>
  • Generates a pseudo random array

    since

    0.1.0

    example

    import { mkSeed, generateSample, arrayOf, int } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( arrayOf(int()),

      generateSample({ count: 10, size: 5, seed: mkSeed(42) })
    ),
    [
    [27],
    [22, -14, 73],
    [-84, -13, 50],
    [-6, 16, -62, 76],
    [-44, 96],
    [48, 0],
    [],
    [23, 75, -63, -71, 64],
    [],
    [-83],
    ]

    );

    Type parameters

    • T

    Parameters

    Returns Gen<T[]>

  • A pseudo random boolean

    since

    0.1.0

    example

    import { mkSeed, generateSample, boolean } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( boolean,

      generateSample({ seed: mkSeed(42) })
    ),
    [true, true, true, true, true, false, true, true, false, false]

    );

    Parameters

    Returns [boolean, GenState]

  • char(__namedParameters?: { from?: string; to?: string }): Gen<string>
  • A pseudo random character

    since

    0.1.0

    example

    import { mkSeed, generateSample, char } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( char(),

      generateSample({ count: 20, seed: mkSeed(42) })
    ),
    ['K', '}', 'l', 'i', 'C', ':', 'n', 'o', 'q', '0', '{', 'h', '}', 'I', '=', 'o', '<', 'U', 'Z', ';']

    );

    assert.deepStrictEqual( pipe( char({ from: 'a', to: 'z' }),

      generateSample({ count: 20, seed: mkSeed(42) })
    ),
    ['r', 'v', 'l', 'f', 'p', 'i', 'n', 'b', 'k', 's', 'w', 'a', 'j', 'e', 'b', 'q', 'p', 'w', 'a', 'm']

    );

    Parameters

    • Optional __namedParameters: { from?: string; to?: string }
      • Optional from?: string
      • Optional to?: string

    Returns Gen<string>

  • float(__namedParameters?: { max?: number; min?: number }): Gen<number>
  • Generates a pseudo random float in a given interval

    since

    0.1.0

    example

    import { mkSeed, generateSample, float } from '@no-day/fp-ts-generators'; import * as gen from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    const formatFloat = (digits: number) => (n: number) => Math.round(n * 10 ** digits) / 10 ** digits;

    assert.deepStrictEqual( pipe( float({ min: -10, max: 10 }), gen.map(formatFloat(4)),

      generateSample({ count: 10, seed: mkSeed(42) })
    ),
    [-10, -9.9807, 3.1279, 7.1632, -3.2143, 2.4419, -6.8668, -7.1208, -7.7128, -3.9007]

    );

    Parameters

    • Optional __namedParameters: { max?: number; min?: number }
      • Optional max?: number
      • Optional min?: number

    Returns Gen<number>

  • int(__namedParameters?: { max?: number; min?: number }): Gen<number>
  • Generates a pseudo random integer in a given interval

    since

    0.1.0

    example

    import { mkSeed, generateSample, int } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( int({ min: -10, max: 10 }),

      generateSample({ count: 10, seed: mkSeed(42) })
    ),
    [-9, 3, 8, -2, -2, -8, -4, 3, -7, -10]

    );

    Parameters

    • Optional __namedParameters: { max?: number; min?: number }
      • Optional max?: number
      • Optional min?: number

    Returns Gen<number>

  • A random generator which simply outputs the current seed.

    since

    0.1.0

    example

    import { mkSeed, generateSample, lcgStep } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( lcgStep,

      generateSample({ count: 4, seed: mkSeed(42) })
    ),
    [43, 2075653, 1409598201, 1842888923]

    );

    Parameters

    Returns [number, GenState]

  • oneOf<T>(gens: NonEmptyArray<Gen<T>>): Gen<T>
  • Create a random generator which selects and executes a random generator from a non-empty array of random generators with uniform probability.

    since

    0.1.0

    example

    import { mkSeed, generateSample, oneOf, int } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( oneOf([int({ min: 10, max: 20 }), int({ min: 50, max: 60 })]),

      generateSample({ count: 6, seed: mkSeed(42) })
    ),
    [58, 57, 55, 60, 12, 10]

    );

    Type parameters

    • T

    Parameters

    • gens: NonEmptyArray<Gen<T>>

    Returns Gen<T>

  • recordOf<E, NER>(r: (keyof NER extends never ? never : NER) & Record<string, State<E, any>>): State<E, { [ K in string | number | symbol]: [NER[K]] extends [State<any, A>] ? A : never }>
  • Generates a pseudo random record if generators are provided for each field

    since

    0.1.0

    example

    import { mkSeed, generateSample, recordOf, boolean, int } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( recordOf({ bar: boolean, baz: int(), foo: int() }),

      generateSample({ count: 4, seed: mkSeed(42) })
    ),
    [
    {
    bar: true,
    baz: 27,
    foo: -25,
    },
    {
    bar: true,
    baz: -14,
    foo: 73,
    },
    {
    bar: true,
    baz: -84,
    foo: -13,
    },
    {
    bar: false,
    baz: 36,
    foo: -6,
    },
    ]

    );

    Type parameters

    • E

    • NER: Record<string, State<E, any>>

    Parameters

    • r: (keyof NER extends never ? never : NER) & Record<string, State<E, any>>

    Returns State<E, { [ K in string | number | symbol]: [NER[K]] extends [State<any, A>] ? A : never }>

  • string(__namedParameters?: { from?: string; to?: string }): Gen<string>
  • A pseudo random string

    since

    0.1.0

    example

    import { mkSeed, generateSample, string } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( string({ from: 'a', to: 'z' }),

      generateSample({ count: 10, seed: mkSeed(42) })
    ),

    ['vlfpinbksw', '', 'ebqpwa', 'uknubf', 'lq', 'jflq', 'fehcuxoqm', 'lsug', 'bat', 't']

    );

    Parameters

    • Optional __namedParameters: { from?: string; to?: string }
      • Optional from?: string
      • Optional to?: string

    Returns Gen<string>

  • tupleOf<E, T>(...t: T & { 0: State<E, any> }): State<E, { [ K in string | number | symbol]: [T[K]] extends [State<E, A>] ? A : never }>
  • Generates a pseudo random tuple if generators are provided for each position

    since

    0.1.0

    example

    import { mkSeed, generateSample, tupleOf, int, boolean } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( tupleOf(int(), boolean),

      generateSample({ count: 4, seed: mkSeed(42) })
    ),
    [
    [-57, true],
    [-25, true],
    [-14, false],
    [-64, true],
    ]

    );

    Type parameters

    • E

    • T: State<E, any>[]

    Parameters

    • Rest ...t: T & { 0: State<E, any> }

    Returns State<E, { [ K in string | number | symbol]: [T[K]] extends [State<E, A>] ? A : never }>

  • uniform<T>(): Gen<number>
  • A random generator which approximates a uniform random variable on [0, 1]

    since

    0.1.0

    example

    import { mkSeed, generateSample, uniform } from '@no-day/fp-ts-generators'; import * as gen from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    const formatFloat = (digits: number) => (n: number) => Math.round(n * 10 ** digits) / 10 ** digits;

    assert.deepStrictEqual( pipe( uniform(), gen.map(formatFloat(4)),

      generateSample({ count: 10, seed: mkSeed(42) })
    ),
    [0, 0.001, 0.6564, 0.8582, 0.3393, 0.6221, 0.1567, 0.144, 0.1144, 0.305]

    );

    Type parameters

    • T

    Returns Gen<number>

  • vectorOf(size: number): <T>(gen: Gen<T>) => Gen<T[]>
  • Generates a pseudo random array of a fixed size

    since

    0.1.0

    example

    import { mkSeed, generateSample, vectorOf, int } from '@no-day/fp-ts-generators'; import { pipe } from 'fp-ts/function';

    assert.deepStrictEqual( pipe( vectorOf(6)(int()),

      generateSample({ count: 4, seed: mkSeed(42) })
    ),
    [
    [-57, 27, -25, 22, -14, 73],
    [-64, -84, -13, 50, 36, -6],
    [16, -62, 76, -8, -44, 96],
    [88, 48, 0, -37, -53, 23],
    ]

    );

    Parameters

    • size: number

    Returns <T>(gen: Gen<T>) => Gen<T[]>

      • Type parameters

        • T

        Parameters

        Returns Gen<T[]>

Destructors Functions

  • evalGen<S>(s: S): <A>(ma: State<S, A>) => A
  • Run a random generator

    since

    0.1.0

    Type parameters

    • S

    Parameters

    • s: S

    Returns <A>(ma: State<S, A>) => A

      • <A>(ma: State<S, A>): A
      • Type parameters

        • A

        Parameters

        • ma: State<S, A>

        Returns A

  • generate(opts: { seed: Seed; size?: number }): <T>(gen: Gen<T>) => T
  • Run a random generator with a given seed and size.

    since

    0.1.0

    Parameters

    • opts: { seed: Seed; size?: number }
      • seed: Seed
      • Optional size?: number

    Returns <T>(gen: Gen<T>) => T

      • <T>(gen: Gen<T>): T
      • Type parameters

        • T

        Parameters

        Returns T

  • generateSample(opts: { count?: number; seed: Seed; size?: number }): <T>(gen: Gen<T>) => T[]
  • Run a random generator with a given seed and size. Produces an array of results, configured by count.

    since

    0.1.0

    Parameters

    • opts: { count?: number; seed: Seed; size?: number }
      • Optional count?: number
      • seed: Seed
      • Optional size?: number

    Returns <T>(gen: Gen<T>) => T[]

      • <T>(gen: Gen<T>): T[]
      • Type parameters

        • T

        Parameters

        Returns T[]

Monad Functions

  • chain<E, A, B>(f: (a: A) => State<E, B>): (ma: State<E, A>) => State<E, B>
  • Composes computations in sequence, using the return value of one computation to determine the next computation.

    since

    2.0.0

    Type parameters

    • E

    • A

    • B

    Parameters

    • f: (a: A) => State<E, B>
        • (a: A): State<E, B>
        • Parameters

          • a: A

          Returns State<E, B>

    Returns (ma: State<E, A>) => State<E, B>

      • (ma: State<E, A>): State<E, B>
      • Parameters

        • ma: State<E, A>

        Returns State<E, B>

Other Functions

  • ap<A>(fa: Gen<A>): <B>(fab: Gen<(a: A) => B>) => Gen<B>
  • Type parameters

    • A

    Parameters

    Returns <B>(fab: Gen<(a: A) => B>) => Gen<B>

      • <B>(fab: Gen<(a: A) => B>): Gen<B>
      • Type parameters

        • B

        Parameters

        • fab: Gen<(a: A) => B>

        Returns Gen<B>

  • bind<N, A, E, B>(name: Exclude<N, keyof A>, f: (a: A) => State<E, B>): (ma: State<E, A>) => State<E, { readonly [ K in string | number | symbol]: K extends keyof A ? A[K] : B }>
  • since

    2.8.0

    Type parameters

    • N: string

    • A

    • E

    • B

    Parameters

    • name: Exclude<N, keyof A>
    • f: (a: A) => State<E, B>
        • (a: A): State<E, B>
        • Parameters

          • a: A

          Returns State<E, B>

    Returns (ma: State<E, A>) => State<E, { readonly [ K in string | number | symbol]: K extends keyof A ? A[K] : B }>

      • (ma: State<E, A>): State<E, { readonly [ K in string | number | symbol]: K extends keyof A ? A[K] : B }>
      • Parameters

        • ma: State<E, A>

        Returns State<E, { readonly [ K in string | number | symbol]: K extends keyof A ? A[K] : B }>

  • bindTo<N>(name: N): <E, A>(fa: State<E, A>) => State<E, { readonly [ K in string]: A }>
  • since

    2.8.0

    Type parameters

    • N: string

    Parameters

    • name: N

    Returns <E, A>(fa: State<E, A>) => State<E, { readonly [ K in string]: A }>

      • <E, A>(fa: State<E, A>): State<E, { readonly [ K in string]: A }>
      • Type parameters

        • E

        • A

        Parameters

        • fa: State<E, A>

        Returns State<E, { readonly [ K in string]: A }>

  • chainFirst<A, E, B>(f: (a: A) => State<E, B>): (first: State<E, A>) => State<E, A>
  • Type parameters

    • A

    • E

    • B

    Parameters

    • f: (a: A) => State<E, B>
        • (a: A): State<E, B>
        • Parameters

          • a: A

          Returns State<E, B>

    Returns (first: State<E, A>) => State<E, A>

      • (first: State<E, A>): State<E, A>
      • Parameters

        • first: State<E, A>

        Returns State<E, A>

  • filter<A, B>(f: Predicate<A> | Refinement<A, B>): (fa: Gen<A>) => Gen<B>
  • Type parameters

    • A

    • B

    Parameters

    • f: Predicate<A> | Refinement<A, B>

    Returns (fa: Gen<A>) => Gen<B>

  • lazy<A>(lazy: Lazy<Gen<A>>): Gen<A>
  • Type parameters

    • A

    Parameters

    • lazy: Lazy<Gen<A>>

    Returns Gen<A>

  • map<A, B>(f: (a: A) => B): (fa: Gen<A>) => Gen<B>
  • Type parameters

    • A

    • B

    Parameters

    • f: (a: A) => B
        • (a: A): B
        • Parameters

          • a: A

          Returns B

    Returns (fa: Gen<A>) => Gen<B>

  • Parameters

    Returns [void, GenState]

  • nullable<T>(arbitrary: Gen<T>): Gen<null | T>
  • Type parameters

    • T

    Parameters

    • arbitrary: Gen<T>

    Returns Gen<null | T>

  • of<A>(a: A): Gen<A>
  • Type parameters

    • A

    Parameters

    • a: A

    Returns Gen<A>

  • union<T>(...gens: { readonly [ P in string | number | symbol]: Gen<T[P]> }): Gen<T[number]>
  • Type parameters

    • T: readonly [unknown, unknown, T]

    Parameters

    • Rest ...gens: { readonly [ P in string | number | symbol]: Gen<T[P]> }

    Returns Gen<T[number]>

  • variant(seed: number): Gen<void>
  • Parameters

    • seed: number

    Returns Gen<void>

constructors Functions

  • mkSeed(n: number): Seed
  • Creates a new Seed. Any number can be given as it will be rounded and overflows are wrapped internally.

    since

    1.0.0

    Parameters

    • n: number

    Returns Seed

Generated using TypeDoc