packages feed

relude-0.3.0 / src / Relude / Container / One.hs

{-# LANGUAGE FlexibleContexts  #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE TypeFamilies      #-}{- |Copyright: (c) 2016 Stephen Diehl           (c) 20016-2018 Serokell           (c) 2018 KowainikLicense:    MITMaintainer: Kowainik <xrom.xkov@gmail.com>Typeclass for creating structures from singleton element.-}module Relude.Container.One       ( One (..)       ) whereimport Relude.Base (Char, Int, Word8)import Relude.Container.Reexport (HashMap, HashSet, Hashable, IntMap, IntSet, Map, Set, uncurry)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.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.Base (Int, String)-- >>> import Relude.Bool (Bool (..))-- >>> import Relude.String (Text)-- >>> import qualified Data.HashMap.Strict as HashMap{- | Typeclass for data types that can be created from one element.>>> one True :: [Bool][True]>>> one 'a' :: Text"a">>> one (3, "hello") :: HashMap Int StringfromList [(3,"hello")]-}class One x where    -- | Type of single element of the structure.    type OneItem x    -- | Create a list, map, 'Text', etc from a single element.    one :: OneItem x -> x-- Listsinstance One [a] where    type OneItem [a] = a    one = (:[])    {-# INLINE one #-}instance One (NE.NonEmpty a) where    type OneItem (NE.NonEmpty a) = a    one = (NE.:|[])    {-# INLINE one #-}instance One (SEQ.Seq a) where    type OneItem (SEQ.Seq a) = a    one = (SEQ.empty SEQ.|>)    {-# INLINE one #-}-- Monomorphic sequencesinstance One T.Text where    type OneItem T.Text = Char    one = T.singleton    {-# INLINE one #-}instance One TL.Text where    type OneItem TL.Text = Char    one = TL.singleton    {-# INLINE one #-}instance One BS.ByteString where    type OneItem BS.ByteString = Word8    one = BS.singleton    {-# INLINE one #-}instance One BSL.ByteString where    type OneItem BSL.ByteString = Word8    one = BSL.singleton    {-# INLINE one #-}-- Mapsinstance One (Map k v) where    type OneItem (Map k v) = (k, v)    one = uncurry M.singleton    {-# INLINE one #-}instance Hashable k => One (HashMap k v) where    type OneItem (HashMap k v) = (k, v)    one = uncurry HM.singleton    {-# INLINE one #-}instance One (IntMap v) where    type OneItem (IntMap v) = (Int, v)    one = uncurry IM.singleton    {-# INLINE one #-}-- Setsinstance One (Set v) where    type OneItem (Set v) = v    one = Set.singleton    {-# INLINE one #-}instance Hashable v => One (HashSet v) where    type OneItem (HashSet v) = v    one = HashSet.singleton    {-# INLINE one #-}instance One IntSet where    type OneItem IntSet = Int    one = IS.singleton    {-# INLINE one #-}