relude-1.0.0.0: Safe, performant, user-friendly and lightweight Haskell Standard Library
Copyright(c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2021 Kowainik
LicenseMIT
MaintainerKowainik <xrom.xkov@gmail.com>
StabilityStable
PortabilityPortable
Safe HaskellSafe
LanguageHaskell2010

Relude.Container.One

Description

One is a typeclass for creating structures from a singleton element. It has three main goals:

  1. Give a shorter name for the construction: uses one instead of common singleton.
  2. Work with monomorphic structures like Text or IntSet.
  3. Give a clearer and less scary name for cases where you can use pure or (:[]).

Since: 0.1.0

Synopsis

Documentation

class One x where Source #

Typeclass for data types that can be created from one element. E.g. lists, non-empty containers, maps.

>>> one True :: [Bool]
[True]
>>> one 'a' :: Text
"a"
>>> one (3, "hello") :: HashMap Int String
fromList [(3,"hello")]

Laws:

  • single-size: ∀ x . size (one x) ≡ 1

(where size is a specific function for each container that returns the size of this container)

Associated Types

type OneItem x Source #

Type of a single element of the structure.

Methods

one :: OneItem x -> x Source #

Create a list, map, Text, etc from a single element.

Instances

Instances details
One ShortByteString Source #

Create singleton ShortByteString.

>>> one 97 :: ShortByteString
"a"
law> length (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem ShortByteString Source #

One ByteString Source #

Create singleton lazy ByteString.

>>> one 97 :: LByteString
"a"
law> length (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem ByteString Source #

One ByteString Source #

Create singleton strict ByteString.

>>> one 97 :: ByteString
"a"
law> length (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem ByteString Source #

One IntSet Source #

Create singleton IntSet.

>>> one 42 :: IntSet
fromList [42]
law> size (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem IntSet Source #

One Text Source #

Create singleton lazy Text.

>>> one 'a' :: LText
"a"
law> length (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem Text Source #

Methods

one :: OneItem Text -> Text Source #

One Text Source #

Create singleton strict Text.

>>> one 'a' :: Text
"a"
law> length (one x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem Text Source #

Methods

one :: OneItem Text -> Text Source #

One [a] Source #

Allows to create a singleton list. You might prefer function with name one instead of pure or (:[]).

>>> one 42 :: [Int]
[42]
law> length (one @[a] x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem [a] Source #

Methods

one :: OneItem [a] -> [a] Source #

One (NonEmpty a) Source #

Allows to create singleton NonEmpty list. You might prefer function with name one instead of pure or (:|[]).

>>> one 42 :: NonEmpty Int
42 :| []
law> length (one @(NonEmpty a) x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (NonEmpty a) Source #

Methods

one :: OneItem (NonEmpty a) -> NonEmpty a Source #

One (IntMap v) Source #

Create singleton IntMap from key-value pair.

>>> one (3, "foo") :: IntMap Text
fromList [(3,"foo")]
law> length (one @(IntMap a) x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (IntMap v) Source #

Methods

one :: OneItem (IntMap v) -> IntMap v Source #

One (Seq a) Source #

Create singleton Seq.

>>> one 42 :: Seq Int
fromList [42]
law> length (one @(Seq a) x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (Seq a) Source #

Methods

one :: OneItem (Seq a) -> Seq a Source #

One (Set a) Source #

Create singleton Set.

>>> one 42 :: Set Int
fromList [42]
law> length (one @(Set a) x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (Set a) Source #

Methods

one :: OneItem (Set a) -> Set a Source #

Hashable a => One (HashSet a) Source #

Create singleton HashSet.

>>> one 42 :: HashSet Int
fromList [42]
law> length (one @(HashSet a) x) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (HashSet a) Source #

Methods

one :: OneItem (HashSet a) -> HashSet a Source #

One (Map k v) Source #

Create singleton Map from key-value pair.

>>> one (3, "foo") :: Map Int Text
fromList [(3,"foo")]
law> length (one @(Map k v) (k, v)) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (Map k v) Source #

Methods

one :: OneItem (Map k v) -> Map k v Source #

Hashable k => One (HashMap k v) Source #

Create singleton HashMap from key-value pair.

>>> one (3, "foo") :: HashMap Int Text
fromList [(3,"foo")]
law> length (one @(HashMap k v) (k, v)) ≡ 1
Instance details

Defined in Relude.Container.One

Associated Types

type OneItem (HashMap k v) Source #

Methods

one :: OneItem (HashMap k v) -> HashMap k v Source #