Giter Club home page Giter Club logo

focus's Introduction

Focus

https://circleci.com/gh/tpoulsen/focus.svg?style=svg https://img.shields.io/hexpm/v/focus.svg

img/focus_lens_prism.png

An experiment with functional optics[fn:1].

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

Usage

To construct a lens:

# A lens for the key :name
Focus.Lens.make_lens(:name)

# A lens for the key "name"
Focus.Lens.make_lens("name")

# A lens for the second item in a list/tuple:
Focus.Lens.make_lens(1)

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:

# Extract a value from a simple map:

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

Focus.Lens.view!(nameLens, person) == "Homer"
# true

Focus.Lens.set(nameLens, person, "Bart")
# %{name: "Bart"}

Focus.Lens.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:

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.Lens.compose/2, or the infix (~>) operator.

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

address
~> locale
~> street
|> Focus.Lens.view!(person)
# "Evergreen Terrace"

address
~> locale
~> street
|> Focus.Lens.set(person, "Fake Street")
# person = %{
#   name: "Homer",
#   address: %{
#     locale: %{
#       number: 742,
#       street: "Fake Street",
#       city: "Springfield",
#     },
#     state: "???"
#   }
# }

Functions

Lens creation

  • Focus.Lens.make_lens/1

Lens application

  • Focus.Lens.view!/2
  • Focus.Lens.view/2
  • Focus.Lens.set/3
  • Focus.Lens.over/3
  • Focus.Lens.apply_list/2

Lens composition

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

Installation

  1. Add focus to your list of dependencies in mix.exs:
    def deps do
      [{:focus, "~> 0.1.0"}]
    end
        
  2. Ensure focus is started before your application:
    def application do
      [applications: [:focus]]
    end
        

References

Footnotes

[fn:1] This library currently combines Lenses, Prisms, and Traversals in its implementation of Lens.

focus's People

Contributors

smpoulsen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.