Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bchiquet/elm-accessors
Accessors, a library implementing lenses for Elm.
https://github.com/bchiquet/elm-accessors
elm elm-lang lenses
Last synced: 3 months ago
JSON representation
Accessors, a library implementing lenses for Elm.
- Host: GitHub
- URL: https://github.com/bchiquet/elm-accessors
- Owner: bChiquet
- License: mit
- Created: 2018-07-14T09:53:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-26T23:18:08.000Z (about 2 years ago)
- Last Synced: 2024-10-12T20:20:10.870Z (3 months ago)
- Topics: elm, elm-lang, lenses
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/bChiquet/elm-accessors/latest
- Size: 30.3 KB
- Stars: 19
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
The Accessors library
=====================This library provides a way to describe relations between a container and its
content, and use that description to manipulate arbitrary data more easily.# Build your relations
There are two kinds of relations between a container and its content: 1:1
relations (e.g. a record and its field) and 1:n relations (e.g. a `List` can
contain 0-n elements, a `Maybe` can contain 0-1 elements).For 1:1 relations, the `makeOneToOne` function will let you build an accessor
by describing how to get the sub-element from the super-element, and how to map
a function over it. For instance, with a record:```elm
recordFoo =
makeOneToOne
.foo
(\change record -> {record | foo = change record.foo})recordBar =
makeOneToOne
.bar
(\change record -> {record | bar = change record.bar})
```1:n relations are more complex in terms of abstraction, but they are usually
very easy to implement:```elm
onEach =
makeOneToN
List.map
List.maptry =
makeOneToN
Maybe.map
Maybe.map
```# Combine your relations
Accessors can be composed easily to describe relations:
```elm
myData = { foo = [ {bar = 3}
, {bar = 2}
, {bar = 0}
]
}myAccessor = recordFoo << onEach << recordBar
```# Manipulate your data easily
Then you use an action function to determine which kind of operation you want to
do on your data using the accessor```elm
getter = get myAccessor myData
-- returns [3, 2, 0]setter = set myAccessor 2 myData
-- returns {foo = [{bar = 2}, {bar = 2}, {bar = 2}]}transform = over myAccessor (\n -> n*2) myData
-- returns {foo = [{bar = 6}, {bar = 4}, {bar = 0}]}
```# Type-safe and reusable
Applying an accessor on non-matching data structures will yield nice
compile-time errors:```elm
fail = (recordFoo << recordFoo) myData--The 2nd argument to `get` is not what I expect:
--
--293| fail = get (recordFoo << recordFoo) myData
-- ^^^^^^
--This `myData` value is a:
--
-- { foo : List { bar : number } }
--
--But `get` needs the 2nd argument to be:
--
-- { foo : { a | foo : c } }
```Any accessor you make can be composed with any other accessor to match your new
data structures:```elm
myOtherData = {bar = Just [1, 3, 2]}halfWay = try << onEach
myOtherAccessor = recordBar << halfWaygetter = get myOtherAccessor myOtherData
-- returns Just [1, 3, 2]
```
# Play with it in Ellie[Ellie default code with accessors](https://ellie-app.com/4wHNCxgft87a1).
# Contribute
build
```elm make```
run tests
`elm-test`
or
`elm-test-rs`
If you write new accessor combinators that rely on common library datas, I'll be
happy to review and merge. Please include tests for your combinators.