Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonrichardson/binoculars
Monadic Lenses for data
https://github.com/simonrichardson/binoculars
Last synced: 22 days ago
JSON representation
Monadic Lenses for data
- Host: GitHub
- URL: https://github.com/simonrichardson/binoculars
- Owner: SimonRichardson
- License: mit
- Created: 2014-09-11T10:34:51.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-08T20:08:25.000Z (about 10 years ago)
- Last Synced: 2024-05-01T23:21:22.081Z (6 months ago)
- Language: Go
- Homepage:
- Size: 207 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
binoculars
==========## Lenses
Monadic Lenses for data
Lenses are composable, immutable getters and setters. Composable in
that they allow updating of nested data structures. Immutable in that
the setters return copies of the whole data structure.## Examples
The [example](example.go) is a lot more up to date by it's very nature of being
code, but to give you an idea, see the following:### Nested updating
```go
type Person struct {
Name string
Location Location
}
type Location struct {
Number int
Street string
Postcode int
}person := Person{
Name: "Joe Smith",
Location: Location{
Number: 1006,
Street: "Pearl St",
Postcode: 80302,
}
}locationLens = binoculars.ObjectLens('Location'),
numberLens = binoculars.ObjectLens('Number'),
store = locationLens.AndThen(numberLens).Run(person);console.log(store.Get());
// 1006console.log(store.Set(1007));
// { Name: 'Joe Smith',
// Location: { Number: 1007, Street: 'Pearl St', Postcode: 80302, },
// }
```### Notes
Although some of the structs are named after other functional language types
(Store, etc), they are not in fact the same. They're close, but each type has
been locked down to a tighter set of types, to prevent the need for type casting.
Which means they'll not pass any monadic laws!!