Giter VIP home page Giter VIP logo

focus's Introduction

Focus

https://img.shields.io/hexpm/v/focus.svg https://github.com/smpoulsen/focus/actions/workflows/test.yml/badge.svg

img/focus_lens_prism.png

Lightweight, pure Elixir 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
Lens.make_lens(:name)

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

# A lens for the second item in a tuple:
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 = Lens.make_lens(:name)

Focus.view(nameLens, person)
# "Homer"

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:

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.
defmodule User do
  import Lens
  deflenses name: nil, age: nil

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

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:
    def deps do
      [{:focus, "~> 0.3.5"}]
    end
        

References

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.

focus's People

Contributors

alexdeleon avatar pmeinhardt avatar smpoulsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

focus's Issues

Lens.safe_view crashes after 3rd composed lens when there is index

import Focus
data = %{a: []}
a = Lens.make_lens(:a)
b = Lens.make_lens(:b)
c = Lens.make_lens(:c)
d = Lens.make_lens(:d)
i0 = Lens.idx(0)

This works fine:
a ~> b ~> c ~> d |> Lens.safe_view(data)
{:error, {:lens, :bad_path}}

This crashes
a ~> i0 ~> b ~> c |> Lens.safe_view(data)
** (ArithmeticError) bad argument in arithmetic expression
Lensable.Tuple.getter/2
lib/lens/lens.ex:227: Lens.safe_view/2

Readme Link broken

Hi !
just noticed that the second link in the References section (Lenses in Functional Programming) is broken

Consider making structure the first argument to get/set/etc... functions

The original intention with making the lens the first argument was to support composing a long chain of lenses and piping into Focus.view/2, Focus.set/3, Focus.over/3 etc….

There is obvious value in making the data structure the first argument, as focus could then slot directly into pipeline operations.

safe_view throwing an error when index doesn't exists

la = Lens.make_lens("a")
lb = Lens.make_lens("b")
i0 = Lens.idx(0)

This works
la ~> i0 ~> lb |> Lens.safe_view(%{"a" => [%{"b" => 3}]})

This crashes
la ~> i0 ~> lb |> Lens.safe_view(%{"a" => []})

** (FunctionClauseError) no function clause matching in Keyword.get/3
(elixir) lib/keyword.ex:150: Keyword.get([], 0, nil)
lib/focus.ex:147: anonymous fn/3 in Focus.compose/2

Shouldn't "safe_view" be safe and not crash when something is missing?

Is it possible to target multiple / all items in a list in a generic way?

Hey, thanks for focus, this is quite superb way to deal with large payloads!

I have one question and I could not answer it after looking through Readme / codebase / tests.

Given the following datastructure:

payload = %{
items: [
  %{id: 1, price: 12.99}, 
  %{id: 2, price: 34.60}, 
  %{id: 3, price: 99.28}, 
]
}

how can one

  • convert all ids to strings?
  • collect all prices?
  • round up prices?
  • etc... ?

Does focus support targeting all items in a list in a generic way, means without explicit index option?

Thanks and have a nice day!
Roman

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.