[elixir-core:11607] [Proposal] Safe versions for `hd` & `tl`

2023-12-12 Thread waiting-for-dev
Here's a revised version with corrected grammar and clarity: Kernel.hd/1 and Kernel.tl/1 raise an ArgumentError when given an empty list ([]). In my humble opinion, it would be beneficial to have safe versions of these functions, ideally following Elixir's naming convention: hd! and tl! for

Re: [elixir-core:11607] Safe versions for `hd/1` & `tl/1`

2023-12-12 Thread Wojtek Mach
I don’t think we should change semantics of hd/1 as it comes from Erlang and can be used in guards. Fortunately we have `List.first([])` and `Enum.drop([], 1)` as “safe" replacements. > On 12 Dec 2023, at 13:19, waiting-for-dev wrote: > > Kernel.hd/1 and Kernel.tl/1 raise an ArgumentError

[elixir-core:11607] Safe versions for `hd/1` & `tl/1`

2023-12-12 Thread waiting-for-dev
Kernel.hd/1 and Kernel.tl/1 raise an ArgumentError when given an empty list ([]). In my humble opinion, it would be beneficial to have safe versions of these functions, ideally following Elixir's naming convention: hd! and tl! for the raising variants, and hd and tl for the safe ones returning

Re: [elixir-core:11608] Safe versions for `hd/1` & `tl/1`

2023-12-12 Thread José Valim
In addition to what Wojtek said and this being a breaking change, you can pattern match on the empty list vs [head | tail] directly, without the overhead of calling a function to just pattern match on ok/error. -- You received this message because you are subscribed to the Google Groups