{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE Safe #-}{-# LANGUAGE TypeFamilies #-}{- |Module : Relude.Container.OneCopyright : (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 KowainikSPDX-License-Identifier : MITMaintainer : Kowainik <xrom.xkov@gmail.com>Stability : StablePortability : Portable'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 'T.Text' or 'IntSet'.3. Give a clearer and less scary name for cases where you can use 'Relude.pure' or @(:[])@.@since 0.1.0-}module Relude.Container.One ( One (..) ) whereimport Relude.Base (Char)import Relude.Container.Reexport (HashMap, HashSet, Hashable, IntMap, IntSet, Map, Set, uncurry)import Relude.Numeric (Int, Word8)import qualified Data.List.NonEmpty as NEimport qualified Data.Sequence as SEQimport qualified Data.ByteString as BSimport qualified Data.ByteString.Lazy as BSLimport qualified Data.ByteString.Short as SBSimport qualified Data.Text as Timport qualified Data.Text.Lazy as TLimport qualified Data.HashMap.Strict as HMimport qualified Data.HashSet as HashSetimport qualified Data.IntMap as IMimport qualified Data.IntSet as ISimport qualified Data.Map as Mimport qualified Data.Set as Set-- $setup-- >>> import Relude{- | 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 StringfromList [(3,"hello")]__Laws:__* __@single-size@__: @∀ x . size (one x) ≡ 1@(where @size@ is a specific function for each container that returns the size ofthis container)-}class One x where -- | Type of a single element of the structure. type OneItem x -- | Create a list, map, 'T.Text', etc from a single element. one :: OneItem x -> x-- Lists{- | Allows to create a singleton list. You might prefer function with name 'one'instead of 'Relude.pure' or @(:[])@.>>> one 42 :: [Int][42]@law> 'Relude.length' ('one' @[a] x) ≡ 1@-}instance One [a] where type OneItem [a] = a one :: a -> [a] one = (:[]) {-# INLINE one #-}{- | Allows to create singleton 'NE.NonEmpty' list. You might prefer function withname 'one' instead of 'Relude.pure' or @(:|[])@.>>> one 42 :: NonEmpty Int42 :| []@law> 'Relude.length' ('one' @('NE.NonEmpty' a) x) ≡ 1@-}instance One (NE.NonEmpty a) where type OneItem (NE.NonEmpty a) = a one :: a -> NE.NonEmpty a one = (NE.:|[]) {-# INLINE one #-}{- | Create singleton 'SEQ.Seq'.>>> one 42 :: Seq IntfromList [42]@law> 'Relude.length' ('one' @('SEQ.Seq' a) x) ≡ 1@-}instance One (SEQ.Seq a) where type OneItem (SEQ.Seq a) = a one :: a -> SEQ.Seq a one = SEQ.singleton {-# INLINE one #-}-- Monomorphic sequences{- | Create singleton strict 'T.Text'.>>> one 'a' :: Text"a"@law> 'Data.Text.length' ('one' x) ≡ 1@-}instance One T.Text where type OneItem T.Text = Char one :: Char -> T.Text one = T.singleton {-# INLINE one #-}{- | Create singleton lazy 'TL.Text'.>>> one 'a' :: LText"a"@law> 'Data.Text.Lazy.length' ('one' x) ≡ 1@-}instance One TL.Text where type OneItem TL.Text = Char one :: Char -> TL.Text one = TL.singleton {-# INLINE one #-}{- | Create singleton strict 'BS.ByteString'.>>> one 97 :: ByteString"a"@law> 'BS.length' ('one' x) ≡ 1@-}instance One BS.ByteString where type OneItem BS.ByteString = Word8 one :: Word8 -> BS.ByteString one = BS.singleton {-# INLINE one #-}{- | Create singleton lazy 'BSL.ByteString'.>>> one 97 :: LByteString"a"@law> 'BSL.length' ('one' x) ≡ 1@-}instance One BSL.ByteString where type OneItem BSL.ByteString = Word8 one :: Word8 -> BSL.ByteString one = BSL.singleton {-# INLINE one #-}{- | Create singleton 'SBS.ShortByteString'.>>> one 97 :: ShortByteString"a"@law> 'SBS.length' ('one' x) ≡ 1@-}instance One SBS.ShortByteString where type OneItem SBS.ShortByteString = Word8 one :: Word8 -> SBS.ShortByteString one x = SBS.pack [x] {-# INLINE one #-}-- Maps{- | Create singleton 'Map' from key-value pair.>>> one (3, "foo") :: Map Int TextfromList [(3,"foo")]@law> 'Relude.length' ('one' @('Map' k v) (k, v)) ≡ 1@-}instance One (Map k v) where type OneItem (Map k v) = (k, v) one :: (k, v) -> Map k v one = uncurry M.singleton {-# INLINE one #-}{- | Create singleton 'HashMap' from key-value pair.>>> one (3, "foo") :: HashMap Int TextfromList [(3,"foo")]@law> 'Relude.length' ('one' @('HashMap' k v) (k, v)) ≡ 1@-}instance Hashable k => One (HashMap k v) where type OneItem (HashMap k v) = (k, v) one :: (k, v) -> HashMap k v one = uncurry HM.singleton {-# INLINE one #-}{- | Create singleton 'IntMap' from key-value pair.>>> one (3, "foo") :: IntMap TextfromList [(3,"foo")]@law> 'Relude.length' ('one' @('IntMap' a) x) ≡ 1@-}instance One (IntMap v) where type OneItem (IntMap v) = (Int, v) one :: (Int, v) -> IntMap v one = uncurry IM.singleton {-# INLINE one #-}-- Sets{- | Create singleton 'Set'.>>> one 42 :: Set IntfromList [42]@law> 'Relude.length' ('one' @('Set' a) x) ≡ 1@-}instance One (Set a) where type OneItem (Set a) = a one :: a -> Set a one = Set.singleton {-# INLINE one #-}{- | Create singleton 'HashSet'.>>> one 42 :: HashSet IntfromList [42]@law> 'Relude.length' ('one' @('HashSet' a) x) ≡ 1@-}instance Hashable a => One (HashSet a) where type OneItem (HashSet a) = a one :: a -> HashSet a one = HashSet.singleton {-# INLINE one #-}{- | Create singleton 'IntSet'.>>> one 42 :: IntSetfromList [42]@law> 'Data.IntSet.size' ('one' x) ≡ 1@-}instance One IntSet where type OneItem IntSet = Int one :: Int -> IntSet one = IS.singleton {-# INLINE one #-}