microlens-mtl (empty) → 0.1.0.0
raw patch · 4 files changed
+183/−0 lines, 4 filesdep +basedep +microlensdep +mtlsetup-changed
Dependencies added: base, microlens, mtl
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- microlens-mtl.cabal +32/−0
- src/Lens/Micro/Mtl.hs +119/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Artyom++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Artyom nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ microlens-mtl.cabal view
@@ -0,0 +1,32 @@+name: microlens-mtl+version: 0.1.0.0+synopsis: microlens support for Reader/Writer/State from mtl+description:+ This package contains functions (like 'view' or '+=') which work on+ @Control.Monad.Reader.MonadReader@, @Control.Monad.Writer.MonadWriter@, and+ @Control.Monad.State.MonadState@ from the mtl package.+license: BSD3+license-file: LICENSE+author: Artyom+maintainer: Artyom <yom@artyom.me>+homepage: http://github.com/aelve/microlens+bug-reports: http://github.com/aelve/microlens/issues+-- copyright: +category: Data, Lenses+build-type: Simple+-- extra-source-files: README.md+cabal-version: >=1.10++source-repository head+ type: git+ location: git://github.com/aelve/microlens.git++library+ exposed-modules: Lens.Micro.Mtl+ -- other-modules: + -- other-extensions: + build-depends: base >=4.4 && <5+ , microlens ==0.1.*+ , mtl >=2.0.1 && <2.3+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Lens/Micro/Mtl.hs view
@@ -0,0 +1,119 @@+module Lens.Micro.Mtl+(+ view,+ use,+ (.=), (%=),+ (+=), (-=), (*=), (//=),+)+where+++import Control.Applicative+import Control.Monad.Reader+import Control.Monad.Writer+import Control.Monad.State+import Lens.Micro+import Lens.Micro.Extras+++{- |+'view' is a synonym for ('^.'), generalised for 'MonadReader' (since+functions are instances of the 'MonadReader' class).++>>> view _1 (1, 2)+1++It's often used when dealing with environment, for instance:++@+doSomething :: ('MonadReader' Config m) => m Int+doSomething = do+ thingy <- 'view' setting1 -- same as “'asks' (^. setting1)”+ anotherThingy <- 'view' setting2+ ...+@+-}+view :: MonadReader s m => Getting a s a -> m a+view l = asks (getConst . l Const)+{-# INLINE view #-}++{- |+'use' is 'view' which implicitly operates on the state.++@+'use' l = 'gets' ('view' l)+@+-}+use :: MonadState s m => Getting a s a -> m a+use l = gets (view l)+{-# INLINE use #-}+++infix 4 .=, %=+infix 4 +=, -=, *=, //=++{- |+Assign value to the target. This is '.~' which works in 'State'.++@+l '.=' b = 'modify' (l '.~' b)+@+-}+(.=) :: MonadState s m => ASetter s s a b -> b -> m ()+l .= b = modify (l .~ b)+{-# INLINE (.=) #-}++{- |+Apply a function to the target. This is '%~' which works in 'State'.++>>> execState (do _1 %= (+1); _2 %= reverse) (1,"hello")+(2,"olleh")+-}+(%=) :: (MonadState s m) => ASetter s s a b -> (a -> b) -> m ()+l %= f = modify (l %~ f)+{-# INLINE (%=) #-}++{- |+Add a number to the target.++@+l '+=' x = l '%=' (+x)+@+-}+(+=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m ()+l += b = modify (l +~ b)+{-# INLINE (+=) #-}++{- |+Subtract a number from the target.++@+l '-=' x = l '%=' ('subtract' x)+@+-}+(-=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m ()+l -= b = modify (l -~ b)+{-# INLINE (-=) #-}++{- |+Multiply the target by a number.++@+l '*=' x = l '%=' (*x)+@+-}+(*=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m ()+l *= b = modify (l *~ b)+{-# INLINE (*=) #-}++{- |+Divide the target by a number.++@+l //= x = l %= (/x)+@+-}+(//=) :: (MonadState s m, Fractional a) => ASetter s s a a -> a -> m ()+l //= a = modify (l //~ a)+{-# INLINE (//=) #-}+