overallduka
Arithmetic Operators with Pipe Operator
Hello, i just have on doubt, is about code design, i have this case:
def searched_for?(user_id, term) do
search_length = from(se in Project.Search,
where: ilike(se.keywords, ^"%#{term}%") and se.user_id == ^user_id,
select: se.id)
|> Project.Repo.all
|> length
search_length > 0
end
As your guys can see i needed create the variable search_length to compare in the final result, have some way to put arythmetic operators in the pipe operator ? i tried |> >(0) and |> != 0 but without succeded.
Most Liked
Qqwy
TypeCheck Core Team
Yes, there is!
All of the common operators are actually simple functions in the Kernel namespace, and can be called like a normal function by prefixing them with their namespace:
3 |> Kernel.+(2).
12
swelham
NobbZ
Besides of the ways that were already shown to you, you can always write another function or use an anonymous one, even the anonymous syntax is a biz counterintuitive in pipes:
iex(1)> defmodule F, do: def greater(a, b), [do: a > b]
{:module, F,
<<70, 79, 82, 49, 0, 0, 4, 216, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 161,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:greater, 2}}
iex(2)> import F
F
iex(3)> 1 |> greater(2)
false
iex(4)> 1 |> (fn (a, b) -> a > b end).(2)
false
iex(5)> 1 |> (&(&1 > &2)).(2)
false
Using single arity anonymous functions is possible as well:
iex(6)> 1 |> (&(&1 > 0)).() # these pair of parens is important!
true
5
Popular in Questions
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I have a super simple question about elixir - how would I take a file like this
foo
bar
baz
and output a new file that enumerates th...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
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
Hello, how can I check the Phoenix version ?
Thanks !
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
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
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
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









