Awesome Open Source
Awesome Open Source
  • Focus [[][]] [[]]

#+ATTR_HTML: :style margin-left: auto; margin-right: auto; [[img/focus_lens_prism.png]]

Lightweight, pure Elixir functional optics[fn:1].

#+BEGIN_QUOTE A lens is a value that composes a getter and a setter function to produce a bidirectional view into a data structure. This definition is intentionally broad—lenses are a very general concept, and they can be applied to almost any kind of value that encapsulates data. -- [[][Racket 'lens' documentation]] #+END_QUOTE

** Usage

To construct a lens:

#+BEGIN_SRC elixir

A lens for the key :name


A lens for the key "name"


A lens for the second item in a tuple:

Lens.make_lens(1) #+END_SRC

Each lens provides both a getter and a setter for the accessor it was created for.

Lenses can be used to access and/or modify structured data:

#+BEGIN_SRC elixir

Extract a value from a simple map:

person = %{name: "Homer"} nameLens = Lens.make_lens(:name)

Focus.view(nameLens, person)


Focus.set(nameLens, person, "Bart")

%{name: "Bart"}

Focus.over(nameLens, person, &String.upcase/1)

%{name: "HOMER"}


Their real utility comes in operating on nested data. Lenses can be created by composing other lenses in order to traverse a data structure:

#+BEGIN_SRC elixir person = %{ name: "Homer", address: %{ locale: %{ number: 742, street: "Evergreen Terrace", city: "Springfield", }, state: "???" } }

To access the street, we can compose the lenses that lead there from the top level.

Lenses can be composed with Focus.compose/2, or the infix (~>) operator.

address = Lens.make_lens(:address) locale = Lens.make_lens(:locale) street = Lens.make_lens(:street)

address ~> locale ~> street |> Focus.view(person)

"Evergreen Terrace"

address ~> locale ~> street |> Focus.set(person, "Fake Street")

person = %{

name: "Homer",

address: %{

locale: %{

number: 742,

street: "Fake Street",

city: "Springfield",


state: "???"




** Macros *** Optic creation

  • =deflenses= :: A wrapper around =defstruct= that additionally defines lenses for the struct's keys inside the module. #+BEGIN_SRC elixir defmodule User do import Lens deflenses name: nil, age: nil

      # deflenses defines %User{}, User.name_lens/0, and User.age_lens/0


** Functions *** Optic creation

  • =Lens.make_lens/1=
  • =Lens.make_lenses/1=
  • =Lens.idx/1= *** Pre-made optics
  • =Prism.ok/0=
  • =Prism.error/0=

*** Optic application

  • =Focus.view/2=
  • =Focus.over/3=
  • =Focus.set/3=
  • =Focus.view_list/2=
  • =Focus.has/2=
  • =Focus.hasnt/2=
  • =Focus.fix_view/2=
  • =Focus.fix_over/3=
  • =Focus.fix_set/3=

*** Optic composition

  • =Focus.compose/2, (~>)=
  • =Focus.alongside/2=

** Installation

  1. Add =focus= to your list of dependencies in =mix.exs=:
#+BEGIN_SRC elixir
  def deps do
    [{:focus, "~> 0.3.5"}]

** References

  • [[][A Little Lens Starter Tutorial]]
  • [[][ekmett/lens package FAQ]]
  • [[][Control.Lens Tutorial]]
  • Footnotes

[fn:1] This library currently combines Lenses and Prisms with Traversals in its implementation. Until v1.0.0, the API is subject to large and frequent change.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
elixir (1,181
lenses (23

Find Open Source By Browsing 7,000 Topics Across 59 Categories