tyro

tyro

Is there a way to pipe the cons (list prepend) operator?

You can prepend to a list with:

[item | list]

But is it possible to pipe this operation? I can’t see any way to do this in List/Kernel, and there seems to be very little info about the cons operator in general. I would like to be able to do something like:

list
|> List.prepend(item)

Of course, I could use a custom/anonymous function but is there an inbuilt way to do this?

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

Elixir, Erland and Prolog use structural pattern matching, while Haskell uses staticly typed pattern matching.

These are similar, but not the same. For instance, in Haskell you cannot re-use the same name twice to say that two things are equal, you will have to use a guard.

I think this is at least a partial reason.

tyro

tyro

Thanks. Perhaps I should be clearer that I’m not looking for alternatives (I’m aware of insert_at), I just want to know if this exact feature exists. Cons is one of the most common operations in Elixir, and piping is similarly ubiquitous, so it would seem odd if you can’t use both together! :stuck_out_tongue:

peerreynders

peerreynders

As far as I can tell:

  • Erlang doesn’t actually have a cons “operator” but syntax for a cons cell i.e. [ term1 | term2 ] - and it actually has two distinct uses, to construct a cons cell and to match a cons cell. So it makes sense that it is a language feature rather than an operator backed by a function.
  • Elixir and it’s macro support build on Erlang but the primary objective of the Pipe macro is to take the result of the preceding expression (LHS) and thread it into the first parameter of the next function (RHS) (Clojure actually refers to something like this as the thread first macro rather than a “Pipe”).

So the two simply don’t mix because Erlang’s cons cell predated Elixir and Elixir’s Pipe was designed to work with functions.

Always inquire whether there is an idiomatic way of doing things as commonality makes everybody’s life easier but also realize that sometimes the DIY-as-required approach is often accepted/preferred in order to keep clutter out of the language/platform.

peerreynders

peerreynders

Erlang Master Class 1: Video 8 - Discussion:

FRANCESCO CESARINI: One question I got once is, we’re learning Haskell and you’re giving us a lecture about how Erlang’s being used in the real world. Why are they actually teaching us Haskell, and not Erlang? And a natural answer there was, you’re here to learn how to learn.

I’m being left with the impression that in Haskell functional programming is an end in itself, while in Erlang and Elixir functional programming is merely a means to an end - so Erlang/Elixir seem more practically minded.

Also Erlang (for optimization reasons AFAIK) allows the formation of improper lists with the cons cell.

Qqwy

Qqwy

TypeCheck Core Team

I actually think that improper lists are allowed because the language is dynamically typed. Because consing is so common, why then add an extra check (that has to happen at runtime as it is dynamically typed) to see if the right hand side is a list?

If I remember correctly, most lisps (that are dynamically typed, just like elixir/erlang) allow improper lists because it is not worth this extra check.

You are totally right that Erlang was a language created for a practical purpose where being functional was a ‘side effect’, while Haskell was first a research language, which focused on practicality in the second place.

Where Next?

Popular in Questions Top

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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
aesmail
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement