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

Relude.Extra.Newtype

Description

Functions to ease work with newtypes.

Since: 0.2.0

Synopsis

Documentation

un :: forall a n. Coercible a n => n -> a Source #

Unwraps value from newtype.

>>> newtype Size = Size Int deriving Show
>>> un @Int (Size 5)
5
>>> un (Size 5) == length ['a', 'x', 'b']
False

Since: 0.2.0

wrap :: forall n a. Coercible a n => a -> n Source #

Wraps value to newtype. Behaves exactly as un but has more meaningful name in case you need to convert some value to newtype.

>>> newtype Flag = Flag Bool deriving (Show, Eq)
>>> wrap False == Flag True
False

Since: 0.2.0

under :: forall n a. Coercible a n => (n -> n) -> a -> a Source #

Applies function to the content of newtype. This function is not supposed to be used on newtypes that are created with the help of smart constructors.

>>> newtype Foo = Foo Bool deriving Show
>>> under not (Foo True)
Foo False
>>> newtype Bar = Bar String deriving Show
>>> under (filter (== 'a')) (Bar "abacaba")
Bar "aaaa"

Since: 0.2.0

under2 :: forall n a. Coercible a n => (n -> n -> n) -> a -> a -> a Source #

Lift binary function for newtypes to work over underlying newtype representation.

>>> under2 @(Sum Int) (<>) (3 :: Int) 4
7
>>> under2 @All (<>) True False
False

Since: 0.3.0

underF2 :: forall n a. Coercible a (n a) => (n a -> n a -> n a) -> a -> a -> a Source #

Version of under2 that works on newtypes parametrized by their representation. Provided for convenience.

>>> underF2 @Sum (<>) (3 :: Int) 4
7
>>> underF2 @Max (<>) 'p' 't'
't'

Since: 0.3.0

(#.) :: Coercible b c => (b -> c) -> (a -> b) -> a -> c Source #

Coercible composition. This function allows to write more efficient implementations of function compositions over newtypes.

Since: 0.3.0