Skip to content

Commit

Permalink
Traversals (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastes authored Apr 17, 2020
1 parent e7e6588 commit 5408099
Show file tree
Hide file tree
Showing 6 changed files with 791 additions and 110 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,51 @@ A Optional is a weaker Lens and a weaker Prism.
modifyAddressRegion address = Optional.modify addressRegionOptional modifyRegion address
```

## Traversal

A Traversal allows you to modify many elements at once.

```elm
type alias Traversal a b =
(b -> b) -> a -> a
```

(`Traversal a b` is just an alias for a function that applies
a transformation over `b` elements of a larger `a` structure.)

###### Example

```elm
firstNameLens : Lens Friend String
firstNameLens =
Lens .firstName (\b a -> { a | firstName = b })

bestFriendsTraversal : Traversal (List Friend) Friend
bestFriendsTraversal =
Traversal.some
Traversal.list
(\friend -> friend.value == Best)

friendsLens : Lens Account (List Friend)
friendsLens =
Lens .friends (\b a -> { a | friends = b })

firstNamesOfBestFriends : Traversal Account String
firstNamesOfBestFriends =
friendsLens
|> Compose.lensWithTraversal bestFriendsTraversal
|> Compose.traversalWithLens firstNameLens

upcaseBestFriendsFirstNames : Account -> Account
upcaseBestFriendsFirstNames account =
Traversal.modify firstNamesOfBestFriends String.toUpper
```

## Common
Common lenses/prisms/optionals that most projects will use.

#### Step into a `Maybe` value.
```elm
```elm
maybe.set 5 Nothing
> Just 5
```
Expand Down
5 changes: 3 additions & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"name": "arturopala/elm-monocle",
"summary": "Library providing functional tools to manipulate complex records",
"license": "MIT",
"version": "2.1.0",
"version": "2.2.0",
"exposed-modules": [
"Monocle.Iso",
"Monocle.Prism",
"Monocle.Lens",
"Monocle.Optional",
"Monocle.Traversal",
"Monocle.Common",
"Monocle.Compose"
],
Expand All @@ -19,4 +20,4 @@
"test-dependencies": {
"elm-explorations/test": "1.1.0 <= v < 1.1.1"
}
}
}
Loading

0 comments on commit 5408099

Please sign in to comment.