Gilou06

Gilou06

Conditional piping part of the language?

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:

Most Liked

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.

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

sodapopcan

sodapopcan

It’s very hard to tell without seeing your exact code, but if you’re feeling it’s a code smell it’s possible that you are trying to force a pipeline when it is not needed. Once you start threading tuples like {event, socket} through a pipeline where only one function cares about event, you’re hiding details. It makes it harder in the future to read the pipeline at a glance without looking at the implementation of each function. Of course if it’s a pattern that appears everywhere it’s not such a big deal, but generally pipelines are best when they operate on a single immutable data structure. YMMV, of course.

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.

derek-zhou

derek-zhou

In my opinion, over use of then is a code smell; your maybe_do function is not.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement