zorn
Trying to understand when to prefer Kernel.length/1 vs Enum.count/1
Is Kernel.length/1 intended only for guards? I feel like I see people using length(items) in non-guard scenarios. Should I prefer it over Enum.count/1 in any general case?
Marked As Solved
zorn
Feel like I found my answer after I posted this.
Enum.count/1 delegates to length/1
https://github.com/elixir-lang/elixir/blob/v1.11.2/lib/elixir/lib/enum.ex#L598-L600
Also Liked
bottlenecked
Also by using length/1 the code is much more explicit - in my mind at least. Not having to wonder what the type of a variable is when reading a piece of code (given for example that Enum.count/1 supports all sorts of constructs) helps me read through a piece of code faster.
Last Post!
bottlenecked
Also by using length/1 the code is much more explicit - in my mind at least. Not having to wonder what the type of a variable is when reading a piece of code (given for example that Enum.count/1 supports all sorts of constructs) helps me read through a piece of code faster.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









