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

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; 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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
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
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

We're in Beta

About us Mission Statement