Questions tagged [haskell-lens]

A lenses library for Haskell.

An extensive library which provides families of lenses, isomorphisms, folds, traversals, getters and setters.

For more info visit:

454 questions
0
votes
1 answer

Lens: combine children values (xml-lens)

I have an xml that looks like this: AAA 100 ... BBB 200 ... I need to get a list of A and B values (for example as a list of…
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
0
votes
0 answers

Lens through the maybe field

I have a data similar to, but a bit more nested and of course named differently: data AppModel = AppModel { _foo :: Maybe String } deriving (Eq, Show) I would like to pass it to the Monomer textField textField :: WidgetEvent e => ALens' s Text ->…
majkrzak
  • 1,332
  • 3
  • 14
  • 30
0
votes
1 answer

How to fix compilation re haskell rigid type variable

I have a an aggregate type named Step and from it, some concrete types are created: class (Eq q, User a, User b, User c) => (Step q a b c) where {-# MINIMAL performerA, performerB, performerC, completionDate #-} performerA :: Lens' q…
Fiftywebs
  • 185
  • 10
0
votes
1 answer

Haskell Lens : lexical error in string/character literal at end of input

I'm working the "Optics by Example" haskell book. Below is my dependencies in package.yaml of stack project- dependencies: - base >= 4.7 && < 5 - aeson - containers - lens - lens-aeson - mtl - text Below is my Haskell Code - {-# LANGUAGE…
user51
  • 8,843
  • 21
  • 79
  • 158
0
votes
1 answer

functor on data type with two constructors - occurs check

I have a data type data Pnt2d i v = Pnt2d {_p2id:: i, _v2:: V2 v} and would like to construct a functor to apply to the second component (V2) as instance Functor (Pnt2d i) where fmap f (Pnt2d i v) = Pnt2d i (f v) but I get an "occurs check". I…
user855443
  • 2,596
  • 3
  • 25
  • 37
0
votes
2 answers

How do I convert indices to a lens for a Forest to a Tree?

I am trying to make a function that take a index into a forest, and a list of integers to choose a way down a tree, and turns that into a lens that focuses that same element. Effectively, it is the opposite of what (t :: Data.Tree.Forest) ^@..…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
0
votes
1 answer

How to use iset with nested datatype and lenses?

I can't get the types in the last function to line up. Point is to set the all price Doubles in connections with a function that depends only on the index of the 3-tuple. The original Double value in the tuple can be discarded. {-# LANGUAGE…
LHurttila
  • 37
  • 6
0
votes
1 answer

How to modify fields of a nested custom data type with lenses, when modifications depend on indices

Considering the following : {-# LANGUAGE TemplateHaskell #-} import Control.Lens data Typex = Typex { _level :: Int , _coordinate :: (Int, Int) , _connections :: [(Int,(Int,Int))] } deriving Show makeLenses…
0
votes
0 answers

Is there a string-based lens for runtime field access?

Similar to this question, but even more runtime-ey. Is there a way to automatically generate lenses, or any other mechanism, for accessing fields by name at runtime? I'm looking for something similar to field from generic-lens, except taking the…
Isaac van Bakel
  • 1,772
  • 10
  • 22
0
votes
1 answer

how to deal with duplicate record field with lenses?

given this piece of code: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FunctionalDependencies #-} module Foo where import Control.Lens ((^.), makeFieldsNoPrefix) import…
itsu
  • 222
  • 1
  • 10
0
votes
1 answer

How does Lens.Internal.Indexed connect to keyed containers?

A traversable may be labelled. To take this idea one step further, one may apply a function to any element of a traversable by its index. import Control.Monad.State updateAt :: forall a. Int -> (a -> a) -> [a] -> [a] updateAt i f = flip evalState…
Ignat Insarov
  • 4,660
  • 18
  • 37
0
votes
0 answers

How to fix "Couldn't match type ‘Item a0’ with ‘[Item [Char]]’" when reading using lens

I'm writing markov chain generator with Haskell, using weighted list for randomly selecting elements. When I'm testing the configuration, I get an error "Couldn't match type ‘Item a0’ with ‘[Item [Char]]’". I suspect this is somehow related to the…
Tuukka Turto
  • 145
  • 1
  • 2
  • 7
0
votes
3 answers

Haskell - A better way of keeping track of the (initial) state of a record

I'm working on some functions that take a record and return a slightly modified record. For example import Control.Lens ((%~), (^.), (&)) modifyRecord :: SomeRecord -> SomeRecord -> SomeRecord modifyRecord baseR currentR = currentR & thisPart %~…
atis
  • 881
  • 5
  • 22
0
votes
0 answers

How can one export a datatype record in haskell without exporting its fields?

I saw the somewhat older question at Avoiding namespace pollution in Haskell and am looking for a modern solution using lenses. I think the approach would come in a few steps: Rewrite fields to be lens friendly: field -> _field. Use lenses. Avoid…
bbarker
  • 11,636
  • 9
  • 38
  • 62
0
votes
1 answer

How do I add an Applicative context to a type expected by Lens' MyDataType?

I have a function generalized over a function func to be applied on MyDataType1 and another MyDataType1 like this setThis :: (Functor f0, Applicative f0) => Lens' MyDataType1 (f0 Float) -> Float -> MyDataType1 -> MyDataType1 -> MyDataType1 …
atis
  • 881
  • 5
  • 22