natural-0.5.0.1: Natural number
Safe HaskellNone
LanguageHaskell2010

Natural

Synopsis

Documentation

data Natural Source #

A natural number (>= 0) represented as a newtype over Integer.

>>> nat 0
Natural 0
>>> nat 5
Natural 5

Instances

Instances details
FromJSON Natural Source #

Parses a Natural from a JSON number, failing on negative values.

>>> decode "0" :: Maybe Natural
Just (Natural 0)
Instance details

Defined in Natural

ToJSON Natural Source #

Serialises a Natural to a JSON number.

>>> fromJSON (Number 0) :: Result Natural
Success (Natural 0)
>>> fromJSON (Number 42) :: Result Natural
Success (Natural 42)
>>> decode "42" :: Maybe Natural
Just (Natural 42)
>>> decode "0" :: Maybe Natural
Just (Natural 0)
>>> decode "-1" :: Maybe Natural
Nothing
Instance details

Defined in Natural

Monoid Natural Source #
>>> mempty :: Natural
Natural 0
Instance details

Defined in Natural

Semigroup Natural Source #
>>> nat 3 <> nat 4
Natural 7
>>> nat 0 <> nat 5
Natural 5
Instance details

Defined in Natural

Show Natural Source # 
Instance details

Defined in Natural

Eq Natural Source # 
Instance details

Defined in Natural

Methods

(==) :: Natural -> Natural -> Bool #

(/=) :: Natural -> Natural -> Bool #

Ord Natural Source # 
Instance details

Defined in Natural

AsNatural Natural Source #
>>> _Natural # nat 5 :: Natural
Natural 5
Instance details

Defined in Natural

AsPositive Natural Source #
>>> nat 5 ^? _Positive
Just (Positive 5)
>>> nat 0 ^? _Positive
Nothing
Instance details

Defined in Natural

HasNatural Natural Source #
>>> (nat 5) ^. natural
Natural 5
Instance details

Defined in Natural

class HasNatural a where Source #

Instances

Instances details
HasNatural MaxNatural Source #
>>> MaxNatural (nat 7) ^. natural
Natural 7
Instance details

Defined in Natural

HasNatural MinNatural Source #
>>> MinNatural (nat 3) ^. natural
Natural 3
Instance details

Defined in Natural

HasNatural Natural Source #
>>> (nat 5) ^. natural
Natural 5
Instance details

Defined in Natural

HasNatural ProductNatural Source #
>>> ProductNatural (nat 5) ^. natural
Natural 5
Instance details

Defined in Natural

class AsNatural a where Source #

Instances

Instances details
AsNatural MaxNatural Source #
>>> MaxNatural (nat 7) ^? _Natural
Just (Natural 7)
Instance details

Defined in Natural

AsNatural MinNatural Source #
>>> MinNatural (nat 3) ^? _Natural
Just (Natural 3)
Instance details

Defined in Natural

AsNatural Natural Source #
>>> _Natural # nat 5 :: Natural
Natural 5
Instance details

Defined in Natural

AsNatural ProductNatural Source #
>>> ProductNatural (nat 5) ^? _Natural
Just (Natural 5)
Instance details

Defined in Natural

AsNatural Integer Source #
>>> (42 :: Integer) ^? _Natural
Just (Natural 42)
>>> (-1 :: Integer) ^? _Natural
Nothing
Instance details

Defined in Natural

AsNatural Int Source #
>>> (5 :: Int) ^? _Natural
Just (Natural 5)
>>> (-1 :: Int) ^? _Natural
Nothing
Instance details

Defined in Natural

AsNatural Word Source #
>>> (7 :: Word) ^? _Natural
Just (Natural 7)
Instance details

Defined in Natural

Integral a => AsNatural (Identity a) Source #
>>> import Data.Functor.Identity(Identity(..))
>>> (Identity 5 :: Identity Integer) ^? _Natural
Just (Natural 5)
Instance details

Defined in Natural

Integral a => AsNatural (Const a b) Source #
>>> import Data.Functor.Identity(Identity(..))
>>> import Control.Applicative(Const(..))
>>> (Const 5 :: Const Integer Bool) ^? _Natural
Just (Natural 5)
Instance details

Defined in Natural

newtype ProductNatural Source #

>>> ProductNatural (nat 3) <> ProductNatural (nat 4)
ProductNatural (Natural 12)
>>> mempty :: ProductNatural
ProductNatural (Natural 1)

Constructors

ProductNatural Natural 

Instances

Instances details
FromJSON ProductNatural Source #

Parses a ProductNatural from a JSON number, failing on negative values.

>>> decode "12" :: Maybe ProductNatural
Just (ProductNatural (Natural 12))
>>> decode "-1" :: Maybe ProductNatural
Nothing
Instance details

Defined in Natural

ToJSON ProductNatural Source #

Serialises a ProductNatural to a JSON number.

>>> encode (ProductNatural (nat 12))
"12"
Instance details

Defined in Natural

Monoid ProductNatural Source #
>>> mempty :: ProductNatural
ProductNatural (Natural 1)
Instance details

Defined in Natural

Semigroup ProductNatural Source #
>>> ProductNatural (nat 3) <> ProductNatural (nat 4)
ProductNatural (Natural 12)
Instance details

Defined in Natural

Show ProductNatural Source # 
Instance details

Defined in Natural

Eq ProductNatural Source # 
Instance details

Defined in Natural

Ord ProductNatural Source # 
Instance details

Defined in Natural

Wrapped ProductNatural Source #
>>> ProductNatural (nat 5) ^. _Wrapped'
Natural 5
Instance details

Defined in Natural

Associated Types

type Unwrapped ProductNatural 
Instance details

Defined in Natural

AsNatural ProductNatural Source #
>>> ProductNatural (nat 5) ^? _Natural
Just (Natural 5)
Instance details

Defined in Natural

HasNatural ProductNatural Source #
>>> ProductNatural (nat 5) ^. natural
Natural 5
Instance details

Defined in Natural

ProductNatural ~ a => Rewrapped ProductNatural a Source # 
Instance details

Defined in Natural

type Unwrapped ProductNatural Source # 
Instance details

Defined in Natural

newtype MaxNatural Source #

>>> MaxNatural (nat 3) <> MaxNatural (nat 7)
MaxNatural (Natural 7)

Constructors

MaxNatural Natural 

Instances

Instances details
FromJSON MaxNatural Source #

Parses a MaxNatural from a JSON number, failing on negative values.

>>> decode "7" :: Maybe MaxNatural
Just (MaxNatural (Natural 7))
>>> decode "-1" :: Maybe MaxNatural
Nothing
Instance details

Defined in Natural

ToJSON MaxNatural Source #

Serialises a MaxNatural to a JSON number.

>>> encode (MaxNatural (nat 7))
"7"
Instance details

Defined in Natural

Semigroup MaxNatural Source #
>>> MaxNatural (nat 3) <> MaxNatural (nat 7)
MaxNatural (Natural 7)
Instance details

Defined in Natural

Show MaxNatural Source # 
Instance details

Defined in Natural

Eq MaxNatural Source # 
Instance details

Defined in Natural

Ord MaxNatural Source # 
Instance details

Defined in Natural

Wrapped MaxNatural Source #
>>> MaxNatural (nat 7) ^. _Wrapped'
Natural 7
Instance details

Defined in Natural

Associated Types

type Unwrapped MaxNatural 
Instance details

Defined in Natural

AsNatural MaxNatural Source #
>>> MaxNatural (nat 7) ^? _Natural
Just (Natural 7)
Instance details

Defined in Natural

HasNatural MaxNatural Source #
>>> MaxNatural (nat 7) ^. natural
Natural 7
Instance details

Defined in Natural

MaxNatural ~ a => Rewrapped MaxNatural a Source # 
Instance details

Defined in Natural

type Unwrapped MaxNatural Source # 
Instance details

Defined in Natural

newtype MinNatural Source #

>>> MinNatural (nat 3) <> MinNatural (nat 7)
MinNatural (Natural 3)

Constructors

MinNatural Natural 

Instances

Instances details
FromJSON MinNatural Source #

Parses a MinNatural from a JSON number, failing on negative values.

>>> decode "3" :: Maybe MinNatural
Just (MinNatural (Natural 3))
>>> decode "-1" :: Maybe MinNatural
Nothing
Instance details

Defined in Natural

ToJSON MinNatural Source #

Serialises a MinNatural to a JSON number.

>>> encode (MinNatural (nat 3))
"3"
Instance details

Defined in Natural

Semigroup MinNatural Source #
>>> MinNatural (nat 3) <> MinNatural (nat 7)
MinNatural (Natural 3)
Instance details

Defined in Natural

Show MinNatural Source # 
Instance details

Defined in Natural

Eq MinNatural Source # 
Instance details

Defined in Natural

Ord MinNatural Source # 
Instance details

Defined in Natural

Wrapped MinNatural Source #
>>> MinNatural (nat 3) ^. _Wrapped'
Natural 3
Instance details

Defined in Natural

Associated Types

type Unwrapped MinNatural 
Instance details

Defined in Natural

AsNatural MinNatural Source #
>>> MinNatural (nat 3) ^? _Natural
Just (Natural 3)
Instance details

Defined in Natural

HasNatural MinNatural Source #
>>> MinNatural (nat 3) ^. natural
Natural 3
Instance details

Defined in Natural

MinNatural ~ a => Rewrapped MinNatural a Source # 
Instance details

Defined in Natural

type Unwrapped MinNatural Source # 
Instance details

Defined in Natural

zero :: Prism' Natural () Source #

Prism matching zero.

>>> zero # ()
Natural 0
>>> nat 0 ^? zero
Just ()
>>> nat 3 ^? zero
Nothing

zero' :: Natural Source #

The zero natural number.

>>> zero'
Natural 0

successor :: Prism' Natural Natural Source #

Prism between a natural and its predecessor.

>>> successor # nat 0
Natural 1
>>> successor # nat 4
Natural 5
>>> nat 5 ^? successor
Just (Natural 4)
>>> nat 0 ^? successor
Nothing

successor' :: Natural -> Natural Source #

The successor of a natural number.

>>> successor' (nat 0)
Natural 1
>>> successor' (nat 4)
Natural 5

plus :: Natural -> Natural -> Natural Source #

Add two natural numbers.

>>> plus (nat 3) (nat 4)
Natural 7
>>> plus (nat 0) (nat 5)
Natural 5

multiply :: Natural -> Natural -> Natural Source #

Multiply two natural numbers.

>>> multiply (nat 3) (nat 4)
Natural 12
>>> multiply (nat 0) (nat 5)
Natural 0

power :: Natural -> Natural -> Natural Source #

Raise a natural to a natural power.

>>> power (nat 2) (nat 10)
Natural 1024
>>> power (nat 3) (nat 0)
Natural 1

zeroOr :: AsNatural a => a -> Natural Source #

Return the natural if the prism matches, otherwise zero.

>>> zeroOr (5 :: Integer)
Natural 5
>>> zeroOr (-1 :: Integer)
Natural 0

length :: Foldable f => f a -> Natural Source #

Count the elements in a foldable structure.

>>> length [1,2,3 :: Int]
Natural 3
>>> length ([] :: [Int])
Natural 0

replicate :: Natural -> a -> [a] Source #

Replicate a value a natural number of times.

>>> replicate (nat 3) 'x'
"xxx"
>>> replicate (nat 0) 'x'
""

take :: Natural -> [a] -> [a] Source #

Take the first n elements.

>>> take (nat 2) [1,2,3,4,5 :: Int]
[1,2]
>>> take (nat 0) [1,2,3 :: Int]
[]
>>> take (nat 5) [1,2 :: Int]
[1,2]

drop :: Natural -> [a] -> [a] Source #

Drop the first n elements.

>>> drop (nat 2) [1,2,3,4,5 :: Int]
[3,4,5]
>>> drop (nat 0) [1,2,3 :: Int]
[1,2,3]
>>> drop (nat 5) [1,2 :: Int]
[]

splitAt :: Natural -> [a] -> ([a], [a]) Source #

Split a list at position n.

>>> splitAt (nat 2) [1,2,3,4,5 :: Int]
([1,2],[3,4,5])

(!!) :: [a] -> Natural -> Maybe a Source #

Index into a list.

>>> [10,20,30 :: Int] !! nat 0
Just 10
>>> [10,20,30 :: Int] !! nat 2
Just 30
>>> [10,20,30 :: Int] !! nat 5
Nothing
>>> ([] :: [Int]) !! nat 0
Nothing

findIndices :: (a -> Bool) -> [a] -> [Natural] Source #

Find all indices where the predicate holds.

>>> findIndices (== 'a') "abacad"
[Natural 0,Natural 2,Natural 4]
>>> findIndices (== 'z') "abacad"
[]

findIndex :: (a -> Bool) -> [a] -> Maybe Natural Source #

Find the first index where the predicate holds.

>>> findIndex (== 'c') "abcde"
Just (Natural 2)
>>> findIndex (== 'z') "abcde"
Nothing

elemIndices :: Eq a => a -> [a] -> [Natural] Source #

Find all indices of a given element.

>>> elemIndices 'a' "banana"
[Natural 1,Natural 3,Natural 5]

elemIndex :: Eq a => a -> [a] -> Maybe Natural Source #

Find the first index of a given element.

>>> elemIndex 'n' "banana"
Just (Natural 2)
>>> elemIndex 'z' "banana"
Nothing

minus :: Natural -> Natural -> Natural Source #

Subtract two naturals, flooring at zero.

>>> minus (nat 5) (nat 3)
Natural 2
>>> minus (nat 3) (nat 5)
Natural 0
>>> minus (nat 3) (nat 3)
Natural 0

list :: Iso' Natural [()] Source #

Iso between a natural and a list of units.

>>> nat 3 ^. list
[(),(),()]
>>> length (nat 3 ^. list)
Natural 3

data Positive Source #

A positive integer (>= 1). Semigroup is multiplication.

>>> pos 3
Positive 3
>>> pos 3 <> pos 4
Positive 12

Instances

Instances details
FromJSON Positive Source #

Parses a Positive from a JSON number, failing on non-positive values.

>>> decode "1" :: Maybe Positive
Just (Positive 1)
Instance details

Defined in Natural

ToJSON Positive Source #

Serialises a Positive to a JSON number.

>>> fromJSON (Number 1) :: Result Positive
Success (Positive 1)
>>> fromJSON (Number 42) :: Result Positive
Success (Positive 42)
>>> decode "42" :: Maybe Positive
Just (Positive 42)
>>> decode "1" :: Maybe Positive
Just (Positive 1)
>>> decode "0" :: Maybe Positive
Nothing
>>> decode "-1" :: Maybe Positive
Nothing
Instance details

Defined in Natural

Semigroup Positive Source #
>>> pos 3 <> pos 4
Positive 12
Instance details

Defined in Natural

Show Positive Source # 
Instance details

Defined in Natural

Eq Positive Source # 
Instance details

Defined in Natural

Ord Positive Source # 
Instance details

Defined in Natural

AsPositive Positive Source #
>>> _Positive # pos 5 :: Positive
Positive 5
Instance details

Defined in Natural

HasPositive Positive Source #
>>> pos 5 ^. positive
Positive 5
Instance details

Defined in Natural

class HasPositive a where Source #

Instances

Instances details
HasPositive MaxPositive Source #
>>> MaxPositive (pos 7) ^. positive
Positive 7
Instance details

Defined in Natural

HasPositive MinPositive Source #
>>> MinPositive (pos 3) ^. positive
Positive 3
Instance details

Defined in Natural

HasPositive Positive Source #
>>> pos 5 ^. positive
Positive 5
Instance details

Defined in Natural

HasPositive SumPositive Source #
>>> SumPositive (pos 5) ^. positive
Positive 5
Instance details

Defined in Natural

class AsPositive a where Source #

Instances

Instances details
AsPositive MaxPositive Source #
>>> MaxPositive (pos 7) ^? _Positive
Just (Positive 7)
Instance details

Defined in Natural

AsPositive MinPositive Source #
>>> MinPositive (pos 3) ^? _Positive
Just (Positive 3)
Instance details

Defined in Natural

AsPositive Natural Source #
>>> nat 5 ^? _Positive
Just (Positive 5)
>>> nat 0 ^? _Positive
Nothing
Instance details

Defined in Natural

AsPositive NotZero Source #

Prism from NotZero to Positive (matches only positive values).

>>> (NotZero True (pos 5)) ^? _Positive
Just (Positive 5)
>>> (NotZero False (pos 5)) ^? _Positive
Nothing
Instance details

Defined in Natural

AsPositive Positive Source #
>>> _Positive # pos 5 :: Positive
Positive 5
Instance details

Defined in Natural

AsPositive SumPositive Source #
>>> SumPositive (pos 5) ^? _Positive
Just (Positive 5)
Instance details

Defined in Natural

AsPositive Integer Source #
>>> (42 :: Integer) ^? _Positive
Just (Positive 42)
>>> (0 :: Integer) ^? _Positive
Nothing
Instance details

Defined in Natural

AsPositive Int Source #
>>> (5 :: Int) ^? _Positive
Just (Positive 5)
>>> (0 :: Int) ^? _Positive
Nothing
Instance details

Defined in Natural

AsPositive Word Source #
>>> (7 :: Word) ^? _Positive
Just (Positive 7)
>>> (0 :: Word) ^? _Positive
Nothing
Instance details

Defined in Natural

Integral a => AsPositive (Identity a) Source #
>>> import Data.Functor.Identity(Identity(..))
>>> (Identity 5 :: Identity Integer) ^? _Positive
Just (Positive 5)
Instance details

Defined in Natural

Integral a => AsPositive (Const a b) Source #
>>> import Control.Applicative(Const(..))
>>> (Const 5 :: Const Integer Bool) ^? _Positive
Just (Positive 5)
Instance details

Defined in Natural

newtype SumPositive Source #

>>> SumPositive (pos 3) <> SumPositive (pos 4)
SumPositive (Positive 7)

Constructors

SumPositive Positive 

Instances

Instances details
FromJSON SumPositive Source #

Parses a SumPositive from a JSON number, failing on non-positive values.

>>> decode "7" :: Maybe SumPositive
Just (SumPositive (Positive 7))
>>> decode "0" :: Maybe SumPositive
Nothing
Instance details

Defined in Natural

ToJSON SumPositive Source #

Serialises a SumPositive to a JSON number.

>>> encode (SumPositive (pos 7))
"7"
Instance details

Defined in Natural

Semigroup SumPositive Source #
>>> SumPositive (pos 3) <> SumPositive (pos 4)
SumPositive (Positive 7)
Instance details

Defined in Natural

Show SumPositive Source # 
Instance details

Defined in Natural

Eq SumPositive Source # 
Instance details

Defined in Natural

Ord SumPositive Source # 
Instance details

Defined in Natural

Wrapped SumPositive Source #
>>> SumPositive (pos 5) ^. _Wrapped'
Positive 5
Instance details

Defined in Natural

Associated Types

type Unwrapped SumPositive 
Instance details

Defined in Natural

AsPositive SumPositive Source #
>>> SumPositive (pos 5) ^? _Positive
Just (Positive 5)
Instance details

Defined in Natural

HasPositive SumPositive Source #
>>> SumPositive (pos 5) ^. positive
Positive 5
Instance details

Defined in Natural

SumPositive ~ a => Rewrapped SumPositive a Source # 
Instance details

Defined in Natural

type Unwrapped SumPositive Source # 
Instance details

Defined in Natural

newtype MaxPositive Source #

>>> MaxPositive (pos 3) <> MaxPositive (pos 7)
MaxPositive (Positive 7)

Constructors

MaxPositive Positive 

Instances

Instances details
FromJSON MaxPositive Source #

Parses a MaxPositive from a JSON number, failing on non-positive values.

>>> decode "7" :: Maybe MaxPositive
Just (MaxPositive (Positive 7))
>>> decode "0" :: Maybe MaxPositive
Nothing
Instance details

Defined in Natural

ToJSON MaxPositive Source #

Serialises a MaxPositive to a JSON number.

>>> encode (MaxPositive (pos 7))
"7"
Instance details

Defined in Natural

Semigroup MaxPositive Source #
>>> MaxPositive (pos 3) <> MaxPositive (pos 7)
MaxPositive (Positive 7)
Instance details

Defined in Natural

Show MaxPositive Source # 
Instance details

Defined in Natural

Eq MaxPositive Source # 
Instance details

Defined in Natural

Ord MaxPositive Source # 
Instance details

Defined in Natural

Wrapped MaxPositive Source #
>>> MaxPositive (pos 7) ^. _Wrapped'
Positive 7
Instance details

Defined in Natural

Associated Types

type Unwrapped MaxPositive 
Instance details

Defined in Natural

AsPositive MaxPositive Source #
>>> MaxPositive (pos 7) ^? _Positive
Just (Positive 7)
Instance details

Defined in Natural

HasPositive MaxPositive Source #
>>> MaxPositive (pos 7) ^. positive
Positive 7
Instance details

Defined in Natural

MaxPositive ~ a => Rewrapped MaxPositive a Source # 
Instance details

Defined in Natural

type Unwrapped MaxPositive Source # 
Instance details

Defined in Natural

newtype MinPositive Source #

>>> MinPositive (pos 3) <> MinPositive (pos 7)
MinPositive (Positive 3)

Constructors

MinPositive Positive 

Instances

Instances details
FromJSON MinPositive Source #

Parses a MinPositive from a JSON number, failing on non-positive values.

>>> decode "3" :: Maybe MinPositive
Just (MinPositive (Positive 3))
>>> decode "0" :: Maybe MinPositive
Nothing
Instance details

Defined in Natural

ToJSON MinPositive Source #

Serialises a MinPositive to a JSON number.

>>> encode (MinPositive (pos 3))
"3"
Instance details

Defined in Natural

Semigroup MinPositive Source #
>>> MinPositive (pos 3) <> MinPositive (pos 7)
MinPositive (Positive 3)
Instance details

Defined in Natural

Show MinPositive Source # 
Instance details

Defined in Natural

Eq MinPositive Source # 
Instance details

Defined in Natural

Ord MinPositive Source # 
Instance details

Defined in Natural

Wrapped MinPositive Source #
>>> MinPositive (pos 3) ^. _Wrapped'
Positive 3
Instance details

Defined in Natural

Associated Types

type Unwrapped MinPositive 
Instance details

Defined in Natural

AsPositive MinPositive Source #
>>> MinPositive (pos 3) ^? _Positive
Just (Positive 3)
Instance details

Defined in Natural

HasPositive MinPositive Source #
>>> MinPositive (pos 3) ^. positive
Positive 3
Instance details

Defined in Natural

MinPositive ~ a => Rewrapped MinPositive a Source # 
Instance details

Defined in Natural

type Unwrapped MinPositive Source # 
Instance details

Defined in Natural

naturalPositive :: Iso' Natural (Maybe Positive) Source #

Iso between a natural and maybe a positive.

>>> nat 5 ^. naturalPositive
Just (Positive 5)
>>> nat 0 ^. naturalPositive
Nothing

one :: Prism' Positive () Source #

Prism matching one.

>>> one # ()
Positive 1
>>> pos 1 ^? one
Just ()
>>> pos 3 ^? one
Nothing

one' :: Positive Source #

The positive number one.

>>> one'
Positive 1

successor1 :: Prism' Positive Positive Source #

Prism between a positive and its predecessor.

>>> successor1 # pos 1
Positive 2
>>> successor1 # pos 4
Positive 5
>>> pos 5 ^? successor1
Just (Positive 4)
>>> pos 1 ^? successor1
Nothing

successor1' :: Positive -> Positive Source #

The successor of a positive number.

>>> successor1' (pos 1)
Positive 2
>>> successor1' (pos 4)
Positive 5

successorW :: Iso' Natural Positive Source #

Iso between natural and positive (n - n+1).

>>> nat 0 ^. successorW
Positive 1
>>> nat 4 ^. successorW
Positive 5
>>> successorW # pos 1
Natural 0
>>> successorW # pos 5
Natural 4

plus1 :: Positive -> Positive -> Positive Source #

Add two positive numbers.

>>> plus1 (pos 3) (pos 4)
Positive 7

multiply1 :: Positive -> Positive -> Positive Source #

Multiply two positive numbers.

>>> multiply1 (pos 3) (pos 4)
Positive 12

power1 :: Positive -> Positive -> Positive Source #

Raise a positive to a positive power.

>>> power1 (pos 2) (pos 10)
Positive 1024
>>> power1 (pos 3) (pos 2)
Positive 9

oneOr :: AsPositive a => a -> Positive Source #

Return the positive if the prism matches, otherwise one.

>>> oneOr (5 :: Integer)
Positive 5
>>> oneOr (0 :: Integer)
Positive 1

length1 :: Foldable1 f => f a -> Positive Source #

Count the elements in a non-empty foldable.

>>> length1 ('a' :| "bc")
Positive 3
>>> length1 ('x' :| "")
Positive 1

replicate1 :: Positive -> a -> NonEmpty a Source #

Replicate a value a positive number of times.

>>> replicate1 (pos 3) 'x'
'x' :| "xx"
>>> replicate1 (pos 1) 'y'
'y' :| ""

take1 :: Positive -> NonEmpty a -> NonEmpty a Source #

Take the first n elements from a non-empty list.

>>> take1 (pos 2) (1 :| [2,3,4,5 :: Int])
1 :| [2]
>>> take1 (pos 1) (1 :| [2,3 :: Int])
1 :| []

drop1 :: Positive -> NonEmpty a -> [a] Source #

Drop the first n elements from a non-empty list.

>>> drop1 (pos 2) (1 :| [2,3,4,5 :: Int])
[3,4,5]
>>> drop1 (pos 1) (1 :| [2,3 :: Int])
[2,3]

splitAt1 :: Positive -> NonEmpty a -> (NonEmpty a, [a]) Source #

Split a non-empty list at position n.

>>> splitAt1 (pos 2) (1 :| [2,3,4,5 :: Int])
(1 :| [2],[3,4,5])

(!!!) :: NonEmpty a -> Positive -> Maybe a Source #

Index into a non-empty list (1-based).

>>> (10 :| [20,30 :: Int]) !!! pos 1
Just 10
>>> (10 :| [20,30 :: Int]) !!! pos 3
Just 30
>>> (10 :| [20,30 :: Int]) !!! pos 5
Nothing

findIndices1 :: (a -> Bool) -> NonEmpty a -> [Positive] Source #

Find all 1-based indices where the predicate holds.

>>> findIndices1 (== 'a') ('a' :| "baca")
[Positive 1,Positive 3,Positive 5]

findIndex1 :: (a -> Bool) -> NonEmpty a -> Maybe Positive Source #

Find the first 1-based index where the predicate holds.

>>> findIndex1 (== 'c') ('a' :| "bcde")
Just (Positive 3)
>>> findIndex1 (== 'z') ('a' :| "bcde")
Nothing

elemIndices1 :: Eq a => a -> NonEmpty a -> [Positive] Source #

Find all 1-based indices of a given element.

>>> elemIndices1 'a' ('b' :| "anana")
[Positive 2,Positive 4,Positive 6]

elemIndex1 :: Eq a => a -> NonEmpty a -> Maybe Positive Source #

Find the first 1-based index of a given element.

>>> elemIndex1 'n' ('b' :| "anana")
Just (Positive 3)
>>> elemIndex1 'z' ('b' :| "anana")
Nothing

minus1 :: Positive -> Positive -> Positive Source #

Subtract two positives, flooring at one.

>>> minus1 (pos 5) (pos 3)
Positive 2
>>> minus1 (pos 3) (pos 5)
Positive 1
>>> minus1 (pos 3) (pos 3)
Positive 1

list1 :: Iso' Positive (NonEmpty ()) Source #

Iso between a positive and a non-empty list of units.

>>> pos 3 ^. list1
() :| [(),()]
>>> length1 (pos 3 ^. list1)
Positive 3

plusone :: Natural -> Positive Source #

Convert natural to positive by adding one.

>>> plusone (nat 0)
Positive 1
>>> plusone (nat 4)
Positive 5

minusone :: Positive -> Natural Source #

Convert positive to natural by subtracting one.

>>> minusone (pos 1)
Natural 0
>>> minusone (pos 5)
Natural 4

data NotZero Source #

A non-zero integer. True for positive, False for negative. The Positive gives the absolute value.

>>> NotZero True (pos 3)
NotZero True (Positive 3)
>>> NotZero False (pos 7)
NotZero False (Positive 7)

Constructors

NotZero Bool Positive 

Instances

Instances details
FromJSON NotZero Source #

Parses a NotZero from a JSON number, failing on zero.

>>> decode "5" :: Maybe NotZero
Just (NotZero True (Positive 5))
Instance details

Defined in Natural

ToJSON NotZero Source #

Serialises a NotZero to a JSON number.

>>> fromJSON (Number 5) :: Result NotZero
Success (NotZero True (Positive 5))
>>> fromJSON (Number (-3)) :: Result NotZero
Success (NotZero False (Positive 3))
>>> decode "7" :: Maybe NotZero
Just (NotZero True (Positive 7))
>>> decode "-2" :: Maybe NotZero
Just (NotZero False (Positive 2))
>>> decode "0" :: Maybe NotZero
Nothing
Instance details

Defined in Natural

Semigroup NotZero Source #

Semigroup under addition. Note: this is partial if the result would be zero. Use plusNZ for a total version.

>>> NotZero True (pos 3) <> NotZero True (pos 4)
NotZero True (Positive 7)
>>> NotZero False (pos 3) <> NotZero False (pos 4)
NotZero False (Positive 7)
>>> NotZero True (pos 5) <> NotZero False (pos 3)
NotZero True (Positive 2)
>>> NotZero False (pos 5) <> NotZero True (pos 3)
NotZero False (Positive 2)
Instance details

Defined in Natural

Show NotZero Source # 
Instance details

Defined in Natural

Eq NotZero Source # 
Instance details

Defined in Natural

Methods

(==) :: NotZero -> NotZero -> Bool #

(/=) :: NotZero -> NotZero -> Bool #

Ord NotZero Source #
>>> compare (NotZero True (pos 3)) (NotZero True (pos 5))
LT
>>> compare (NotZero False (pos 3)) (NotZero False (pos 5))
GT
>>> compare (NotZero False (pos 1)) (NotZero True (pos 1))
LT
>>> compare (NotZero True (pos 1)) (NotZero False (pos 1))
GT
Instance details

Defined in Natural

AsNotZero NotZero Source #
>>> _NotZero # NotZero True (pos 5) :: NotZero
NotZero True (Positive 5)
Instance details

Defined in Natural

AsPositive NotZero Source #

Prism from NotZero to Positive (matches only positive values).

>>> (NotZero True (pos 5)) ^? _Positive
Just (Positive 5)
>>> (NotZero False (pos 5)) ^? _Positive
Nothing
Instance details

Defined in Natural

HasNotZero NotZero Source #
>>> NotZero True (pos 5) ^. notZero
NotZero True (Positive 5)
Instance details

Defined in Natural

class HasNotZero a where Source #

Instances

Instances details
HasNotZero MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 7)) ^. notZero
NotZero True (Positive 7)
Instance details

Defined in Natural

HasNotZero MinNotZero Source #
>>> MinNotZero (NotZero False (pos 3)) ^. notZero
NotZero False (Positive 3)
Instance details

Defined in Natural

HasNotZero NotZero Source #
>>> NotZero True (pos 5) ^. notZero
NotZero True (Positive 5)
Instance details

Defined in Natural

HasNotZero SumNotZero Source #
>>> SumNotZero (NotZero True (pos 5)) ^. notZero
NotZero True (Positive 5)
Instance details

Defined in Natural

class AsNotZero a where Source #

>>> (7 :: Integer) ^? _NotZero
Just (NotZero True (Positive 7))
>>> (-3 :: Integer) ^? _NotZero
Just (NotZero False (Positive 3))
>>> (0 :: Integer) ^? _NotZero
Nothing

Instances

Instances details
AsNotZero MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 7)) ^? _NotZero
Just (NotZero True (Positive 7))
Instance details

Defined in Natural

AsNotZero MinNotZero Source #
>>> MinNotZero (NotZero False (pos 3)) ^? _NotZero
Just (NotZero False (Positive 3))
Instance details

Defined in Natural

AsNotZero NotZero Source #
>>> _NotZero # NotZero True (pos 5) :: NotZero
NotZero True (Positive 5)
Instance details

Defined in Natural

AsNotZero SumNotZero Source #
>>> SumNotZero (NotZero True (pos 5)) ^? _NotZero
Just (NotZero True (Positive 5))
Instance details

Defined in Natural

AsNotZero Integer Source #
>>> (42 :: Integer) ^? _NotZero
Just (NotZero True (Positive 42))
>>> (-42 :: Integer) ^? _NotZero
Just (NotZero False (Positive 42))
>>> (0 :: Integer) ^? _NotZero
Nothing
Instance details

Defined in Natural

AsNotZero Int Source #
>>> (5 :: Int) ^? _NotZero
Just (NotZero True (Positive 5))
>>> (-5 :: Int) ^? _NotZero
Just (NotZero False (Positive 5))
>>> (0 :: Int) ^? _NotZero
Nothing
Instance details

Defined in Natural

AsNotZero Word Source #
>>> (1 :: Word) ^? _NotZero
Just (NotZero True (Positive 1))
>>> (0 :: Word) ^? _NotZero
Nothing
Instance details

Defined in Natural

Integral a => AsNotZero (Identity a) Source #
>>> import Data.Functor.Identity(Identity(..))
>>> (Identity (-3) :: Identity Integer) ^? _NotZero
Just (NotZero False (Positive 3))
Instance details

Defined in Natural

Integral a => AsNotZero (Const a b) Source #
>>> import Control.Applicative(Const(..))
>>> (Const 5 :: Const Integer Bool) ^? _NotZero
Just (NotZero True (Positive 5))
Instance details

Defined in Natural

newtype SumNotZero Source #

>>> SumNotZero (NotZero True (pos 3)) <> SumNotZero (NotZero True (pos 4))
SumNotZero (NotZero True (Positive 7))

Constructors

SumNotZero NotZero 

Instances

Instances details
FromJSON SumNotZero Source #

Parses a SumNotZero from a JSON number, failing on zero.

>>> decode "7" :: Maybe SumNotZero
Just (SumNotZero (NotZero True (Positive 7)))
>>> decode "0" :: Maybe SumNotZero
Nothing
Instance details

Defined in Natural

ToJSON SumNotZero Source #

Serialises a SumNotZero to a JSON number.

>>> encode (SumNotZero (NotZero True (pos 7)))
"7"
Instance details

Defined in Natural

Semigroup SumNotZero Source #
>>> SumNotZero (NotZero True (pos 3)) <> SumNotZero (NotZero True (pos 4))
SumNotZero (NotZero True (Positive 7))
Instance details

Defined in Natural

Show SumNotZero Source # 
Instance details

Defined in Natural

Eq SumNotZero Source # 
Instance details

Defined in Natural

Ord SumNotZero Source # 
Instance details

Defined in Natural

Wrapped SumNotZero Source #
>>> SumNotZero (NotZero True (pos 5)) ^. _Wrapped'
NotZero True (Positive 5)
Instance details

Defined in Natural

Associated Types

type Unwrapped SumNotZero 
Instance details

Defined in Natural

AsNotZero SumNotZero Source #
>>> SumNotZero (NotZero True (pos 5)) ^? _NotZero
Just (NotZero True (Positive 5))
Instance details

Defined in Natural

HasNotZero SumNotZero Source #
>>> SumNotZero (NotZero True (pos 5)) ^. notZero
NotZero True (Positive 5)
Instance details

Defined in Natural

SumNotZero ~ a => Rewrapped SumNotZero a Source # 
Instance details

Defined in Natural

type Unwrapped SumNotZero Source # 
Instance details

Defined in Natural

newtype MaxNotZero Source #

>>> MaxNotZero (NotZero True (pos 3)) <> MaxNotZero (NotZero True (pos 7))
MaxNotZero (NotZero True (Positive 7))
>>> MaxNotZero (NotZero False (pos 3)) <> MaxNotZero (NotZero True (pos 1))
MaxNotZero (NotZero True (Positive 1))

Constructors

MaxNotZero NotZero 

Instances

Instances details
FromJSON MaxNotZero Source #

Parses a MaxNotZero from a JSON number, failing on zero.

>>> decode "-2" :: Maybe MaxNotZero
Just (MaxNotZero (NotZero False (Positive 2)))
>>> decode "0" :: Maybe MaxNotZero
Nothing
Instance details

Defined in Natural

ToJSON MaxNotZero Source #

Serialises a MaxNotZero to a JSON number.

>>> encode (MaxNotZero (NotZero False (pos 2)))
"-2"
Instance details

Defined in Natural

Semigroup MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 3)) <> MaxNotZero (NotZero True (pos 7))
MaxNotZero (NotZero True (Positive 7))
Instance details

Defined in Natural

Show MaxNotZero Source # 
Instance details

Defined in Natural

Eq MaxNotZero Source # 
Instance details

Defined in Natural

Ord MaxNotZero Source # 
Instance details

Defined in Natural

Wrapped MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 7)) ^. _Wrapped'
NotZero True (Positive 7)
Instance details

Defined in Natural

Associated Types

type Unwrapped MaxNotZero 
Instance details

Defined in Natural

AsNotZero MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 7)) ^? _NotZero
Just (NotZero True (Positive 7))
Instance details

Defined in Natural

HasNotZero MaxNotZero Source #
>>> MaxNotZero (NotZero True (pos 7)) ^. notZero
NotZero True (Positive 7)
Instance details

Defined in Natural

MaxNotZero ~ a => Rewrapped MaxNotZero a Source # 
Instance details

Defined in Natural

type Unwrapped MaxNotZero Source # 
Instance details

Defined in Natural

newtype MinNotZero Source #

>>> MinNotZero (NotZero True (pos 3)) <> MinNotZero (NotZero True (pos 7))
MinNotZero (NotZero True (Positive 3))
>>> MinNotZero (NotZero False (pos 3)) <> MinNotZero (NotZero True (pos 1))
MinNotZero (NotZero False (Positive 3))

Constructors

MinNotZero NotZero 

Instances

Instances details
FromJSON MinNotZero Source #

Parses a MinNotZero from a JSON number, failing on zero.

>>> decode "1" :: Maybe MinNotZero
Just (MinNotZero (NotZero True (Positive 1)))
>>> decode "0" :: Maybe MinNotZero
Nothing
Instance details

Defined in Natural

ToJSON MinNotZero Source #

Serialises a MinNotZero to a JSON number.

>>> encode (MinNotZero (NotZero True (pos 1)))
"1"
Instance details

Defined in Natural

Semigroup MinNotZero Source #
>>> MinNotZero (NotZero True (pos 3)) <> MinNotZero (NotZero True (pos 7))
MinNotZero (NotZero True (Positive 3))
Instance details

Defined in Natural

Show MinNotZero Source # 
Instance details

Defined in Natural

Eq MinNotZero Source # 
Instance details

Defined in Natural

Ord MinNotZero Source # 
Instance details

Defined in Natural

Wrapped MinNotZero Source #
>>> MinNotZero (NotZero False (pos 3)) ^. _Wrapped'
NotZero False (Positive 3)
Instance details

Defined in Natural

Associated Types

type Unwrapped MinNotZero 
Instance details

Defined in Natural

AsNotZero MinNotZero Source #
>>> MinNotZero (NotZero False (pos 3)) ^? _NotZero
Just (NotZero False (Positive 3))
Instance details

Defined in Natural

HasNotZero MinNotZero Source #
>>> MinNotZero (NotZero False (pos 3)) ^. notZero
NotZero False (Positive 3)
Instance details

Defined in Natural

MinNotZero ~ a => Rewrapped MinNotZero a Source # 
Instance details

Defined in Natural

type Unwrapped MinNotZero Source # 
Instance details

Defined in Natural

positiveNotZero :: Positive -> NotZero Source #

Embed a Positive as a positive NotZero.

>>> positiveNotZero (pos 5)
NotZero True (Positive 5)

negativeNotZero :: Positive -> NotZero Source #

Embed a Positive as a negative NotZero.

>>> negativeNotZero (pos 5)
NotZero False (Positive 5)

notZeroPositive :: NotZero -> Positive Source #

Extract the magnitude from a NotZero.

>>> notZeroPositive (NotZero True (pos 5))
Positive 5
>>> notZeroPositive (NotZero False (pos 3))
Positive 3

notZeroInteger :: NotZero -> Integer Source #

Convert a NotZero to an Integer.

>>> notZeroInteger (NotZero True (pos 5))
5
>>> notZeroInteger (NotZero False (pos 3))
-3

isPositive :: NotZero -> Bool Source #

Test if a NotZero is positive.

>>> isPositive (NotZero True (pos 5))
True
>>> isPositive (NotZero False (pos 5))
False

isNegative :: NotZero -> Bool Source #

Test if a NotZero is negative.

>>> isNegative (NotZero False (pos 5))
True
>>> isNegative (NotZero True (pos 5))
False

negateNZ :: NotZero -> NotZero Source #

Negate a NotZero.

>>> negateNZ (NotZero True (pos 5))
NotZero False (Positive 5)
>>> negateNZ (NotZero False (pos 3))
NotZero True (Positive 3)

absoluteNZ :: NotZero -> Positive Source #

Absolute value as a Positive.

>>> absoluteNZ (NotZero True (pos 5))
Positive 5
>>> absoluteNZ (NotZero False (pos 3))
Positive 3

signumNZ :: NotZero -> NotZero Source #

Signum: positive one or negative one.

>>> signumNZ (NotZero True (pos 99))
NotZero True (Positive 1)
>>> signumNZ (NotZero False (pos 42))
NotZero False (Positive 1)

plusNZ :: NotZero -> NotZero -> Maybe NotZero Source #

Add two NotZero values. Returns Nothing if the result is zero.

>>> plusNZ (NotZero True (pos 3)) (NotZero True (pos 4))
Just (NotZero True (Positive 7))
>>> plusNZ (NotZero True (pos 3)) (NotZero False (pos 3))
Nothing
>>> plusNZ (NotZero True (pos 5)) (NotZero False (pos 3))
Just (NotZero True (Positive 2))
>>> plusNZ (NotZero False (pos 5)) (NotZero True (pos 3))
Just (NotZero False (Positive 2))

multiplyNZ :: NotZero -> NotZero -> NotZero Source #

Multiply two NotZero values. Always non-zero.

>>> multiplyNZ (NotZero True (pos 3)) (NotZero True (pos 4))
NotZero True (Positive 12)
>>> multiplyNZ (NotZero False (pos 3)) (NotZero True (pos 4))
NotZero False (Positive 12)
>>> multiplyNZ (NotZero False (pos 3)) (NotZero False (pos 4))
NotZero True (Positive 12)

notZeroOr :: AsNotZero a => a -> NotZero Source #

Return the NotZero if the prism matches, otherwise positive one.

>>> notZeroOr (5 :: Integer)
NotZero True (Positive 5)
>>> notZeroOr (0 :: Integer)
NotZero True (Positive 1)
>>> notZeroOr (-3 :: Integer)
NotZero False (Positive 3)

toJsonNatural :: HasNatural a => a -> Value Source #

Serialises any value with a HasNatural instance to a JSON Value.

>>> toJsonNatural (nat 42)
Number 42.0
>>> toJsonNatural (ProductNatural (nat 12))
Number 12.0
>>> toJsonNatural (MaxNatural (nat 7))
Number 7.0
>>> toJsonNatural (MinNatural (nat 3))
Number 3.0

parseJsonNatural :: Value -> Parser Natural Source #

Parses a JSON value into a Natural, failing on negative values.

>>> parse parseJsonNatural (Number 42)
Success (Natural 42)
>>> parse parseJsonNatural (Number 0)
Success (Natural 0)
>>> parse parseJsonNatural (Number (-1))
Error "parse failed, Natural: expected non-negative integer"

toJsonPositive :: HasPositive a => a -> Value Source #

Serialises any value with a HasPositive instance to a JSON Value.

>>> toJsonPositive (pos 42)
Number 42.0
>>> toJsonPositive (SumPositive (pos 7))
Number 7.0
>>> toJsonPositive (MaxPositive (pos 7))
Number 7.0
>>> toJsonPositive (MinPositive (pos 3))
Number 3.0

parseJsonPositive :: Value -> Parser Positive Source #

Parses a JSON value into a Positive, failing on non-positive values.

>>> parse parseJsonPositive (Number 42)
Success (Positive 42)
>>> parse parseJsonPositive (Number 1)
Success (Positive 1)
>>> parse parseJsonPositive (Number 0)
Error "parse failed, Positive: expected positive integer"
>>> parse parseJsonPositive (Number (-1))
Error "parse failed, Positive: expected positive integer"

toJsonNotZero :: HasNotZero a => a -> Value Source #

Serialises any value with a HasNotZero instance to a JSON Value.

>>> toJsonNotZero (NotZero True (pos 5))
Number 5.0
>>> toJsonNotZero (NotZero False (pos 3))
Number (-3.0)
>>> toJsonNotZero (SumNotZero (NotZero True (pos 7)))
Number 7.0
>>> toJsonNotZero (MaxNotZero (NotZero False (pos 2)))
Number (-2.0)
>>> toJsonNotZero (MinNotZero (NotZero True (pos 1)))
Number 1.0

parseJsonNotZero :: Value -> Parser NotZero Source #

Parses a JSON value into a NotZero, failing on zero.

>>> parse parseJsonNotZero (Number 5)
Success (NotZero True (Positive 5))
>>> parse parseJsonNotZero (Number (-3))
Success (NotZero False (Positive 3))
>>> parse parseJsonNotZero (Number 0)
Error "parse failed, NotZero: expected non-zero integer"