Shuffle and pick elements from arrays.
spago install shuffle
You can shuffle an array and the result will be contained inside an Effect
:
shuffle ∷ ∀ a. Array a → Effect (Array a)
import Effect.Shuffle (shuffle)
shuffle ["a", "b", "c"]
["b", "c", "a"]
Often when you want pick an element from an array at random the underlying type will be a Monoid
. This means that if the array is empty there is an suitable fallback:
pick ∷ ∀ a. Monoid a ⇒ Array a → Effect a
import Effect.Shuffle (pick)
pick ["a", "b", "c"]
"b"
pick []
""
Alternatively you can provide a fallback directly with pickOr
:
pickOr ∷ ∀ a. a → Array a → Effect a
import Effect.Shuffle (pickOr)
pickOr 0 [1, 2, 3]
2
pickOr 0 []
0
If you want to explicitly receive a Maybe
you can use pickMaybe
:
pickMaybe ∷ ∀ a. Array a → Effect (Maybe a)
import Effect.Shuffle (pickMaybe)
pickMaybe ["a", "b", "c"]
Just "b"
pickMaybe []
Nothing
If your array is non-empty then you can use pickNonEmpty
:
pickNonEmpty ∷ ∀ a. NonEmptyArray a → Effect a
import Effect.Shuffle (pickNonEmpty)
pickNonEmpty (cons' "a" [ "b", "c" ])
"b"
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!