JKWA

JKWA

Author of Advanced Functional Programming with Elixir

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

funx

Showing Posts 1 to 9

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.

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?

krasenyp

krasenyp

You are correct about protocols. In my experience they’re criminally underused in Elixir and most people have fixation on behaviours for polymorphism but protocols are where it’s at.

JKWA

JKWA OP

Author of Advanced Functional Programming with Elixir

Elixir’s approach is pragmatic and works well. But when the same path behaves differently depending on the operation (nil vs. create vs. crash), it’s easy to miss encoding an invariant. Lenses just package that consistency into a reusable abstraction.

JKWA

JKWA OP

Author of Advanced Functional Programming with Elixir

Hmm… You can.. but should? That’s a tough one. I searched Ash and Phoenix for “defimpl Access” and didn’t see any.

JKWA

JKWA OP

Author of Advanced Functional Programming with Elixir

This Lens does not convert a struct into a map, allowing it to continue to behave as a scalar while exposing a narrowly defined, explicit access path.

lud

lud

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

krasenyp

krasenyp

I finally had some time to go through the article. Knowing lenses from Haskell and being disappointed by the lack of interest in functional programming in the community, it’s a breath of fresh air. Nice introduction and examples. Good job!

JKWA

JKWA OP

Author of Advanced Functional Programming with Elixir

Thanks!

— All posts loaded —

Where Next? Top

Trending in Blog Posts Top

rhcarvalho
At the heart of every Phoenix application is the often “invisible” HTTP server layer. For over a decade Cowboy has served the community ...
New
pckrishnadas88
Hey everyone! :waving_hand: I’ve published Part 7 of the Building Distributed Systems in Elixir series, where we build core distributed ...
New
mudasobwa
So, instead of wasting my afternoon arguing with anonymous handles on X, I turned to my trusty, soulless assistant and said: “Listen, ple...
New
abreujp
New article: Elixir Project Structure — From mix new to a Growing Codebase I’ve published a new article in my Elixir learning series on d...
New
zorn
An educational side project in Elixir, Phoenix, and Tauri. I share what I learned while wiring Automerge into the BEAM, including how I s...
New
jola
The way Phoenix is set up adding a CDN sub-domain for serving static assets, without worrying about the main dynamic content, is incredib...
New
jola
Wrote about how to safely run a globally unique process in an Elixir cluster, and a scary story from the past! Learn about :global for r...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews