MackeyRMS / elm-accessors / List.Accessors

List.Accessors

each : Base.Traversal_ (List a) (List b) a b x y

This accessor combinator lets you access values inside List.

import Accessors exposing (..)
import List.Accessors as List
import Lens as L

listRecord : { foo : List { bar : Int } }
listRecord = { foo = [ {bar = 2}
                     , {bar = 3}
                     , {bar = 4}
                     ]
             }

all (L.foo << List.each << L.bar) listRecord
--> [2, 3, 4]

map (L.foo << List.each << L.bar) ((+) 1) listRecord
--> {foo = [{bar = 3}, {bar = 4}, {bar = 5}]}

eachIdx : Base.Traversal_ (List a) (List b) ( Basics.Int, a ) b x y

This accessor lets you traverse a list including the index of each element

import Accessors exposing (..)
import List.Accessors as List
import Lens as L

listRecord : {foo : List {bar : Int}}
listRecord = { foo = [ {bar = 2}
                     , {bar = 3}
                     , {bar = 4}
                     ]
             }

multiplyIfGTOne : (Int, { bar : Int }) -> { bar : Int }
multiplyIfGTOne ( idx, ({ bar } as rec) ) =
    if idx > 0 then
         { bar = bar * 10 }
    else
        rec

all (L.foo << List.eachIdx) listRecord
--> [(0, {bar = 2}), (1, {bar = 3}), (2, {bar = 4})]

map (L.foo << List.eachIdx) multiplyIfGTOne listRecord
--> {foo = [{bar = 2}, {bar = 30}, {bar = 40}]}

all (L.foo << List.eachIdx << ixd L.bar) listRecord
--> [2, 3, 4]

map (L.foo << List.eachIdx << ixd L.bar) ((+) 1) listRecord
--> {foo = [{bar = 3}, {bar = 4}, {bar = 5}]}

at : Basics.Int -> Base.Traversal (List a) a x y

at: Structure Preserving accessor over List members.

import Accessors exposing (..)
import List.Accessors as List
import Lens as L

list : List { bar : String }
list = [{ bar = "Stuff" }, { bar =  "Things" }, { bar = "Woot" }]

try (List.at 1) list
--> Just { bar = "Things" }

try (List.at 9000) list
--> Nothing

try (List.at 0 << L.bar) list
--> Just "Stuff"

set (List.at 0 << L.bar) "Whatever" list
--> [{ bar = "Whatever" }, { bar =  "Things" }, { bar = "Woot" }]

set (List.at 9000 << L.bar) "Whatever" list
--> list

id : Basics.Int -> Base.Traversal (List { a | id : Basics.Int }) { a | id : Basics.Int } x y

id: Structure Preserving accessor over List members.

import Accessors exposing (..)
import List.Accessors as List
import Lens as L

list : List { id : Int, bar : String }
list = [{ id = 7, bar = "Stuff" }, { id = 1, bar =  "Things" }, { id = 5, bar = "Woot" }]

try (List.id 1) list
--> Just { id = 1, bar = "Things" }

try (List.id 0) list
--> Nothing

try (List.id 7 << L.bar) list
--> Just "Stuff"

set (List.id 7 << L.bar) "Whatever" list
--> [{ id = 7, bar = "Whatever" }, { id = 1, bar =  "Things" }, { id = 5, bar = "Woot" }]

set (List.id 9000 << L.bar) "Whatever" list
--> list