utility functions
This commit is contained in:
@@ -41,6 +41,11 @@ orEmpty :: Alternative f => Bool -> a -> f a
|
|||||||
eitherA :: (Alternative f) => f a -> f b -> f (Either a b)
|
eitherA :: (Alternative f) => f a -> f b -> f (Either a b)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
pass :: Applicative f => f ()
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Alternative
|
Alternative
|
||||||
-------
|
-------
|
||||||
|
|
||||||
@@ -75,3 +80,11 @@ liftA :: Applicative f => (a -> b) -> f a -> f b
|
|||||||
```haskell
|
```haskell
|
||||||
empty :: Alternative f => f a
|
empty :: Alternative f => f a
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
guarded :: (Alternative f) => (a -> Bool) -> a -> f a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
guardedA :: (Functor f, Alternative t) => (a -> f Bool) -> a -> f (t a)
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
Bits
|
Bits
|
||||||
====
|
====
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
{-# LANGUAGE BinaryLiterals #-}
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
42 = 0b101010
|
||||||
|
```
|
||||||
|
|
||||||
Bit Operations
|
Bit Operations
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
@@ -28,12 +36,31 @@ xor :: Bits a => a -> a -> a
|
|||||||
|
|
||||||
*Example*:
|
*Example*:
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
> True `xor` False
|
||||||
|
True
|
||||||
|
|
||||||
|
> True `xor` True
|
||||||
|
False
|
||||||
|
|
||||||
|
> 0b101 `xor` 0b011
|
||||||
|
6 -- 0b110
|
||||||
|
```
|
||||||
|
|
||||||
#### complement
|
#### complement
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
complement :: Bits a => a -> a
|
complement :: Bits a => a -> a
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
> complement True
|
||||||
|
False
|
||||||
|
|
||||||
|
> complement 0b101
|
||||||
|
-2 -- -0b010
|
||||||
|
```
|
||||||
|
|
||||||
*Example*:
|
*Example*:
|
||||||
|
|
||||||
#### shift
|
#### shift
|
||||||
@@ -81,19 +108,47 @@ setBit :: Bits a => a -> Int -> a
|
|||||||
clearBit :: Bits a => a -> Int -> a
|
clearBit :: Bits a => a -> Int -> a
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### complementBit
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
complementBit :: Bits a => a -> Int -> a
|
complementBit :: Bits a => a -> Int -> a
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Example*:
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
λ> 0b100 `complementBit` 1
|
||||||
|
6 -- 0b110
|
||||||
|
```
|
||||||
|
|
||||||
|
#### testBit
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
testBit :: Bits a => a -> Int -> Bool
|
testBit :: Bits a => a -> Int -> Bool
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Example*:
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
> 0b10 `testBit` 0
|
||||||
|
False
|
||||||
|
> 0b10 `testBit` 1
|
||||||
|
True
|
||||||
|
```
|
||||||
|
|
||||||
|
#### isSigned
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
isSigned :: Bits a => a -> Bool
|
isSigned :: Bits a => a -> Bool
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
> isSigned 42
|
||||||
|
True
|
||||||
|
> isSigned True
|
||||||
|
False
|
||||||
|
```
|
||||||
|
|
||||||
Bit Size
|
Bit Size
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
Bool
|
Bool
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
bool :: a -> a -> Bool -> a
|
||||||
|
```
|
||||||
|
|||||||
+16
-2
@@ -1,5 +1,5 @@
|
|||||||
Exception Handling
|
Exceptions
|
||||||
==================
|
==========
|
||||||
|
|
||||||
#### MonadError
|
#### MonadError
|
||||||
|
|
||||||
@@ -61,6 +61,20 @@ throwSTM :: Exception e => e -> STM a
|
|||||||
throwError :: MonadError e m => e -> m a
|
throwError :: MonadError e m => e -> m a
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Utilities
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
hush :: Alternative m => Either e a -> m a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
note :: (MonadError e m, Applicative m) => e -> Maybe a -> m a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
tryIO :: MonadIO m => IO a -> ExceptT IOException m a
|
||||||
|
```
|
||||||
|
|
||||||
#### Fatal Errors
|
#### Fatal Errors
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
|
|||||||
@@ -68,8 +68,24 @@ identity :: a -> a
|
|||||||
|
|
||||||
The identity function maps any value to itself.
|
The identity function maps any value to itself.
|
||||||
|
|
||||||
|
#### applyN
|
||||||
|
|
||||||
|
Apply a function to a value `n` times.
|
||||||
|
|
||||||
*Example*:
|
*Example*:
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
applyN :: Int -> (a -> a) -> a -> a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
> applyN 25 (+2) 0
|
||||||
|
50
|
||||||
|
|
||||||
|
> applyN 3 (1:) []
|
||||||
|
[1,1,1]
|
||||||
|
```
|
||||||
|
|
||||||
Strictness
|
Strictness
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ uncons :: [a] -> Maybe (a, [a])
|
|||||||
unsnoc :: [x] -> Maybe ([x],x)
|
unsnoc :: [x] -> Maybe ([x],x)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
list :: [b] -> (a -> b) -> [a] -> [b]
|
||||||
|
```
|
||||||
|
|
||||||
Sorting
|
Sorting
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
|||||||
@@ -119,6 +119,22 @@ ap :: Monad m => m (a -> b) -> m a -> m b
|
|||||||
(<$!>) :: Monad m => (a -> b) -> m a -> m b
|
(<$!>) :: Monad m => (a -> b) -> m a -> m b
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
whenM :: Monad m => m Bool -> m () -> m ()
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
unlessM :: Monad m => m Bool -> m () -> m ()
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
ifM :: Monad m => m Bool -> m a -> m a -> m a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
guardM :: MonadPlus m => m Bool -> m ()
|
||||||
|
```
|
||||||
|
|
||||||
MonadPlus
|
MonadPlus
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,37 @@
|
|||||||
Monoid
|
Monoid
|
||||||
======
|
======
|
||||||
|
|
||||||
|
Monoid
|
||||||
|
------
|
||||||
|
|
||||||
|
Semigroup
|
||||||
|
---------
|
||||||
|
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
option :: b -> (a -> b) -> Option a -> b
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
diff :: Semigroup m => m -> Endo m
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
cycle1 :: Semigroup m => m -> m
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
stimesMonoid :: (Integral b, Monoid a) => b -> a -> a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
stimesIdempotent :: Integral b => b -> a -> a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
stimesIdempotentMonoid :: (Integral b, Monoid a) => b -> a -> a
|
||||||
|
```
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
mtimesDefault :: (Integral b, Monoid a) => b -> a -> a
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user