• Stars
    star
    168
  • Rank 221,850 (Top 5 %)
  • Language
    Scala
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Yet another Scala lens macro

Sauron Circle CI Download

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.

More Repositories

1

better-files

Simple, safe and intuitive Scala I/O
Scala
1,462
star
2

scalgos

algorithms in scala
Scala
435
star
3

metarest

Scala macros to generate RESTful Models
Scala
195
star
4

mac-setup-script

script to setup my mac
Shell
158
star
5

newswall

Rotate and display latest newspapers on e-ink display
JavaScript
35
star
6

JFaceRecog

facial recognition in Java
Java
30
star
7

vocowl

A smart flashcard app
CoffeeScript
21
star
8

node-thunder-driver

Nodejs driver for Dream Cheeky Thunder Missile Launcher
JavaScript
14
star
9

chatgpt-cli

Simple CLI for ChatGPT using node.js
JavaScript
13
star
10

JSpeech2Text

A simple speech to text app in Java
Java
6
star
11

coffee-pearls

collection of small beautiful programs written in coffeescript
CoffeeScript
5
star
12

Quora-Challenges

My solution in Java to the Quora Datacenter cooling challange problem
Java
4
star
13

Monty

a simple no-limit texas hold'em bot using monte carlo simulations
Scala
3
star
14

stamp

Scala library to format dates and times based on human-friendly examples
Scala
3
star
15

garfield

Scala
3
star
16

my-vestaboard

JavaScript
2
star
17

full-stack-scala-web-app

Working code for the "Full Stack Scala Web App" tech talk
Scala
2
star
18

node-thunder-webui

a web interface for dream cheeky usb thunder missile launcher
JavaScript
2
star
19

chrome_ai

Random collection of my chrome extensions
JavaScript
2
star
20

BabelFish

Invoke other languages from Scala on the JVM
Scala
1
star
21

Kotha

A simple Java framework to write strongly typed remote APIs
Java
1
star
22

visionect-deploy

Code to Deploy Visionect Software
Dockerfile
1
star
23

bond_ladder_calculator

Python
1
star