This module exposes some helpers for "miyamoen/select-list"
This accessor lets you traverse a list including the index of each element
import Accessors exposing (..)
import Accessors.SelectList as SL
import Lens as L
import SelectList exposing (SelectList)
listRecord : { foo : SelectList { bar : Int } }
listRecord =
{ foo = SelectList.fromLists [{ bar = 1 }] { bar = 2 } [{ bar = 3 }, { bar = 4 }]
}
multiplyIfGTOne : (Int, { bar : Int }) -> (Int, { bar : Int })
multiplyIfGTOne ( idx, ({ bar } as rec) ) =
if idx > 0 then
( idx, { bar = bar * 10 } )
else
(idx, rec)
get (L.foo << SL.eachIdx) listRecord
--> SelectList.fromLists [(0, {bar = 1})] (1, {bar = 2}) [(2, {bar = 3}), (3, {bar = 4})]
over (L.foo << SL.eachIdx) multiplyIfGTOne listRecord
--> { foo = SelectList.fromLists [{ bar = 1 }] { bar = 20 } [{ bar = 30 }, { bar = 40 }] }
get (L.foo << SL.eachIdx << snd << L.bar) listRecord
--> SelectList.fromLists [1] 2 [3, 4]
over (L.foo << SL.eachIdx << snd << L.bar) ((+) 1) listRecord
--> {foo = SelectList.fromLists [{bar = 2}] {bar = 3} [{bar = 4}, {bar = 5}]}This accessor lets you traverse a list including the index of each element
import Accessors exposing (..)
import Accessors.SelectList as SL
import Lens as L
import SelectList exposing (SelectList)
listRecord : { foo : SelectList { bar : Int } }
listRecord =
{ foo = SelectList.fromLists [{ bar = 1 }] { bar = 2 } [{ bar = 3 }, { bar = 4 }]
}
multiplyIfGTOne : (Int, { bar : Int }) -> (Int, { bar : Int })
multiplyIfGTOne ( idx, ({ bar } as rec) ) =
if idx > 0 then
( idx, { bar = bar * 10 } )
else
(idx, rec)
get (L.foo << SL.selected << L.bar) listRecord
--> 2
set (L.foo << SL.selected << L.bar) 37 listRecord
--> { foo = SelectList.fromLists [{ bar = 1 }] { bar = 37 } [{ bar = 3 }, { bar = 4 }] }
over (L.foo << SL.selected << L.bar) ((*) 10) listRecord
--> { foo = SelectList.fromLists [{ bar = 1 }] { bar = 20 } [{ bar = 3 }, { bar = 4 }] }
This accessor combinator lets you access values inside List.