overallduka

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

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
Post #2
swelham

swelham

If you want to use the pipe with arythmetic operators you can do this for example 1 |> Kernel.>(2).

EDIT: sorry for the duplicate answer, @Qqwy must be faster at typing than me! :slight_smile:

NobbZ

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
overallduka

overallduka

Oh, just worked fine, thank you guys for the quick replies. =)

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics 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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ycv005
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
siddhant3030
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

We're in Beta

About us Mission Statement