pathikrit / Sauron
Yet another Scala lens macro
Stars: ✭ 166
Programming Languages
scala
5932 projects
Labels
Projects that are alternatives of or similar to Sauron
purescript-barlow-lens
Increase your magnification 🔭 and zoom deep into a record.
Stars: ✭ 32 (-80.72%)
Mutual labels: lenses
Monocle Ts
Functional optics: a (partial) porting of Scala monocle
Stars: ✭ 657 (+295.78%)
Mutual labels: lenses
Language Ext
C# functional language extensions - a base class library for functional programming
Stars: ✭ 3,964 (+2287.95%)
Mutual labels: lenses
Prolens
👓 Profunctor based lightweight implementation of Lenses
Stars: ✭ 63 (-62.05%)
Mutual labels: lenses
Dontfeartheprofunctoroptics
Don't Fear the Profunctor Optics!
Stars: ✭ 367 (+121.08%)
Mutual labels: lenses
Dart functional data
Simple and non-intrusive code generator for lenses and boilerplate of data types
Stars: ✭ 39 (-76.51%)
Mutual labels: lenses
Use Profunctor State
React Hook for state management with profunctor lenses
Stars: ✭ 331 (+99.4%)
Mutual labels: lenses
Frunk
Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
Stars: ✭ 725 (+336.75%)
Mutual labels: lenses
Elm Monocle
Functional abstractions to manipulate complex records in Elm - Iso, Prism, Lens, Optional, Traversal.
Stars: ✭ 137 (-17.47%)
Mutual labels: lenses
Lens
A Racket package for creating and composing pure functional lenses
Stars: ✭ 54 (-67.47%)
Mutual labels: lenses
Sauron
Lightweight lens library in less than 50-lines of Scala:
case class Person(address: Address)
case class Address(street: Street)
case class Street(name: String)
val person = Person(Address(Street("1 Functional Rd.")))
import com.github.pathikrit.sauron._
lens(person)(_.address.street.name)(_.toUpperCase)
There is zero overhead; the lens
macro simply expands to this during compilation:
person.copy(address = person.address.copy(
street = person.address.street.copy(
name = (person.address.street.name).toUpperCase)
)
)
Simple setters:
lens(person)(_.address.street.name).setTo("1 Objective Rd.")
Reusable lenses:
val f1 = lens(person)(_.address.street.name)
val p1: Person = f1(_.toLowerCase)
val p2: Person = f1(_.toUpperCase)
Lens factories: The above lens only updates a particular person. You can make even more generic lenses that can update any Person
:
val f = lens(_: Person)(_.address.street.name)
val p3: Person = f(p1)(_.toUpperCase)
val p4: Person = f(p2)(_.toLowerCase)
Lens composition:
val lens1: Person ~~> Address = lens(_: Person)(_.address)
val lens2: Address ~~> String = lens(_: Address)(_.street.name)
val lens3: Person ~~> String = lens1 andThenLens lens2 // or lens2 composeLens lens1
val p5: Person = lens3(person)(_.toLowerCase)
sbt: In your build.sbt
, add the following entries:
resolvers += Resolver.bintrayRepo("pathikrit", "maven")
libraryDependencies += "com.github.pathikrit" %% "sauron" % "1.1.0"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0-M5" cross CrossVersion.full)
This library is inspired by the clever work done by @adamw in his quicklens library.
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].