使用 Control.Lens 从子列表中获取值列表
Get list of values from sublist with Control.Lens
我正在尝试从子结构中获取值列表。
我有以下结构
("Value",[(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5")])
我正在尝试获取列表中元组的第二个元素。
["1", "2" , "3" , "4" , "5"]
我要绑定的表达式是:
view (_2 . toListOf . _2) a
我也试过traverse
。但似乎 traverse
对列表有折叠效果。我需要结果作为列表。
Prelude Control.Lens> let a = ("Value", [(i, show i)|i<-[1..5]]) :: (String, [(Int, String)])
Prelude Control.Lens> a
("Value",[(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5")])
Prelude Control.Lens> view (_2 . toListOf . _2) a
<interactive>:36:7: error:
• Couldn't match type ‘[]’ with ‘Const t’
Expected type: Getting t (String, [(Int, String)]) t
Actual type: (t -> Const t t)
-> (String, [(Int, String)]) -> [(String, [(Int, String)])]
• In the first argument of ‘view’, namely ‘(_2 . toListOf . _2)’
In the expression: view (_2 . toListOf . _2) a
In an equation for ‘it’: it = view (_2 . toListOf . _2) a
• Relevant bindings include it :: t (bound at <interactive>:36:1)
<interactive>:36:23: error:
• Couldn't match type ‘Const t t0’
with ‘[(Int, String)]
-> Const (Data.Monoid.Endo [[(Int, String)]]) [(Int, String)]’
Expected type: (t -> Const t t)
-> Getting
(Data.Monoid.Endo [[(Int, String)]])
[(Int, String)]
[(Int, String)]
Actual type: (t -> Const t t)
-> ([(Int, String)]
-> Const (Data.Monoid.Endo [[(Int, String)]]) [(Int, String)])
-> Const t t0
• In the second argument of ‘(.)’, namely ‘_2’
In the second argument of ‘(.)’, namely ‘toListOf . _2’
In the first argument of ‘view’, namely ‘(_2 . toListOf . _2)’
• Relevant bindings include it :: t (bound at <interactive>:36:1)
Prelude Control.Lens>
文档说:
View the value pointed to by a Getter, Iso or Lens or the result of folding over all the results of a Fold or Traversal that points at a monoidal value.
您需要使用 toListOf
而不是 view
。例如:
toListOf (_2.traverse._2) a
或
a ^.. _2 . traverse . _2
所以,toListOf
不是镜头,它只是像 view
一样的另一个运算符,但它提取了 Fold 的目标列表。
我正在尝试从子结构中获取值列表。 我有以下结构
("Value",[(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5")])
我正在尝试获取列表中元组的第二个元素。
["1", "2" , "3" , "4" , "5"]
我要绑定的表达式是:
view (_2 . toListOf . _2) a
我也试过traverse
。但似乎 traverse
对列表有折叠效果。我需要结果作为列表。
Prelude Control.Lens> let a = ("Value", [(i, show i)|i<-[1..5]]) :: (String, [(Int, String)])
Prelude Control.Lens> a
("Value",[(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5")])
Prelude Control.Lens> view (_2 . toListOf . _2) a
<interactive>:36:7: error:
• Couldn't match type ‘[]’ with ‘Const t’
Expected type: Getting t (String, [(Int, String)]) t
Actual type: (t -> Const t t)
-> (String, [(Int, String)]) -> [(String, [(Int, String)])]
• In the first argument of ‘view’, namely ‘(_2 . toListOf . _2)’
In the expression: view (_2 . toListOf . _2) a
In an equation for ‘it’: it = view (_2 . toListOf . _2) a
• Relevant bindings include it :: t (bound at <interactive>:36:1)
<interactive>:36:23: error:
• Couldn't match type ‘Const t t0’
with ‘[(Int, String)]
-> Const (Data.Monoid.Endo [[(Int, String)]]) [(Int, String)]’
Expected type: (t -> Const t t)
-> Getting
(Data.Monoid.Endo [[(Int, String)]])
[(Int, String)]
[(Int, String)]
Actual type: (t -> Const t t)
-> ([(Int, String)]
-> Const (Data.Monoid.Endo [[(Int, String)]]) [(Int, String)])
-> Const t t0
• In the second argument of ‘(.)’, namely ‘_2’
In the second argument of ‘(.)’, namely ‘toListOf . _2’
In the first argument of ‘view’, namely ‘(_2 . toListOf . _2)’
• Relevant bindings include it :: t (bound at <interactive>:36:1)
Prelude Control.Lens>
文档说:
View the value pointed to by a Getter, Iso or Lens or the result of folding over all the results of a Fold or Traversal that points at a monoidal value.
您需要使用 toListOf
而不是 view
。例如:
toListOf (_2.traverse._2) a
或
a ^.. _2 . traverse . _2
所以,toListOf
不是镜头,它只是像 view
一样的另一个运算符,但它提取了 Fold 的目标列表。