acrolink

acrolink

How to check for a condition inside a pipe chain?

Inside a pipe chain, how to execute one pipe line / exclude it based on say is_list(variable)? i.e. execute pipe line 5 only if variable is a list. Is that possible?

Marked As Solved

rossta

rossta

It might be possible to extract a function where you can pattern match and compose the query. Here’s what it could look like for the expires policy (actual code may depend on where your functions and usage are located):

# Policy module
def with_expiry(page, nil) do
  expires_params = "01-01-1970" |> Timex.parse!("%d-%m-%Y", :strftime) |> Ecto.DateTime.cast!
  page |> where([p, c], p.expires >= ^expires_params)
end
def with_expiry(page, expires_params) do
  page |> where([p, c], p.expires >= ^expires_params)
end

#...

page =
  Policy
    |> join(:left, [p], c in Client, p.client_id == c.id)
    |> where([p, c], c.user_id == ^conn.assigns.current_user.id)
    |> Policy.with_expiry(params["policy"]["expires"])
    # ...

Also Liked

acrolink

acrolink

@rossta, @vidalraphael, @kelvinst

Thank you very much.. I have learned new things here today.. I am happy that I have found my way back to this forum after some bad bi yearly banning experiences at holy stackoverflow (latest only because I asked a duplicate question)..

OvermindDL1

OvermindDL1

I’m often lazy and just put a case between things… >.>

value
|> something(42)
|> something_else()
|> case do
     page where limit_size == nil -> page
     page -> page |> limit(size)
   end
|> some_more_things()
...

Also you really need to use code tags instead of quotes for bodies of code, you do it like:
```elixir
Code goes here like 2+2
```
Makes:

Code goes here like 2+2
wolf4earth

wolf4earth

As a possible alternative to writing a full fledged maybe_* function, you can also consider to use a general purpose maybe_if/3 function. I’ve done that in the past when I needed to chain multiple optional steps.

initial_data
|> first_transformation()
|> maybe_if(should_apply_step2, &second_transformation/1)
|> maybe_if(should_apply_step3, &third_transformation_with_params(&1, my_param))
|> another_one()

where maybe_if is defined like this:

def maybe_if(data, true, action)
  when is_function(action, 1),
  do: apply(action, data)

def maybe_if(data, false, _action), do: data

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement