view . to ≡ id
>>> view (to f) a f a
>>> view _2 (1,"hello") "hello"
>>> view (to succ) 5 6
>>> view (_2._1) ("hello",("world","!!!")) "world"As view is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold, It may be useful to think of it as having one of these more restricted signatures:
view :: Getter s a -> s -> a view :: Monoid m => Fold s m -> s -> m view :: Iso' s a -> s -> a view :: Lens' s a -> s -> a view :: Monoid m => Traversal' s m -> s -> mIn a more general setting, such as when working with a Monad transformer stack you can use:
view :: MonadReader s m => Getter s a -> m a view :: (MonadReader s m, Monoid a) => Fold s a -> m a view :: MonadReader s m => Iso' s a -> m a view :: MonadReader s m => Lens' s a -> m a view :: (MonadReader s m, Monoid a) => Traversal' s a -> m a
>>> view _1 (1, 2) 1The reason it's not in Lens.Micro is that view in lens has a more general signature:
view :: MonadReader s m => Getting a s a -> m aSo, 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 microlens-mtl.
>>> view _1 (1, 2) 1When you're using Reader for config and your config type has lenses generated for it, most of the time you'll be using view instead of asks:
doSomething :: (MonadReader Config m) => m Int doSomething = do thingy <- view setting1 -- same as “asks (^. setting1)” anotherThingy <- view setting2 ...
view :: Getter s t a b -> s -> aDemote a lens or getter to a projection function.
view :: Monoid a => Fold s t a b -> s -> aReturns the monoidal summary of a traversal or a fold.
myShow :: Var ("y" '::= String :| "x" '::= Int :| Empty) -> String myShow (view x -> Just n) = "Int of "++show n myShow (view y -> Just s) = "String of "++s
view :: Getter s a -> s -> a