Skip to content

Commit

Permalink
refactored Data.Bits.Lens operators
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Nov 24, 2012
1 parent 4a575e3 commit 0908364
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Expand Up @@ -17,6 +17,7 @@
* Renamed `traverseAt` to `_at` in `Control.Lens.IndexedTraversal`.
* Renamed `traverseArray` to `_array` in `Data.Array.Lens`.
* Renamed and made the combinators in `Control.Lens.Zipper` more compositional to reduce third-party naming conflicts down to just `left` and `right`.
* Renamed `&=` and `|=` to `.&.=` and `.|.=` for consistency, mutatis mutandis their related operations.

3.5.1
-----
Expand Down
116 changes: 58 additions & 58 deletions src/Data/Bits/Lens.hs
Expand Up @@ -12,8 +12,8 @@
--
----------------------------------------------------------------------------
module Data.Bits.Lens
( (|~), (&~), (<|~), (<&~)
, (|=), (&=), (<|=), (<&=)
( (.|.~), (.&.~), (<.|.~), (<.&.~)
, (.|.=), (.&.=), (<.|.=), (<.&.=)
, bitAt
, bits
) where
Expand All @@ -23,114 +23,114 @@ import Control.Monad.State.Class
import Data.Bits
import Data.Functor

infixr 4 |~, &~, <|~, <&~
infix 4 |=, &=, <|=, <&=
infixr 4 .|.~, .&.~, <.|.~, <.&.~
infix 4 .|.=, .&.=, <.|.=, <.&.=

-- | Bitwise '.|.' the target(s) of a 'Lens' or 'Setter'
--
-- >>> _2 |~ 6 $ ("hello",3)
-- >>> _2 .|.~ 6 $ ("hello",3)
-- ("hello",7)
--
-- @
-- ('|~') :: 'Bits' a => 'Setter' s t a a -> a -> s -> t
-- ('|~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> t
-- ('|~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> t
-- ('|~') :: ('Monoid a', 'Bits' a) => 'Traversal' s t a a -> a -> s -> t
-- ('.|.~') :: 'Bits' a => 'Setter' s t a a -> a -> s -> t
-- ('.|.~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> t
-- ('.|.~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> t
-- ('.|.~') :: ('Monoid a', 'Bits' a) => 'Traversal' s t a a -> a -> s -> t
-- @
(|~):: Bits a => Setting s t a a -> a -> s -> t
l |~ n = over l (.|. n)
{-# INLINE (|~) #-}
(.|.~):: Bits a => Setting s t a a -> a -> s -> t
l .|.~ n = over l (.|. n)
{-# INLINE (.|.~) #-}

-- | Bitwise '.&.' the target(s) of a 'Lens' or 'Setter'
--
-- >>> _2 &~ 7 $ ("hello",254)
-- >>> _2 .&.~ 7 $ ("hello",254)
-- ("hello",6)
--
-- @
-- ('&~') :: 'Bits' a => 'Setter' s t a a -> a -> s -> t
-- ('&~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> t
-- ('&~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> t
-- ('&~') :: ('Monoid a', 'Bits' a) => 'Traversal' s t a a -> a -> s -> t
-- ('.&.~') :: 'Bits' a => 'Setter' s t a a -> a -> s -> t
-- ('.&.~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> t
-- ('.&.~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> t
-- ('.&.~') :: ('Monoid a', 'Bits' a) => 'Traversal' s t a a -> a -> s -> t
-- @
(&~) :: Bits a => Setting s t a a -> a -> s -> t
l &~ n = over l (.&. n)
{-# INLINE (&~) #-}
(.&.~) :: Bits a => Setting s t a a -> a -> s -> t
l .&.~ n = over l (.&. n)
{-# INLINE (.&.~) #-}

-- | Modify the target(s) of a 'Simple' 'Lens', 'Setter' or 'Traversal' by computing its bitwise '.&.' with another value.
--
-- @
-- ('&=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Setter' s a -> a -> m ()
-- ('&=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Iso' s a -> a -> m ()
-- ('&=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m ()
-- ('&=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Traversal' s a -> a -> m ()
-- ('.&.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Setter' s a -> a -> m ()
-- ('.&.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Iso' s a -> a -> m ()
-- ('.&.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m ()
-- ('.&.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Traversal' s a -> a -> m ()
-- @
(&=):: (MonadState s m, Bits a) => Simple Setting s a -> a -> m ()
l &= a = modify (l &~ a)
{-# INLINE (&=) #-}
(.&.=):: (MonadState s m, Bits a) => Simple Setting s a -> a -> m ()
l .&.= a = modify (l .&.~ a)
{-# INLINE (.&.=) #-}

-- | Modify the target(s) of a 'Simple' 'Lens', 'Setter' or 'Traversal' by computing its bitwise '.|.' with another value.
--
-- @
-- ('|=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Setter' s a -> a -> m ()
-- ('|=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Iso' s a -> a -> m ()
-- ('|=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m ()
-- ('|=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Traversal' s a -> a -> m ()
-- ('.|.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Setter' s a -> a -> m ()
-- ('.|.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Iso' s a -> a -> m ()
-- ('.|.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m ()
-- ('.|.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Traversal' s a -> a -> m ()
-- @
(|=) :: (MonadState s m, Bits a) => Simple Setting s a -> a -> m ()
l |= a = modify (l |~ a)
{-# INLINE (|=) #-}
(.|.=) :: (MonadState s m, Bits a) => Simple Setting s a -> a -> m ()
l .|.= a = modify (l .|.~ a)
{-# INLINE (.|.=) #-}

-- | Bitwise '.|.' the target(s) of a 'Lens' (or 'Traversal'), returning the result
-- (or a monoidal summary of all of the results).
--
-- >>> _2 <|~ 6 $ ("hello",3)
-- >>> _2 <.|.~ 6 $ ("hello",3)
-- (7,("hello",7))
--
-- @
-- ('<|~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> (a, t)
-- ('<|~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> (a, t)
-- ('<|~') :: ('Bits' a, 'Monoid a) => 'Traversal' s t a a -> a -> s -> (a, t)
-- ('<.|.~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> (a, t)
-- ('<.|.~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> (a, t)
-- ('<.|.~') :: ('Bits' a, 'Monoid a) => 'Traversal' s t a a -> a -> s -> (a, t)
-- @
(<|~):: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
l <|~ n = l <%~ (.|. n)
{-# INLINE (<|~) #-}
(<.|.~):: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
l <.|.~ n = l <%~ (.|. n)
{-# INLINE (<.|.~) #-}

-- | Bitwise '.&.' the target(s) of a 'Lens' or 'Traversal', returning the result
-- (or a monoidal summary of all of the results).
--
-- >>> _2 <&~ 7 $ ("hello",254)
-- >>> _2 <.&.~ 7 $ ("hello",254)
-- (6,("hello",6))
--
-- @
-- ('<&~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> (a, t)
-- ('<&~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> (a, t)
-- ('<&~') :: ('Bits' a, 'Monoid a) => 'Traversal' s t a a -> a -> s -> (a, t)
-- ('<.&.~') :: 'Bits' a => 'Iso' s t a a -> a -> s -> (a, t)
-- ('<.&.~') :: 'Bits' a => 'Lens' s t a a -> a -> s -> (a, t)
-- ('<.&.~') :: ('Bits' a, 'Monoid a) => 'Traversal' s t a a -> a -> s -> (a, t)
-- @
(<&~) :: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
l <&~ n = l <%~ (.&. n)
{-# INLINE (<&~) #-}
(<.&.~) :: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
l <.&.~ n = l <%~ (.&. n)
{-# INLINE (<.&.~) #-}

-- | Modify the target(s) of a 'Simple' 'Lens' (or 'Traversal') by computing its bitwise '.&.' with another value,
-- returning the result (or a monoidal summary of all of the results traversed)
--
-- @
-- ('<&=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m a
-- ('<&=') :: ('MonadState' s m, 'Bits' a, 'Monoid' a) => 'Simple' 'Traversal' s a -> a -> m a
-- ('<.&.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m a
-- ('<.&.=') :: ('MonadState' s m, 'Bits' a, 'Monoid' a) => 'Simple' 'Traversal' s a -> a -> m a
-- @
(<&=):: (MonadState s m, Bits a) => SimpleLensLike ((,)a) s a -> a -> m a
l <&= b = l <%= (.&. b)
{-# INLINE (<&=) #-}
(<.&.=):: (MonadState s m, Bits a) => SimpleLensLike ((,)a) s a -> a -> m a
l <.&.= b = l <%= (.&. b)
{-# INLINE (<.&.=) #-}

-- | Modify the target(s) of a 'Simple' 'Lens', (or 'Traversal') by computing its bitwise '.|.' with another value,
-- returning the result (or a monoidal summary of all of the results traversed)
--
-- @
-- ('<|=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m a
-- ('<|=') :: ('MonadState' s m, 'Bits' a, 'Monoid' a) => 'Simple' 'Traversal' s a -> a -> m a
-- ('<.|.=') :: ('MonadState' s m, 'Bits' a) => 'Simple' 'Lens' s a -> a -> m a
-- ('<.|.=') :: ('MonadState' s m, 'Bits' a, 'Monoid' a) => 'Simple' 'Traversal' s a -> a -> m a
-- @
(<|=) :: (MonadState s m, Bits a) => SimpleLensLike ((,)a) s a -> a -> m a
l <|= b = l <%= (.|. b)
{-# INLINE (<|=) #-}
(<.|.=) :: (MonadState s m, Bits a) => SimpleLensLike ((,)a) s a -> a -> m a
l <.|.= b = l <%= (.|. b)
{-# INLINE (<.|.=) #-}

-- | This lens can be used to access the value of the nth bit in a number.
--
Expand Down

0 comments on commit 0908364

Please sign in to comment.