lenses

How to make lenses for records with type-families [duplicate]

旧巷老猫 提交于 2019-12-29 09:10:16
问题 This question already has answers here : How to derive instances for records with type-families (2 answers) Closed 2 years ago . Here's what I've got, which is not compiling: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} import Data.Text as T import Data.Int (Int64) import Control.Lens type family Incoming validationResult

Simulating interacting stateful objects in Haskell

混江龙づ霸主 提交于 2019-12-29 08:27:18
问题 I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable code. My problem is that I don't know how to pull the same trick when I have two (or more) stateful objects interacting with one another. Below I give a highly simplified version of the problem and sketch out what I have so far. For the sake of this

Existential quantifier silently disrupts Template Haskell (makeLenses). Why?

限于喜欢 提交于 2019-12-24 03:09:10
问题 I have this file: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE ExistentialQuantification #-} module Toy where import Control.Lens data Bar = Bar { _barish :: String } data Foo = forall a. Show a => Foo { _fooish :: a } $(makeLenses ''Bar) $(makeLenses ''Foo) x = barish y = fooish and I get the following error message: Toy.hs:15:5: Not in scope: `fooish' Perhaps you meant `_fooish' (line 9) This is my first time attempting to use existential quantifiers; I have no idea why this combination of

Type variable would escape its scope

雨燕双飞 提交于 2019-12-23 06:50:08
问题 I'm trying to refactor my function by giving it a lens argument (from the xml-lens package). I'm missing something about type quantifiers. What is going on here? *Main> let z name = listToMaybe $ pom ^.. root ./ ell name . text *Main> :t z z :: Text -> Maybe Text *Main> let z name l = listToMaybe $ pom ^.. l ./ ell name . text <interactive>:13:38: Couldn't match expected type ‘(Element -> f Element) -> Document -> f Document’ with actual type ‘t’ because type variable ‘f’ would escape its

How to use monocle to modify a nested map and another field in scala

你说的曾经没有我的故事 提交于 2019-12-22 10:38:37
问题 I'm giving a try to monocle for the first time. Here is the case class : case class State(mem: Map[String, Int], pointer: Int) And the current modification, using standard scala, that I would like to do : def add1 = (s: State) => s.copy( mem = s.mem.updated("a", s.mem("a") + 1), pointer = s.pointer + 1 ) And here is my implementation with monocle val mem = GenLens[State](_.mem) val pointer = GenLens[State](_.pointer) val add2 = (mem composeLens at("a")).modify(_.map(_ + 1)) andThen pointer

How do I handle the Maybe result of at in Control.Lens.Indexed without a Monoid instance

对着背影说爱祢 提交于 2019-12-22 01:50:38
问题 I recently discovered the lens package on Hackage and have been trying to make use of it now in a small test project that might turn into a MUD/MUSH server one very distant day if I keep working on it. Here is a minimized version of my code illustrating the problem I am facing right now with the at lenses used to access Key/Value containers (Data.Map.Strict in my case) {-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving, TemplateHaskell #-} module World where import Control.Applicative

Partial Lenses: Group array of objects by property, use prop value as key

ぐ巨炮叔叔 提交于 2019-12-18 09:28:37
问题 I have an array of objects like this: [ { name: "Group 1", value: "Foo" }, { name: "Group 2", value: "Bar" }, { name: "Group 1", value: "Baz" } ] I'd like to use Partial Lenses library to transform these groups to keys of an object with corresponding group's items, like this: { "Group 1": [ { name: "Group 1", value: "Foo" }, { name: "Group 1", value: "Baz" } ], "Group 2": [ { name: "Group 2", value: "Bar" } ] } My current approach is like this, assuming I have the source data in a variable

Shapeless: generic lens parameterized by case class or field

倖福魔咒の 提交于 2019-12-18 04:08:23
问题 Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to do? If not, the end result I'm trying to achieve is a generic method for updating nested Maps with case-class contents, e.g.: import scalaz._ import Scalaz._ import PLens._ import shapeless._ import shapeless.contrib.scalaz._ def nestedMapLens[R, T <:

Shapeless lenses usage with a string definition

孤街浪徒 提交于 2019-12-13 20:28:16
问题 I would like use shapeless lenses to access value of the case class field by a String definition. I know this code works. case class Test(id: String, calc: Long) val instance = Test("123232", 3434L) val lens = lens[Test] >> 'id val valueOfFieldId = lens.get(instance) But what I am trying to do is: val fieldName = "id" val lens = lens[Test] >> fieldName.witness //I typed .witness because it was expecting a witness (if I am not wrong) val valueOfFieldId = lens.get(instance) But with this code,

Lenses and TypeFamilies

孤者浪人 提交于 2019-12-12 15:07:20
问题 I've encountered a problem of using Control.Lens together with datatypes while using the -XTypeFamilies GHC pragma. {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} import Control.Lens (makeLenses) class SomeClass t where data SomeData t :: * -> * data MyData = MyData Int instance SomeClass MyData where data SomeData MyData a = SomeData {_a :: a, _b :: a} makeLenses ''SomeData The error message is: reifyDatatype: Use a value constructor to reify a data family instance . Is there