|
|
|
|
|
by yummyfajitas
4005 days ago
|
|
Sure, here's an example with lenses, taken from actual code I've written: (for {
_ <- EventLenses.duid := UserDuid("12345")
_ <- EventLenses.contexts := Seq(EventContext.NilContext)
_ <- EventLenses.url := "http://news.ycombinator.com"
} yield ()).run(originalEvent)
This code will take an event, set it's duid, contexts and url. If you squint hard enough it almost looks like: originalEvent.duid = ...
originalEvent.contexts = ...
originalEvent.url = ...
The thing that's cool about lenses is that they compose nicely. E.g., the contexts lens is defined as: val contexts: Lens[Event, Seq[EventContext]] = query >=> getterMLens("cx") >=> Lens.lensu(...)
This means that first we take the query lens, compose it with a lens that gets the "cx" parameter, then do the actual work of decoding the "cx" param. |
|