This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0 1.0.1

Random.Maybe

List of Maybe Generators

Generators

maybe : Generator a -> Generator (Maybe a)

Generate a Maybe from a generator. Will generate Nothings 50% of the time.

withDefault : a -> Generator (Maybe a) -> Generator a

Generate values from a maybe generator or a default value.

withDefaultGenerator : Generator a -> Generator (Maybe a) -> Generator a

Generate values from a maybe generator or a default generator.

module Random.Maybe exposing (..)

{-| List of Maybe Generators

# Generators
@docs maybe, withDefault, withDefaultGenerator

-}

import Random exposing (Generator, map)
import Random.Extra exposing (constant, frequency, flatMap)
import Maybe


{-| Generate a Maybe from a generator. Will generate Nothings 50% of the time.
-}
maybe : Generator a -> Generator (Maybe a)
maybe generator =
    frequency
        [ ( 1, constant Nothing )
        , ( 1, map Just generator )
        ]
        (constant Nothing)


{-| Generate values from a maybe generator or a default value.
-}
withDefault : a -> Generator (Maybe a) -> Generator a
withDefault value generator =
    map (Maybe.withDefault value) generator


{-| Generate values from a maybe generator or a default generator.
-}
withDefaultGenerator : Generator a -> Generator (Maybe a) -> Generator a
withDefaultGenerator default generator =
    flatMap (flip withDefault generator) default