karlosmid

karlosmid

The |> operator appears in many languages, mostly in the functional world. F# has essentially the exact same operator, as does OCaml. Elixir has a slightly fancier version (which we considered but ultimately decided against for now)

Showing Posts 1 to 8

cmo

cmo

What is fancier about the Elixir one than the F#/OCaml one?

sbuttgereit

sbuttgereit

If I’m reading that page correctly, the right-hand side only allows for functions expecting a single argument whereas in Elixir the left-hand side becomes the value of the first argument on the right-hand side. My understanding comes from this line in the page linked:

The pipe operator, spelled |> , is deceptively simple. It takes the value on its left side and passes it as the single argument to a function (or in PHP’s case, callable ) on its right side

All of their examples seem to support this:

$string = 'something GoesHERE';

$newString = match ($format) {
    'snake_case' => $string
        |> splitString(...)
        |> fn($x) => implode('_', $x)
        |> strtolower(...),
    'lowerCamel' => $string
        |> splitString(...),
        |> fn($x) => array_map(ucfirst(...), $x)
        |> fn($x) => implode('', $x)
        |> lcfirst(...),
    // Other case options here.
};

There’s some syntax in that example I’m not familiar with (last time I did any serious PHP was in 2007), but my guess is that would be the difference.

derek-zhou

derek-zhou

Ocaml and F# have currying, so they don’t need to support more than 1-arity. Elixir has established a convention of treating the first argument as the “subject”, so piping into the first argument makes sense. In PHP’s case, there is neither currying, nor established convention for argument order. I think piping will be not be as elegant here.

mayel

mayel

Maybe they need something like we have in Arrows — arrows v0.2.1

# Standard first position pipe
2 |> Integer.to_string()
"2"

# Using ellipsis to place the piped value in a non-first position
3 |> String.pad_leading("2", ..., "0")
"002"
AstonJ

AstonJ

While on the topic of PHP there was a nice article about it just yesterday on Devtalk..

As much as I was never a fan of the syntax, it’s hard to deny that PHP sites can run well without issue for a decent period - I still manage some forums (running on PHP) that haven’t been updated in 15 years!

sodapopcan

sodapopcan

I still use it for small server-side interactions (though not for anything that has survived). It’s still the obvious choice for that that kind of thing, but lordy can I never get used to $.

jam

jam

It’d be nice if Elixir core had all the arrows functionality baked in :slightly_smiling_face:

sodapopcan

sodapopcan

This is a dead horse that has been thoroughly beaten several times over. It’s never going to happen, but there are several options if you really want it.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews