lens

How to make the product of two lenses?

限于喜欢 提交于 2019-12-01 02:02:14
问题 If I have two lenses: foo :: Lens' X Foo bar :: Lens' X Bar Is there a way to construct a product lens: foobar :: Lens' X (Foo, Bar) foobar = ... foo bar or is it impossible? 回答1: In general case, this is impossible. Probably the most common case when you have lenses to different fields of the record, the lenses are disjoint, so you can make a lawful lens. But in general it's not true. This is why the combinator is not provided in the libraries, even it would be easy to write. Assume lensProd

Haskell: Template Haskell and the scope

让人想犯罪 __ 提交于 2019-11-30 23:22:38
问题 This code is compiled fine: data None = None { _f :: Int } type Simpl = Env type Env = Int However, I got an error with this code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data None = None { _f :: Int } type Simpl = Env makeLenses ''None type Env = Int Error: Not in scope: type constructor or class `Env' I just added a single line makeLenses ''None between type declarations. This means TemplateHaskell code could change the scope of type constructor? Does anyone know the detail

How to modify using a monadic function with lenses?

自作多情 提交于 2019-11-30 17:52:47
I needed a lens function that works like over , but with monadic operations: overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) While this function is easy to define (it's actually just an identity modulo WrappedMonad ), I wonder are such functions defined somewhere in lens ? {-# LANGUAGE RankNTypes #-} import Control.Applicative import Control.Lens overF :: (Functor f) => Lens s t a b -> (a -> f b) -> (s -> f t) overF l = l overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) overM l = (unwrapMonad .) . l . (WrapMonad .) in Control.Lens.Traversal: traverseOf :: Over p f

Lenses over Comonads or Representable

情到浓时终转凉″ 提交于 2019-11-30 17:39:19
问题 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! 回答1: Comonad doesn't give

Can I make a Lens with a Monad constraint?

妖精的绣舞 提交于 2019-11-30 11:45:31
Context: This question is specifically in reference to Control.Lens (version 3.9.1 at the time of this writing) I've been using the lens library and it is very nice to be able to read and write to a piece (or pieces for traversals) of a structure. I then had a though about whether a lens could be used against an external database. Of course, I would then need to execute in the IO Monad . So to generalize: Question: Given a getter, (s -> m a) and an setter (b -> s -> m t) where m is a Monad, is possible to construct Lens s t a b where the Functor of the lens is now contained to also be a Monad?

leetcode练习

坚强是说给别人听的谎言 提交于 2019-11-29 23:45:23
一、两数之和#解法1class Solution(object): def twoSum(self, nums, target): lens = len(nums) for i in range(0,lens): for j in range(i+1,lens): num1 = nums[i] num2 = nums[j] if (num1 + num2 == target): # m = nums.index(num1) # n = nums.index(num2) lis = [i,j] return (lis) twoSum(0,[4,3,2], 5)#解法2class Solution(object): def twoSum(self, nums, target): j = -1 lens = len(nums) for i in range(1,lens): temp = nums[:i] if (target - nums[i]) in temp: j = temp.index(target - nums[i]) break if j >= 0: lis = [j,i] return (lis) twoSum(0,[4,3,2], 5) 来源: https://www.cnblogs.com/xiaojingjingzhuanshu/p/11539879.html

leetcode 10. Regular Expression Matching(正则表达式匹配)

帅比萌擦擦* 提交于 2019-11-29 19:10:57
Given an input string ( s ) and a pattern ( p ), implement regular expression matching with support for '.' and '*' . '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z . p could be empty and contains only lowercase letters a-z , and characters like . or * . Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation: '*' means

Can I make a Lens with a Monad constraint?

牧云@^-^@ 提交于 2019-11-29 17:27:00
问题 Context: This question is specifically in reference to Control.Lens (version 3.9.1 at the time of this writing) I've been using the lens library and it is very nice to be able to read and write to a piece (or pieces for traversals) of a structure. I then had a though about whether a lens could be used against an external database. Of course, I would then need to execute in the IO Monad . So to generalize: Question: Given a getter, (s -> m a) and an setter (b -> s -> m t) where m is a Monad,

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