lens

Lenses over Comonads or Representable

社会主义新天地 提交于 2019-12-08 07:44:11
问题 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

Point-free lens creation does not type check

筅森魡賤 提交于 2019-12-07 04:07:02
问题 In the function test , I traverse over a list, generate lenses from it's members, and then print some data. This works when I use a pointful call style. It fails to typecheck when I make it point-free. Why is this the case, and how can I solve this problem? It looks like to me that GHC is not retaining the information that the higher-ranked f (in the lens) is a Functor when using point-free style, but I'm not too sure. I'm using GHC 7.8.3 {-# LANGUAGE RankNTypes #-} {-# LANGUAGE

388. Longest Absolute File Path

白昼怎懂夜的黑 提交于 2019-12-06 13:50:24
class Solution { public int lengthLongestPath(String input) { String [] arr=input.split("\n"); int [] lens= new int[arr.length]; int max=0; for(String s:arr){ int lastBackT= s.lastIndexOf("\t"); int level = lastBackT+1; int parentLen = level==0? 0: lens[level-1]+1; int nameLen = s.length()-lastBackT-1; lens[level] = parentLen+nameLen; if(s.contains(".")) { max = Math.max(max,lens[level]); } } return max; } } The first thought is DFS, however, we're able to deal it with array 来源: https://www.cnblogs.com/zg1005/p/11987302.html

How to get classy lenses with overloaded field names?

扶醉桌前 提交于 2019-12-06 02:14:13
I'm trying to build lenses for records which have the same field names. Along with that, I'm trying to "wrap/extend" these base records and want the same field names to work for the wrapped/extended records (which, I believe, classy lenses do). How do I get the following to work: -- Data types for context of the code snippet below data Download = Download { userId :: UserId ,gid :: Gid ,logId :: LogId ,parentId :: Maybe DownloadId ,createdAt :: UTCTime ,updatedAt :: UTCTime } data File = File { downloadId :: DownloadId ,fpath :: String ,len :: Int ,createdAt :: UTCTime ,updatedAt :: UTCTime }

Correct lens distortion using single calibration image in Matlab

让人想犯罪 __ 提交于 2019-12-06 02:08:32
I would like to correct lens distortions on a series of images. All the images were captured with the camera fixed in place, and a checkerboard image from the same set up is also available. After detecting the corners of the distorted checkerboard image, I would like to compute the radial distortion coefficients so that I can correct the images. Similar to the estimateCameraParameters function. Ideally, I would like to use a method similar to Matlab camera calibration however this does not seem to work for cases where only a single calibration image is available (and the images were all

Lens: zooming newtype

痴心易碎 提交于 2019-12-05 18:42:47
I'm interested in getting a zooming functionality for my monad transformer stack which is defined the following way: newtype Awesome a = Awesome (StateT AwesomeState (ExceptT B.ByteString IO) a) deriving (Functor, Applicative, Monad , MonadIO, MonadError B.ByteString , MonadState AwesomeState) My AwesomeState is deeply nested record, so using zoom would greatly help me in updating some of the fields. But the problem is that zoom doesn't work out of the box for my newtype. Couldn't match type ‘Control.Lens.Internal.Zoom.Zoomed Awesome’ with ‘Control.Lens.Internal.Zoom.Zoomed m0’ I found an

How do I make lenses from a record in GHCi

拥有回忆 提交于 2019-12-05 11:13:23
问题 I want to play around with the Lens library a bit. I've loaded it into GHCi and created a record data type with the appropriate underscores: > data Foo a = Foo {_arg1 :: Int, _arg2 :: [a]} I would like to make the lenses for Foo using the makeLenses template. I would like to do this without needing to read through the entire set of Template-Haskell docs. What incantation can I type in at the GHCi prompt to get this to work? 回答1: Tested in GHCi 7.8.3: :set -XTemplateHaskell :m +Control.Lens :{

When manipulating immutable datastructures, what's the difference between Clojure's assoc-in and Haskell's lenses?

本小妞迷上赌 提交于 2019-12-05 10:33:08
问题 I need to manipulate and modify deeply nested immutable collections (maps and lists), and I'd like to better understand the different approaches. These two libraries solve more or less the same problem, right? How are they different, what types of problem is one approach more suitable for over the other? Clojure's assoc-in Haskell's lens 回答1: Clojure's assoc-in lets you specify a path through a nested data struture using integers and keywords and introduce a new value at that path. It has

Python实现的几个算法

青春壹個敷衍的年華 提交于 2019-12-05 09:37:48
一、冒泡排序 arr = [5,3,1,2,4] n = len(arr) for i in range(n - 1): for j in range(n - 1 - i): print(i,j) if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] print(arr) print(arr) arr为要排序的列表 该代码实现的分析过程 这是我在本子上的具体推导过程。 需要注意的是,嵌套for循环,会先把子循环里的i执行到条件结束后再进行外循环的i值变量 即当i = 0的时候, j第一次循环为0. j=0的代码执行结束后,j = 1,i=0不变 然后j=2,j=3, 因为range(n-1-i),n=5,即为range(4) ---->0,1,2,3。 执行结束后,i 才会变成1.再次 进入j循环。 j = range(n-1-1) = range(3) ----->0,1,2 具体的推到过程图上很清晰易懂 二、移动数组 输入 :[1,2,3,4,5,6,7] k = 3 输出 : [5,6,7,1,2,3,4] k = 3 arr = [1, 2, 3, 4, 5, 6, 7] lens = len(arr) k = k % lens #因为K不一定小于lens,所以当k>lens时,位移k

Isomorphisms between 3 and more types using lens

自闭症网瘾萝莉.ら 提交于 2019-12-05 01:28:26
Inspired by a question on polymorphic function between ADTs I'm trying to create isomorphisms between multiple (not just 2) types, so that every time I need an isomorphic but not the same type, I can sprinkle my code with some convert . Suppose I have 3 ADTs: data AB = A | B deriving (Show) data CD = C | D deriving (Show) data EF = E | F deriving (Show) Using lens I can implement 2 isomorphisms between AB and CD, and CD and EF: {-# LANGUAGE MultiParamTypeClasses #-} class Isomorphic a b where convert :: Iso' a b instance Isomorphic AB CD where convert = iso ab2cd cd2ab where ab2cd A = C ab2cd