overallduka

overallduka

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.

Showing Posts 1 to 4

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 #1
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:

overallduka

overallduka OP

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

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

jeffyzooom
Hi all, I’ve been working on RustyCSV, a CSV parser and encoder for Elixir backed by Rust NIFs. Version 0.4.0 is now available. The goa...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews