JKWA

JKWA

Author of Advanced Functional Programming with Elixir

Here’s why lawful lenses matter (even in Elixir):

Couldn’t fit optics in my book, but I can build them into Funx.

funx

Most Liked

LostKobrakai

LostKobrakai

I certainly feel like lenses could get a lot more attention on elixir, but does a blogpost in favor of them need to start with dunking on elixirs built in lenses? – especially given most of the critic stated could be mitigated by a single Enum.map?

garfield = %{
  name: "Garfield",
  weight: 20,
  owner: %{
    name: "Jon"
  }
}

invalid_owner_age_path = [:owner, :age] |> Enum.map(&Access.key!/1)

get_in(garfield, invalid_owner_age_path)
# ** (KeyError) key :age not found in: %{name: "Jon"}

put_in(garfield, invalid_owner_age_path, 40)
#** (KeyError) key :age not found in: %{name: "Jon"}

update_in(garfield, invalid_owner_age_path, fn curr -> curr + 1 end)
# ** (KeyError) key :age not found in: %{name: "Jon"}

There’s also reasons why elixir doesn’t allow you to access structs when using bare keys. Structs in elixir are not treated as “data containers”, but as user defined datatypes, which might want to be used as a scalar. If you want that user defined datatype to be accessible like other map data you can implement the Access behaviour, which can then take into account that structs internals. The reason for a behaviour over a protocol is iirc due to performance reasons, potentially even ones of the past, but I’m not sure. You can certainly see how Access wants to be a protocol in spirit. Sadly this means this is less likely to be implemented that e.g. an Enumerable especially for library code.

But it’s always possible to access struct keys without Access implemented by forcing map key access using Access.key or Access.key!, just like Map.get works on structs.

lud

lud

Access is not a protocol, you will not find defimpl Access. You should search for “def fetch”!

garrison

garrison

As someone unfamiliar with the surrounding theory, I found the framing in terms of Elixir’s stdlib to be pedagogically helpful. Of course the points you make about Access are also useful context and could be incorporated.

The difference between behavio(u)rs and protocols is that of extensibility, right? So by turning Access into a protocol you are essentially saying that “other people” can decide whether my struct can be accessed like a map. Not just the user themselves, but any third party that invokes defimpl in the user’s dependency tree.

Does that actually make semantic sense?

TBH this question may be too theoretical. What’s the canonical example of a struct that should implement Access?

Where Next?

Popular in Blog Posts Top

tmartin8080
Sharing some articles I’ve written recently while working in Elixir. Some are how-to guides and others for reference. Will update this ...
New
victorbjorklund
I’m showing you how you can customise the phx.new generator to give you a new Phoenix project the way YOU want it. In this post I show yo...
New
fredwu
Hi folks, I wrote a blog post the other day on how I built my MVP in 3 months whilst having a day job, using Elixir/Phoenix/LiveView. Th...
New
RudManusachi
Hi there! Recently I was playing around with extracting and updating data in the DB and for fun challenged myself to try to implement a ...
New
AstonJ
Update: How to use the blogs section You can post in one of the Official Blog Posts threads (like this one), or, via Devtalk and a new t...
New
New
wmnnd
Hey there, I’ve started a little blog series about building and deploying Elixir applications. Now I would like to share with you the fi...
New
adolfont
Hi, You all probably know that José Valim is Brazilian and that his native language is Portuguese. I, Herminio Torres, Matheus Pesanha a...
New
AstonJ
Update: How to use the blogs section You can post in one of the Official Blog Posts threads (like this one), or, via Devtalk and a new t...
New
shanesveller
Introduction and first content post in my new series about running Elixir Phoenix apps on Kubernetes are live! I’m describing an opiniona...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement