Gilou06

Gilou06

Hello,
I keep using a paradigme using a homemade function to make my life easier when piping.
I wonder if there is a better way to achieve the same result, maybe something already part of Elixir language.

Instead of writing a tailored maybe_dothat() function, I prefer a generic maybe_do() wrap function around a do_that() function.

Here is the may_be/1 wrapper.

  def maybe_do(pipe_value, condition, function)
      when is_boolean(condition) and is_function(function) do
    if condition do
      function.(pipe_value)
    else
      pipe_value
    end
  end

Let’s say I want to always call do_this/1 on a socket and do_that/1 but only if my_bool is true, else socket rest unchanged.
I write it this way:

  socket
    |> maybe_do(my_bool, &do_this/1)
    |> do_that()

Your insights are welcome.
Jean-yves :slight_smile:

Showing Posts 1 to 10

greven

greven

There is not such construct in Elixir that I know of, there is tap/2 to perform a side effect but the original value is the one being pipped, there is also then/2 but here is the function return in the second argument that is being pipped instead.

So I guess your solution is fine if you find yourself repeating that pattern a lot.

dimitarvp

dimitarvp

As the other poster said, tap and then cover a lot of needs, and also yeah if it’s more convenient for you to use your own function then please don’t feel bad about it! It’s very normal.

People use their own functions a lot when you need to go through a series of validations, for example. It’s expected to do it like that in many teams even.

cloudytoday

cloudytoday

You can do the same with case:

value
|> case do
  x when my_condition -> f.(x)
  x -> x

Alternatively you can write a macro that supports holes, lets say you call it p:

value
|> (p if my_condition, do: f.(_) else: _)

Doesn’t have to be exactly like this ofc, you can define it to be whatever you like the most. Just giving an example here. I actually have a bunch of these in my helpers libs. Edit: or, even better, you can also extend the pipe itself with this macro.

sodapopcan

sodapopcan

If you’re really talking about a socket and only talking about a couple of functions, I would use a live hook like on_mount or attach_hook along with an if.

Gilou06

Gilou06 OP

It’s about coding GUI behavior (Phoenix/Surface) based on user actions.

Gilou06

Gilou06 OP

Yes, it does the same, but I like the compactness of maybe_do(…). It reads nicely.

cloudytoday

cloudytoday

Yes agree, case is a bit lengthy.

dimitarvp

dimitarvp

An alternative implementation:

def maybe_do(value, true, func) when is_function(func, 1), do: func.(value)
def maybe_do(value, _, _), do: value

Gilou06

Gilou06 OP

That’s way better, thank you!
I still fear that my approach maybe a code smell.
I mean, I must not be the first one to need to chain (pipe) functions based on some conditions that are not directly part of the piped data.

This is typically true with GUI events. You want to reflect the actions in the socket (the piped data), but the event itself is not part of that socket.
An option would be to add the events to the piped data, piping {event,socket} rather than the plain socket.
Anyway, thank you again.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews