haskell,composition,lens,lenses

You could construct it like this: combo :: Lens' M Int -- alias for 'Functor f => (Int -> f Int) -> (M -> f M) combo f m = arr (\xs -> fmap (\r -> xs // [(i, r)]) $ f $ xs ! i) m where i =...

Both lens and aeson have functions to allow customizable handling of field and constructor names. Since aeson's default is not what you want, and wouldn't work anyway if you want the lens names to be the same as the JSON field names, let's change the aeson configuration: {-# LANGUAGE DeriveGeneric...

Short Answer f isn't a Lens, so can't be used as ALens. When you see ALens as an argument to a function, it expects a Lens. Long Answer A polymorphic value that requires an Applicative instance can be used fewer places than a value that only requires a Functor. Applicative...

I couldn't get your code to compile, but that shouldn't matter. I assume that you have a function of type fromStringTraversal :: (AsValue t, FromJSON v, ToJSON v, Default v) => String -> Traversal' t v fromStringTraversal = undefined Then to write your instance, simply inline the definition of Traversal'...

This is a normal thing. The lens library relies heavily on polymorphism, so the monomorphism restriction (which makes things less polymorphic than they could be) doesn't interact so well with it. In your case, I think you could also write your code like this: meetupLat :: Lens' Meetup Latitude meetupLat...

The type error happens because el is a Traversal. Traversals point to multiple values (in this case, all foo elements). (^.), on the other hand, always returns a single value. Using (^.) with a Traversal only works if the target type is a Monoid; in that case, the multiple values...

There is no existing operator for this but you could easily define your own: %$~ :: Functor f => ASetter s t (f a) (f b) -> (a -> b) -> s -> t l %$~ f = over a (fmap f) The standard way to do this is to...

I've never actually seen or used this before. But I imagine it's useful in the same way as the id function (which returns it's argument unmodified) or the Identity monad (which is a monad with no "effects"; exactly equivalent to not using monads at all) are useful. These and other...

You could think of the Getter part of a lens as a function, which you can extract using view. For example, the lens way of writing the fst function is: view _1 :: (a,b) -> a Now observe: view _1 . view _2 :: (c, (a,b)) -> a -- First...

With ix you can index not only by number, but by e.g. key in the Maps. The element index in Traverse order. λ> let m = Map.fromList [("foo", 'f'), ("bar", 'b')] λ> m ^? ix "foo" Just 'f' λ> m ^? element 0 -- Map is ordered container! Just 'b'...

András Kovács answer shows how to achieve this with RankNTypes. If you wish to avoid RankNTypes, then you can use ALens and cloneLens: f :: a -> ALens' a Int -> (Int, a) f a l = (newvalue, a & cloneLens l .~ newvalue) where oldvalue = a^.cloneLens l newvalue...

For the counterexample, let us first define a new data type, for which we generate traversals using makePrisms: data T = A T | C deriving Show makePrisms ''T _A :: Traversal T T is now a valid traversal. Now, construct a new traversal using failing: t :: Traversal' T...

(./) is based on a Plated instance, which is the class of types that have "children" of the same type. HTML elements, an AST for mathematical expressions, etc. Here (.) just chains traversals. So *Main> x ^.. root ./ ell "version" . text ["1.0-SNAPSHOT"] goes to all children of the...

Prelude Control.Lens> [1, 2, 3, 4] & each . filtered even %~ (+ 10) [1, 12, 3, 14] To find functions like this, you can instruct hoogle to search the lens package by putting "+lens" into the search field. In this case: http://www.haskell.org/hoogle/?hoogle=%2Blens+filter...

One way is to make an Iso and compose with that. In ghci: > import Control.Lens > import Control.Zipper > > data A t = A t > let _A = iso A (\(A a) -> a) > > let a = within (ix 1 . _A) $ zipper ([1,2,3]...

Rearrange your program as follows {-# LANGUAGE TemplateHaskell #-} import Control.Lens data A = A { _a1 :: B, _a2 :: Int } data B = B1 { _b1 :: Int } | B2 makeLenses ''A makeLenses ''B The issue has to do with staging order of Template Haskell (and...

The to combinator only makes a getter, you haven't defined a full lens. However, defining the full lens is pretty straightforward, you just need to return the correct lens based on the color passed in: byColor :: Color -> Lens' Players Player byColor White = white byColor Black = black...

Sometimes, storing lenses in containers can cause issues but we can circumvent these by using ALens'. Conceptually, ALens' Element Document is more or less the same as Functor f => ((Element -> f Element) -> Document -> f Document), but you can put it into a container with fewer problems....

The answer turned out to be just combo arr idx f m = (arr . ix (m^.idx)) f m As the index may be out of bounds, ix is a partial lens called Traversal. So bar has to use uses instead of use: foo x = do combo arr idx...

You want to replace nth 0 with values which is a traversal over Aeson arrays. Also, since you have a traversal with multiple results and want a list rather than a Maybe, you have to use ^.. instead of ^?. *Main> locations ^.. key "Locations" . values . key "averageReview"...

Well, if you were to use Data.Map.Map as your structure instead (should be a pretty easy refactor) you won't have to replicate all this work yourself: import qualified Data.Map as M import Control.Lens type Headers = M.Map ByteString ByteString fetchHeader :: Headers -> ByteString -> Maybe ByteString fetchHeader = flip...

haskell,lens,higher-rank-types

You want [bar] to have type [forall c . Lens' (Foo c) c], but it actually has the type forall f c . Functor f => [(c -> f c) -> Foo c -> f (Foo c)]. The compiler infers the latter type signature because it is predicative. You can...

view actually has a type that's less restrictive than Lens s t a b -> s -> a. If you drop the type signature, ghci will tell you the type for view :t view view :: ((a1 -> Const a1 b1) -> t -> Const a b) -> t ->...

haskell,clojure,lens,nested-map

Lenses in Haskell are not restricted to keys. You could for example write a lens for the length of a string: lengthLens = lens length setLength setLength s l = take l s ++ take (l - length s) (repeat '\0') Clojure key sequences are restricted to map/vector/etc keys. I...

This one is rather tricky given the current publicly exposed API. I took the liberty of expanding the type of deepOf to also support indexed folds and indexed traversals along the way as it was easier than not doing so, and makes the implementation be the full one we'd want...

The key point here is that the annotation should be in a separate line. If we do that, then we have a binding with an explicit type, as far as GHC is concerned. someFunction :: MyMonad () someFunction = do ... let x :: Traversal' MyState MyDataT x = myState.clients.ix...

First of all use lens-aeson not aeson-lens. aeson-lens hasn't been updated in a while and I don't think all its lenses are law-abiding. Since they both share Data.Aeson.Lens you'll have to run ghc-pkg unregister aeson-lens (or cabal sandbox hc-pkg unregister aeson-lens if you're using a sandbox). You haven't described exactly...

haskell,functional-programming,lens

Your description of "Bag of Holding" wasn't precise but I think this is close to what you meant. The basic idea is to traverse into a child bag using a [Int] (similar to the Ixed instance for Tree) and use the At instance for Map to edit the items. {-#...

haskell,invariants,lens,lenses

A lens isn't, in fact, "a getter and a setter", it just happens to be isomorphic to such a pair. So you can't just export one of them, rather you have to define something new and export that. Fortunately, this is extremely simple: data Counter = Counter { _count' ::...

As you said, you're probably looking for lenses. A lens is a value that allows to read, set or modify a given field. Usually with Control.Lens, you define fields with underscores and you use makeLenses to create full-featured lenses. There are many combinators that allow lenses to be used together...

Only the definitions before the template haskell call are accessible in the scope. See this previous question on the same topic: Haskell: Template Haskell and the scope....

haskell,monad-transformers,lens

I solved it by fixing package inconsistencies, thanks to a hint by Rufflewind (see comment on my question above). I first checked my existing package configuration with ghc-pkg check, and then used ghc-pkg unregister --force to remove all broken packages. I iteratively repeated it until no more broken packages were...