[Proposal] Create Kernel.fst and Kernel.snd functions

71 views
Skip to first unread message

Vinícius Müller

unread,
May 21, 2023, 9:06:12 PM5/21/23
to elixir-lang-core
Currently it is necessary to do `|> &elem(&1, 1)` or `|> &elem(&1, 2)` when trying to get a specific element of a tuple in a pipeline or normally.

F#[0], Haskell[1], Ocaml[2] and other functional languages have the `fst` and `snd` functions, which simply extract the first and second elements of a tuple, respectively.

I think it would make some code samples simpler, since it reads much better, I guess it's the same idea as using `|> hd()` rather than doing `|> fn [h | _] -> h end`.

Does this makes sense for Elixir? If so, I'm willing to implement it.

Austin Ziegler

unread,
May 21, 2023, 9:25:03 PM5/21/23
to elixir-l...@googlegroups.com
Personally, I think that the names are excessively short and confusing. `first_elem` and `second_elem` would be more meaningful.

-a

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8c965a60-d8a5-4384-aeb4-182a86c085bbn%40googlegroups.com.


--

Felipe Stival

unread,
May 22, 2023, 12:00:27 AM5/22/23
to elixir-lang-core
In general I am a +1, but I'd rather have these utility functions under `Tuple`, as `Tuple.first/1` and `Tuple.second/1`. 

Kurtis Rainbolt-Greene

unread,
May 22, 2023, 1:00:54 AM5/22/23
to elixir-l...@googlegroups.com
I wanted this set of functions originally, though I never used any of those languages, so I suspect it's natural, I even implemented them in almost every project. That said now the |> &f(&1, a) syntax is so natural to me that I've since removed those functions. I don't think you can go *wrong* by adding it to the core lib, but you couldn't make me decide on a name with my job on the line.



--
Kurtis Rainbolt-Greene,
Software Developer & Founder of Difference Engineers

José Valim

unread,
May 22, 2023, 1:52:09 AM5/22/23
to elixir-l...@googlegroups.com
I am not sure we want to encourage those functions. The mentioned languages have a static type system, which means you know the shape upfront.

In Elixir, we don’t, so we want to prefer pattern matching as much as possible, as it also helps assert on tuple size and help catch mistakes.

Reply all
Reply to author
Forward
0 new messages