【发布时间】:2020-03-05 12:06:52
【问题描述】:
考虑以下几点:
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Typex = Typex
{ _level :: Int
, _coordinate :: (Int, Int)
, _connections :: [(Int,(Int,Int))]
} deriving Show
makeLenses ''Typex
initTypexLevel :: Int -> Int -> Int -> [Typex]
initTypexLevel a b c = [ Typex a (x, y) [(0,(0,0))]
| x <- [0..b], y <- [0..c]
]
buildNestedTypexs :: [(Int, Int)] -> [[Typex]]
buildNestedTypexs pts
= setConnections [ initTypexLevel i y y
| (i,(_,y)) <- zip [0..] pts
]
setConnections :: [[Typex]] -> [[Typex]]
setConnections = ?
我如何使用镜头来修改所有Typexs 中的connections 类型为[[Typex]] -> [[Typex]] 的函数,使得在每个Typex 中
connections = [(level of Typex being modified +1, (x, y))] where
x,y = 0..(length of next [Typex] in [[Typex]])/2
X 和 y 都需要经过下一个 [Typex] 的那个长度。如果可能,最终的 [Typex] 应保持不变。所以同一个[Typex]中每个Typex的所有连接都是一样的。
setConnections $ buildNestedTypexs [(0,1),(1,1)] 的输出应该是:
[ [ Typex { _level = 0
, _coordinate = (0,0)
, _connections = [(1,(0,0)), (1,(0,1)), (1,(1,0)), (1,(1,1))] }
, Typex { _level = 0
, _coordinate = (0,1)
, _connections = [(1,(0,0)), (1,(0,1)), (1,(1,0)), (1,(1,1))] }
, Typex { _level = 0
, _coordinate = (1,0)
, _connections = [(1,(0,0)), (1,(0,1)), (1,(1,0)), (1,(1,1))] }
, Typex { _level = 0
, _coordinate = (1,1)
, _connections = [(1,(0,0)), (1,(0,1)), (1,(1,0)), (1,(1,1))] }
]
,[ Typex { _level = 1
, _coordinate = (0,0)
, _connections = [(0,(0,0))] }
, Typex { _level = 1
, _coordinate = (0,1)
, _connections = [(0,(0,0))] }
, Typex { _level = 1
, _coordinate = (1,0)
, _connections = [(0,(0,0))] }
, Typex { _level = 1
, _coordinate = (1,1)
, _connections = [(0,(0,0))] }
]]
我想我需要import Control.Lens.Indexed,但仅此而已,因此感谢所有帮助。
【问题讨论】:
-
一个具体的例子将有助于了解问题到底是什么。
-
@leftaroundabout 我添加了更多解释和预期结果。其中
Typexs 与级别 1 的连接保持不变,因为它们是作为参数给出的最后一个 [Typex] 的一部分。 -
好的,我想现在技术上已经很清楚了,但是......你不能简化/概括一下这个问题吗?这似乎仍然涉及与您所要求的核心无关的特定领域的细节。
-
@leftaroundabout 感谢您使它更具可读性。我从连接中删除了 Double 部分,因为它与问题无关。除此之外,我真的不知道如何在不添加可能使其更难理解的变量的情况下使其更通用。
-
我们也可以去掉
c参数并完全避免嵌套元组吗?
标签: haskell nested indices haskell-lens custom-data-type