1{-# LANGUAGE Trustworthy #-}
2
3
4{- |
5Module      :  Lens.Micro.Extras
6Copyright   :  (C) 2013-2016 Edward Kmett, 2015-2016 Artyom Kazak, 2018 Monadfix
7License     :  BSD-style (see the file LICENSE)
8-}
9module Lens.Micro.Extras
10(
11  view,
12  preview,
13)
14where
15
16
17import Lens.Micro
18import Lens.Micro.Internal
19
20import Control.Applicative
21import Data.Monoid
22
23
24{- |
25'view' is a synonym for ('^.'):
26
27>>> view _1 (1, 2)
281
29
30The reason it's not in "Lens.Micro" is that @view@ in lens has a more general signature:
31
32@
33view :: MonadReader s m => Getting a s a -> m a
34@
35
36So, you would be able to use this 'view' with functions, but not in various reader monads. For most people this shouldn't be an issue; if it is for you, use @view@ from <http://hackage.haskell.org/package/microlens-mtl microlens-mtl>.
37-}
38view :: Getting a s a -> s -> a
39view l = getConst #. l Const
40{-# INLINE view #-}
41
42{- |
43'preview' is a synonym for ('^?'):
44
45>>> preview _head [1,2,3]
46Just 1
47
48The reason it's not in "Lens.Micro" is that @preview@ in lens has a more general signature:
49
50@
51preview :: MonadReader s m => Getting (First a) s a -> m (Maybe a)
52@
53
54Just like with 'view', you would be able to use this 'preview' with functions, but not in reader monads; if this is an issue for you, use @preview@ from <http://hackage.haskell.org/package/microlens-mtl microlens-mtl>.
55-}
56preview :: Getting (First a) s a -> s -> Maybe a
57preview l = getFirst #. foldMapOf l (First #. Just)
58{-# INLINE preview #-}
59