aoiroaoino / nanolens   0.1.2

MIT License GitHub

Nanolens is tiny and experimental lens library for Scala 3

Scala versions: 3.x

nanolens

CI Maven Central

Nanolens is tiny and experimental lens library for Scala 3

type Lens[S, A] = [F[_]] => Functor[F] ?=> (A => F[A]) => S => F[S]

type Prism[S, T, A, B] = [P[_, _], F[_]] => (Choice[P], Applicative[F]) ?=>
  P[A, F[B]] => P[S, F[T]]

Installation

libraryDependencies += "dev.aoiroaoino" %% "nanolens" % "[version]"

Example

import dev.aoiroaoino.nanolens._

case class User(id: Int, name: String, address: Address)
case class Address(street: String)

def address: Lens[User, Address] =
  Lens[User, Address](_.address)(u => a => u.copy(address = a))

def street: Lens[Address, String] =
  Lens[Address, String](_.street)(a => s => a.copy(street = s))
  
// ===
  
val user = User(42, "John", Address("street"))

user $ (address ~ street).modify(_.toUpperCase)
// => User(42,John,Address(STREET))

user $ (address ~ street).get
// => street