Shuffle and pick elements from arrays.
spago install shuffle
You can shuffle an array and the result will be contained inside an Effect
:
shuffle ∷ ∀ f a. Foldable f ⇒ Unfoldable f ⇒ f a → Effect (f a)
import Effect.Shuffle (shuffle)
do
... <- shuffle ["a", "b", "c"]
-- ["b", "c", "a"]
You can similarly pick an element from an array at random:
pick ∷ ∀ f a. Foldable f ⇒ Unfoldable f ⇒ f a → Effect (Maybe a)
import Effect.Shuffle (pick)
do
... <- pick ["a", "b", "c"]
-- Just "b"
You can provide a fallback directly with pickOr
:
pickOr ∷ ∀ f a. Foldable f ⇒ Unfoldable f ⇒ a → f a → Effect a
import Effect.Shuffle (pickOr)
do
... <- pickOr "" ["a", "b", "c"]
-- "b"
If the underlying type is a Monoid
there is an suitable fallback automatically:
pickMonoid ∷ ∀ f a. Foldable f ⇒ Unfoldable f ⇒ Monoid a ⇒ f a → Effect a
import Effect.Shuffle (pickMonoid)
strings :: Array String
strings = []
do
... <- pickMonoid strings
-- ""
If you have a NonEmptyArray
you can pick an element without needing a fallback:
pickNonEmpty ∷ ∀ a. NonEmptyArray a → Effect a
import Data.Array.NonEmpty (singleton)
import Effect.Shuffle (pickNonEmpty)
do
... <- pickNonEmpty $ singleton "a"
-- "a"
Documentation and more detailed examples are hosted on Pursuit.
To install dependencies:
yarn install
yarn spago install
To run tests:
yarn spago test
To generate the documentation locally:
yarn spago docs
To run linters:
yarn lint
To run formatters:
yarn format
Please read this repository's Code of Conduct which outlines our collaboration standards and the Changelog for details on breaking changes that have been made.
This repository adheres to semantic versioning standards. For more information on semantic versioning visit SemVer.
Bump2version is used to version and tag changes. For example:
bump2version patch
- Joel Lefkowitz - Initial work
Lots of love to the open source community!