haskell - How to take an 2D slice with Lens -


import qualified data.vector v import control.lens import data.vector.lens  v = v.fromlist [v.fromlist [1..3], v.fromlist [4..6], v.fromlist [7..9]] 

1d slice (for example) :

*main> v ^. sliced 1 2 fromlist [fromlist [4,5,6],fromlist [7,8,9]] 

2d sclice: should write result?

*main> v ^. sliced 1 2 {- ??????? -}  sliced 0 2   --  or not so? v.fromlist [v.fromlist [4,5], v.fromlist [7,8]] 

this should it

insliced :: int -> int -> lens' (v.vector (v.vector a)) (v.vector (v.vector a)) insliced n f m = f (v.map (v.slice n) m)     <&> v.zipwith (\a b -> v.// zip [i..i+n-1] (v.tolist b)) m 

then

λ v ^. sliced 1 2 . insliced 0 2 fromlist [fromlist [4,5],fromlist [7,8]] 

this has similar requirements sliced valid.

it's worth mentioning there's general version of called column linear. can't used vector because vectors aren't representable (because because it's size isn't statically known). v3:

λ v3 (v3 1 2 3) (v3 4 5 6) (v3 7 8 9) ^. _yz . column _xy v2 (v2 4 5) (v2 7 8) 

you write own (less safe) version vectors:

vcolumn :: alens' b -> lens' (v.vector a) (v.vector b) vcolumn l f m = f (v.map (^# l) m) <&> v.zipwith (\a b -> & l #~ b) m 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

python - Exception while importing pymorphy2 -

c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -