relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Safe
Language | Haskell2010

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

- class One x where
  - type OneItem x
  - one :: OneItem x -> x

# Documentation

class One x where #

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 #

Type of a single element of the structure.

Methods

one :: OneItem x -> x #

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

#### Instances

Instances details

One ByteString # | 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 |
Instance details Defined in Relude.Container.One type OneItem ByteString = Word8
Methods one :: OneItem ByteString -> ByteString #
One ByteString # | 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 |
Instance details Defined in Relude.Container.One type OneItem ByteString = Word8
Methods one :: OneItem ByteString -> ByteString #
One ShortByteString # | Create singleton `ShortByteString`.
    >>> one 97 :: ShortByteString
    "a"
    law> length (one x) ≡ 1
Instance details Defined in Relude.Container.One Associated Types
type OneItem ShortByteString |
Instance details Defined in Relude.Container.One type OneItem ShortByteString = Word8
Methods one :: OneItem ShortByteString -> ShortByteString #
One IntSet # | 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 |
Instance details Defined in Relude.Container.One type OneItem IntSet = Int
Methods one :: OneItem IntSet -> IntSet #
One Text # | 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 |
Instance details Defined in Relude.Container.One type OneItem Text = Char
Methods one :: OneItem Text -> Text #
One Text # | 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 |
Instance details Defined in Relude.Container.One type OneItem Text = Char
Methods one :: OneItem Text -> Text #
One (IntMap v) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (IntMap v) = (Int, v)
Methods one :: OneItem (IntMap v) -> IntMap v #
One (Seq a) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (Seq a) = a
Methods one :: OneItem (Seq a) -> Seq a #
One (Set a) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (Set a) = a
Methods one :: OneItem (Set a) -> Set a #
One (NonEmpty a) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (NonEmpty a) = a
Methods one :: OneItem (NonEmpty a) -> NonEmpty a #
Hashable a => One (HashSet a) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (HashSet a) = a
Methods one :: OneItem (HashSet a) -> HashSet a #
One [a] # | 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] |
Instance details Defined in Relude.Container.One type OneItem [a] = a
Methods one :: OneItem [a] -> [a] #
One (Map k v) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (Map k v) = (k, v)
Methods one :: OneItem (Map k v) -> Map k v #
Hashable k => One (HashMap k v) # | 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) |
Instance details Defined in Relude.Container.One type OneItem (HashMap k v) = (k, v)
Methods one :: OneItem (HashMap k v) -> HashMap k v #