MackeyRMS / elm-accessors / SelectList.Accessors

This module exposes some helpers for "miyamoen/select-list"

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

This accessor combinator lets you access values inside List.

import Accessors exposing (..)
import SelectList.Accessors 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 }]
    }

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

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

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

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

import Accessors exposing (..)
import SelectList.Accessors 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 }) -> { bar : Int }
multiplyIfGTOne ( idx, ({ bar } as rec) ) =
    if idx > 0 then
        { bar = bar * 10 }
    else
        rec


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

map (L.foo << SL.eachIdx) multiplyIfGTOne listRecord
--> { foo = SelectList.fromLists [{ bar = 1 }] { bar = 20 } [{ bar = 30 }, { bar = 40 }] }

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

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

selected : Base.Lens ls (SelectList a) a x y

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

import Accessors exposing (..)
import SelectList.Accessors 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 }] }

map (L.foo << SL.selected << L.bar) ((*) 10) listRecord
--> { foo = SelectList.fromLists [{ bar = 1 }] { bar = 20 } [{ bar = 3 }, { bar = 4 }] }