lens

Combining Getters into a Fold

半城伤御伤魂 提交于 2019-12-11 09:20:20
问题 In the spirit of the following questions: Getting multiple results from map with “lens” Combining lenses I am now looking for a way to combine multiple Getters into a single Fold, so that something like the following: ('a','b','c','d') ^.. (_1 <> _2 <> _3) would result in this: ['a', 'b', 'c'] But the code above actually fails with the following message: No instance for (Monoid (Accessor (Endo [Char]) (Char, Char, Char, Char))) arising from a use of `<>' So how do I achieve this? Is this

How to override a default value, via lenses, only if incoming value is not Nothing

こ雲淡風輕ζ 提交于 2019-12-11 05:47:27
问题 I'm basically trying to override a bunch of default values in a record only if the user-specific values are NOT Nothing . Is it possible to do it via lenses? import qualified Data.Default as DD instance DD.Def Nouns where def = Nouns { -- default values for each field come here } lookupHStore :: HStoreList -> Text -> Maybe Text mkNounsFromHStoreList :: HStoreList -> Nouns mkNounsFromHStoreList h = (DD.def Nouns) & depSingular .~ (lookupHStore h "dep_label_singular") -- ERROR: Won't compile

Lift through nested state transformers (mtl)

£可爱£侵袭症+ 提交于 2019-12-11 02:31:33
问题 So I'm working on an extensible application framework, and a key part of the framework is to be able to run state monads over many different state types; I've got it set up, and can run the nested state monads; however a key feature I need is for monads over nested states to be able to run actions over the global state as well; I managed to rig this up using some complicated Free Monads in an earlier project, but now I'm using mtl and I'm a bit stuck. Here's some context: newtype App a = App

Lens zoom ambiguous variable

拈花ヽ惹草 提交于 2019-12-11 01:44:27
问题 I'm having difficulty using the zoom function given by Control.Lens . With my custom monad transformer HearthMonad , I cannot figure out how to satisfy GHC's "ambiguous type" complaint. The code in question is in drawCard . How can I solve this? Do I have to create my own custom zoom operator to handle the Monad m in Hearth m ? {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE

Writing Category Instance for custom Lens

爱⌒轻易说出口 提交于 2019-12-10 18:33:45
问题 I have been reading this article for understanding Lenses. I know this is different from Edward Knett's lens package, but nonetheless it's useful for fundamentals. So, A Lens is defined like this: type Lens a b = (a -> b, b -> a -> a) It has been mentioned that Lenses form a category and I have been trying out to create an instance for Category typeclass. For a start, I wrote the type definition for the functions: (.) :: Lens y z -> Lens x y -> Lens x z id :: Lens x x And after this, I just

How do you write a complex lens that depend on other lenses using the lens library?

ぃ、小莉子 提交于 2019-12-10 18:19:41
问题 At the moment, I have a WorkLog type, with a start and end date. I want to also add a duration lens, which will be derived from the start and end dates. It should either be read only, or change the end date if its value is changed (I would like to know how to implement both version, even though I will only use one). Here is my code. Basically, if you can implement the workLogDurationRO and workLogDurationRW functions to get all the tests in main passing, that would answer my question. {-#

Lenses: Composing backwards and (.) in Lens context

血红的双手。 提交于 2019-12-10 16:28:11
问题 I have been reading this article and in one of their section it is stated: Lenses compose backwards. Can't we make (.) behave like functions? You're right, we could. We don't for various reasons, but the intuition is right. Lenses should combine just like functions. One thing that's important about that is id can either pre- or post- compose with any lens without affecting it. What does that mean by Lenses compose backwards ? Also, what does this mean: Can't we make (.) behave like functions

Monadic alteration to tuple

大兔子大兔子 提交于 2019-12-10 13:57:17
问题 I am looking for a function with type similar to: Monad m => (a, b) -> (b -> m c) -> m (a, c) It appears to me as some combination of bind ( >>= ) and a lens operation. I am aware that I can solve this with a pattern match after a bind, but my gut tells me there is a "simpler" way to write this by leveraging lenses. Is there any such operation? 回答1: This is definitely lensy. The monad is actually just a bit of a distraction because all you need is a functor: changesecond (a, b) f = fmap (a,)

Haskell: use or uses in Getter

妖精的绣舞 提交于 2019-12-10 11:55:47
问题 In Control.Lens we have Getter that can access the nested structure. Getter has use and uses, but it's not clear to me how they work. So it'd be great if someone can provide some simple examples that use or uses is utilised. Why do I need to know it? because I'm reading some implementation in Haskell and "uses" and "use" were used in them. In particualr it says: inRange <- uses fsCurrentCoinRangeUpperBound (coinIndex <=) If the above code is just for comparing (<=) two values, then why do we

Lenses over Comonads or Representable

℡╲_俬逩灬. 提交于 2019-12-08 20:15:28
Here's a more specific variant of this question: Mutate only focus of Store Comonad? , for the benefit of not asking more than one question at once. Are there any lenses compatible with Control.Lens which allow me to interact with the focus of a comonad (the value from extract ) or with the index/value of the Store Comonad ( pos )? It seems like lenses may be of some use here, but I have been unable to find anything which fits; any help would be appreciated, thanks! Comonad doesn't give you any way to write back to the comonad's focus, so you can't write a general Lens for extract . But it is