2016-12-06 2 views
0

サブリストから値のリストを取得しようとしています。 私は構造Control.Lensでサブリストから値のリストを取得

("Value",[(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5")]) 

を以下持っていると私は、リストからタプルの2番目の要素を取得しようとしています。

[ "1"、 "2"、 "3"、 "4"、 "5"]私は抱き合わせてい

式(_2。toListOf。_2)

図である

私もトラバースを試みました。しかし、トラバースはリストに折り畳み効果があるようです。私は結果をリストとして必要とします。

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> 

答えて

2

ドキュメントは言う:

表示値はゲッター、ISOやレンズやmonoidal値を指し倍またはトラバーサルのすべての結果の上に折るの結果によって指されます。

toListOfの代わりにviewを使用する必要があります。たとえば:だから

toListOf (_2.traverse._2) a 

それとも

a ^.. _2 . traverse . _2 

toListOfはレンズではない、それはviewのような単なる別のオペレータだが、それは、フォールドのターゲットのリストを抽出します。

+0

チャームのように働いた。ありがとうございました。 – yilmazhuseyin

関連する問題