lenses

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

折月煮酒 提交于 2019-11-30 14:59:05
问题 I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

∥☆過路亽.° 提交于 2019-11-30 13:06:29
I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State import qualified Data.Foldable as F import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map

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

北慕城南 提交于 2019-11-29 15:46:36
This question already has an answer here: How to derive instances for records with type-families 2 answers 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 baseType type instance Incoming Validated baseType = baseType type instance Incoming ValidationErrors baseType = Either [T.Text]

Simulating interacting stateful objects in Haskell

淺唱寂寞╮ 提交于 2019-11-29 13:29:35
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 question, let's assume a machine's internal state consists only of a single integer register, so that its

Combining lenses

风格不统一 提交于 2019-11-29 09:17:23
Using a lens library I can apply a modification function to individual targets, like so: Prelude Control.Lens> (1, 'a', 2) & _1 %~ (*3) (3,'a',2) Prelude Control.Lens> (1, 'a', 2) & _3 %~ (*3) (1,'a',6) How can I combine those individual lenses ( _1 and _3 ) to be able to perform this update to both of the targets at once? I expect something in the spirit of the following: Prelude Control.Lens> (1, 'a', 2) & ??? %~ (*3) (3,'a',6) Using untainted from the Settable type class in Control.Lens.Internal.Setter , it is possible to combine two setters, but the result will also only be a setter and

Shapeless: generic lens parameterized by case class or field

可紊 提交于 2019-11-29 04:04:48
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 <: Product](outerKey: String, innerKey: Int, f: Symbol) = ~((lens[T] >> f).asScalaz) compose mapVPLens

Why do we need Control.Lens.Reified?

霸气de小男生 提交于 2019-11-29 03:52:00
Why do we need Control.Lens.Reified ? Is there some reason I can't place a Lens directly into a container? What does reify mean anyway? We need reified lenses because Haskell's type system is predicative. I don't know the technical details of exactly what that means, but it prohibits types like [Lens s t a b] For some purposes, it's acceptable to use Functor f => [(a -> f b) -> s -> f t] instead, but when you reach into that, you don't get a Lens ; you get a LensLike specialized to some functor or another. The ReifiedBlah newtypes let you hang on to the full polymorphism. Operationally,

Combining lenses

久未见 提交于 2019-11-28 02:41:58
问题 Using a lens library I can apply a modification function to individual targets, like so: Prelude Control.Lens> (1, 'a', 2) & _1 %~ (*3) (3,'a',2) Prelude Control.Lens> (1, 'a', 2) & _3 %~ (*3) (1,'a',6) How can I combine those individual lenses ( _1 and _3 ) to be able to perform this update to both of the targets at once? I expect something in the spirit of the following: Prelude Control.Lens> (1, 'a', 2) & ??? %~ (*3) (3,'a',6) 回答1: Using untainted from the Settable type class in Control

Why do we need Control.Lens.Reified?

﹥>﹥吖頭↗ 提交于 2019-11-27 18:11:06
问题 Why do we need Control.Lens.Reified? Is there some reason I can't place a Lens directly into a container? What does reify mean anyway? 回答1: We need reified lenses because Haskell's type system is predicative. I don't know the technical details of exactly what that means, but it prohibits types like [Lens s t a b] For some purposes, it's acceptable to use Functor f => [(a -> f b) -> s -> f t] instead, but when you reach into that, you don't get a Lens ; you get a LensLike specialized to some

Functional lenses

梦想与她 提交于 2019-11-27 10:31:17
Could someone explain functional lenses to me? It's a surprisingly difficult subject to google for and I haven't made any progress. All I know is that they provide similar get/set functionality than in OO. A lens consists of two functions, a getter and a setter: data Lens a b = Lens { getter :: a -> b, setter :: b -> a -> a } For example, we might have lenses for the first and second parts of a pair: fstLens :: Lens (a, b) a fstLens = Lens fst $ \x (a, b) -> (x, b) sndLens :: Lens (a, b) b sndLens = Lens snd $ \x (a, b) -> (a, x) The real convenience of lenses is that they compose: compose ::