lens

How to simplify calling a field on a polymorphic field name into one typeclass

删除回忆录丶 提交于 2019-12-24 07:35:00
问题 In a previous question I asked how a record field can be made polymorphic when using DuplicateRecordFields. I got an excellent answer for this from @user2407038. He answered the question to my initial spec providing one type class per field, but he mentioned that it could all be simplified into one typeclass. (Note: this too can be generalized to a single class with an additional parameter corresponding to the field name; this is probably outside the scope of this question). I'm not sure how

数组切分(句子拼接)

左心房为你撑大大i 提交于 2019-12-24 01:33:07
题目:有n个句子,每个句子的长度都小于等于m,现在需要将相邻较短的句子拼接再一起,使得句子的数量最少,并且长度仍然不大于m,而且拼接完之后句子的长度的方差最小。求拼接方式。 解题(自己给自己出题,sent_comb3最优): # -*- coding: utf-8 -*- import numpy as np def sent_comb1(sent_lens, max_len=35): """先求总长的平均长度,再切分""" if sum(sent_lens) <= max_len: return [list(sent_lens)] avg_len = sum(sent_lens) // np.ceil(sum(sent_lens) / max_len) rlts = [] for sent_len in sent_lens: if (not rlts) or (rlts and sum(rlts[-1]) >= avg_len): rlts.append([sent_len]) elif sum(rlts[-1]) + sent_len <= max_len: rlts[-1].append(sent_len) else: rlts.append([sent_len]) return rlts def sent_comb2(sent_lens, max_len=35): ""

What's the best way to represent a short bit string?

*爱你&永不变心* 提交于 2019-12-23 09:27:40
问题 I want to represent a string of up to around 120 bits, and speed is critical. I need to be able to build a bitstring by repeated snoc operations, and then to consume it with repeated uncons operations. One idea is to steal the implementation of Word128 from data-dword and use something like this to build: empty = 1 snoc xs x = (xs `shiftL` 1) .|. x But the unconsing seems to get a bit ugly, having to first countLeadingZeros and shift left to eliminate them before being able to read off the

Pass a lens into a funciton

≡放荡痞女 提交于 2019-12-23 00:08:21
问题 How to pass properly a lens into a function with state? Let us consider the next code: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE FlexibleContexts #-} import Control.Lens import Control.Monad.State data Game = Game { _armies :: [Army] } deriving (Show) data Army = Army { _troops :: Int } deriving (Show) makeLenses ''Game makeLenses ''Army data BattleResult = Win | Defeat deriving (Show) offend offender defender = do Just ot <- preuse $ offender.troops Just dt <- preuse $ defender.troops

Use cases of makePrisms with examples

社会主义新天地 提交于 2019-12-22 18:26:40
问题 It's not clear to me the difference between makeLense and makePrisms? I'm aware that when we want to access a nested structure/data then use makeLense like this: data Point = Point { _x :: Int, _y :: Int} data Test= Test {_name :: String, _position :: Point} makeLenses ''Point makeLenses ''Test Then we can access or modify the component of Test or Point. For instance, we define a function: modify :: Test -> Test modify = over (position . x) (*8) So we can have: let t1 = Test {_name= "Me",

Correct lens distortion using single calibration image in Matlab

女生的网名这么多〃 提交于 2019-12-22 09:59:30
问题 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

Isomorphisms between 3 and more types using lens

牧云@^-^@ 提交于 2019-12-22 03:52:39
问题 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

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

Is there a shorthand way to update a specific struct field in racket?

喜夏-厌秋 提交于 2019-12-21 07:12:18
问题 Suppose I have a struct with many fields: (struct my-struct (f1 f2 f3 f4)) If I am to return a new struct with f2 updated, I have to rephrase every other fields: (define s (my-struct 1 2 3 4)) (my-struct (my-struct-f1 s) (do-something-on (my-struct-f2 s)) (my-struct-f3 s) (my-struct-f4 s)) Which is redundant and would be a source of bugs if I update the number of the fields or changed their orders. I really wonder if there's a such way I can update a specific field for a struct like: (my

How to combine lenses in “parallel”

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 17:35:57
问题 I'm new to the excelent Control.Lens and I'm trying to combine 2 lens in "parallel" (not in sequence) like I would do with `Control.Arrow.&&&). If I take the example from the lens documentation: `data Foo a = Foo { _baz :: Int, _bar :: Int, a } I would like to be able to do stuff like : >> let foo = (bar &&& baz) .~ (1, 20) $ Foo undefined undefined "FOO" >> foo ^. (bar &&& baz) (1, 20) I've looked everywhere and I could not find a way to do so. Is that because : (&&&) exist with another name