lens

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

孤者浪人 提交于 2020-01-17 19:38:13
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Output: 5 Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5. 分析: 求出最长递增子序列的个数,我们使用lens[i]表示以nums[i]为结尾的递增子序列中元素的个数也就是长度,以time[i]表示以nums[i]为结尾的递增子序列出现的次数,遍历nums数组,维护这两个数组。 遍历nums[i]这个元素,都要和i前面的元素进行比较(用j进行遍历),如果nums[i]大于nums[j],意味着nums[i]可以拼接到nums[j]后面,产生一个更长的子序列,如果lens[i]

XNA Lens flare

血红的双手。 提交于 2020-01-11 21:32:43
  Lens 就是镜头的意思, flare 就是闪光可闪耀的意思, Lens flare 即是光晕。在 3D 游戏中,无论是室外场景的太阳还是室内场景中的灯光效果,都有要用到 Lens flare 的效果。   经过两天多的努力终于把这个效果做出来了,虽然还有点小问题,但总的来说已经实现了。   首先还是老样的,完成后的效果(也是目标效果):   为了节省时间,这个 Demo 中不少的组件都是以前做的,新加的东西也都做成了组件。镜头的控制也是通过以前写的 CameraLib.DLL 控制。   先是引用了 CameraLib 和 Terrain 。后测试效果为   之后,加入一个 SkyBox 类:   代码如下: Codepublic class Skybox :DrawableGameComponent { Model skybox; Effect skyboxeffect; TextureCube textu; Camera camera; Vector3 position; Matrix world = Matrix.Identity; public Skybox(Game game,Camera camera): base(game) { this.camera = camera; } protected override void LoadContent() {

Zoom instance over Free Monad

若如初见. 提交于 2020-01-05 04:24:10
问题 I'm attempting to build a free monad (using free) which acts just like a StateT monad, but which allows you to also run monads over a base state AppState . I have a separate constructor LiftAction which holds those types. The idea is that you keep zoom ing Actions down until they reach AppState, which can store different states inside its extension map. Here was my earlier (failed) attempt using mtl: Lift through nested state transformers (mtl) Anyways, since it's basically a wrapper over

indexing list with Control.Lens requires Monoid constraint

若如初见. 提交于 2020-01-01 09:19:36
问题 The following code doesn't compile: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data MyType = MyType Int data Outer = Outer { _inners :: [ Inner ] } data Inner = Inner { _val :: MyType } $(makeLenses ''Outer) $(makeLenses ''Inner) i1 = Inner (MyType 1) i2 = Inner (MyType 2) o = Outer [i1, i2] x = o ^. inners . ix 0 . val giving this error Toy.hs:17:23: No instance for (Data.Monoid.Monoid MyType) arising from a use of `ix' Possible fix: add an instance declaration for (Data.Monoid

Lens / Prism with error handling

狂风中的少年 提交于 2020-01-01 01:18:07
问题 Let's say I have a pair of conversion functions string2int :: String -> Maybe Int int2string :: Int -> String I could represent these fairly easily using Optics. stringIntPrism :: Prism String Int However if I want to represent failure reason, I'd need to keep these as two separate functions. string2int :: String -> Validation [ParseError] Int int2string :: Int -> String` For this simple example Maybe is perfectly fine, since we can always assume that a failure is a parse failure, thus we don

How would I use lens in Haskell to duplicate Python's enumerate?

自古美人都是妖i 提交于 2019-12-30 10:06:31
问题 Python's enumerate on lists can be written as zip [0..] . I looked at Control.Lens.Traversal and Control.Lens.Indexed, but I couldn't figure out how to use lenses to generalize this to any reasonable container (I hesitate to say "Traversable"). I'm guessing itraverse or itraverseOf is key. 回答1: If you're using a container that is an instance of FunctorWithIndex then you can simply use imap (,) : > imap (,) "abc" [(0,'a'),(1,'b'),(2,'c')] But if the index isn't the position this won't work: >

How to modify using a monadic function with lenses?

喜你入骨 提交于 2019-12-30 05:57:06
问题 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 -

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

旧巷老猫 提交于 2019-12-29 09:10:16
问题 This question already has answers here : How to derive instances for records with type-families (2 answers) Closed 2 years ago . 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

Simulating interacting stateful objects in Haskell

混江龙づ霸主 提交于 2019-12-29 08:27:18
问题 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

How does `lens` work?

旧时模样 提交于 2019-12-24 12:51:55
问题 I mean, not the simple stuff like this (from here): strike :: StateT Game IO () strike = do lift $ putStrLn "*shink*" boss.health -= 10 But things like using lens to map over types from Linear . How would I express this in terms of lens: vecMod :: (Integral a) => V2 a -> V2 a -> V2 a vecMod (V2 x1 y1) (V2 x2 y2) = V2 (x1 `mod` x2) (y1 `mod` y2) Another example: my current code is full of small expressions like this: isAt :: Thing -> Position -> Game Bool isAt thing pos = do b <- use board