Just throwing out some other names to see if any stick: Enum.count_above?/2 Enum.count_below?/2 Enum.count_greater?/2 Enum.count_lesser?/2
To put it inline with some of the other compare methods in elixir, like DateTime.compare/2, maybe an API like: Enum.compare_count([], 1) #=> :lt Enum.compare_count([1], 1) #=> :eq Enum.compare_count([1, 2], 1) #=> :gt Allen Madsen http://www.allenmadsen.com On Thu, Dec 3, 2020 at 6:51 PM Zach Daniel <zachary.s.dan...@gmail.com> wrote: > Well, List.count doesn’t exist yet, but either way it sounds like not a > great idea :) I couldn’t find examples in other Lang’s, so maybe I’ll just > throw out some other names: > > Enum.at_least?/2 > > Enum.at_most?/2 > > Enum.has_count?/2 > > On Thu, Dec 3, 2020 at 5:14 PM Michał Muskała <mic...@muskala.eu> wrote: > >> Unfortunately this can’t be done automatically since it has subtle >> semantic differences. In particular Enum.count/1 (or length/1) not only >> traverses the list to count its size, but also verifies it’s a proper list >> raising an exception for improper lists. The difference could be seen for >> value like: >> >> >> >> [1, 2, 3 | :invalid] >> >> >> >> Calling length/1 or Enum.count/1 on this raises. If compiler did the >> optimisation you propose, for something like length(list) > 0, it wouldn’t >> fully traverse the list and wouldn’t raise. Thus such an optimisation is >> not possible in the general case. >> >> >> >> *From: *elixir-lang-core@googlegroups.com < >> elixir-lang-core@googlegroups.com> >> *Date: *Thursday, 3 December 2020 at 22:04 >> *To: *elixir-lang-core@googlegroups.com < >> elixir-lang-core@googlegroups.com> >> *Subject: *Re: [elixir-core:9802] Proposal `Enum.more_than?/2` or >> `List.more_than?/2` >> >> This probably off the table/unreasonable, but it also seems like >> something that could be statically solved and people would never need to >> know as it is just an optimization. E.g Enum.count(list) > n could >> optimized by the compiler? Probably wouldn’t be good for all Enums, since >> counting would be expected to enumerate them, so maybe only something like >> List.count 🤷♂️ >> >> >> >> On Thu, Dec 3, 2020 at 1:42 PM Zach Daniel <zachary.s.dan...@gmail.com> >> wrote: >> >> Another benefit to the options list would be supporting it for count with >> a predicate, e.g Enum.count(enum, &some_predicate/1, max: 4) >> >> >> >> On Thu, Dec 3, 2020 at 1:35 PM Zach Daniel <zachary.s.dan...@gmail.com> >> wrote: >> >> Nothing is jumping out at me from elsewhere yet, but another option might >> be accepting options in `Enum.count`, like `Enum.count(list, max: 4)`. >> I’ll keep searching though. >> >> >> >> On Thu, Dec 3, 2020 at 1:31 PM Zach Daniel <zachary.s.dan...@gmail.com> >> wrote: >> >> I agree on the name feeling subpar :) I’ll take a look and see if I can >> find other examples. >> >> >> >> On Thu, Dec 3, 2020 at 12:21 PM José Valim <jose.va...@dashbit.co> wrote: >> >> Thanks Zach! I like this idea but the proposed name, for some reason, >> doesn't sit right with me. Is there any prior art from other langs we could >> look at? >> >> >> >> On Thu, Dec 3, 2020 at 6:15 PM Zachary Daniel <zachary.s.dan...@gmail.com> >> wrote: >> >> Counting a list, especially a large one, to know if there are "more than >> x" or "less than x" items is inefficient. >> >> >> >> Right now I often see things like `if Enum.count(list) > 4 ...`, mostly >> because writing a recursive `more_than?` check is tedious, or doing >> something like `Enum.empty?(Enum.drop(list, 4))` is not very expressive. >> >> >> >> I think it would be nice to have an `Enum.more_than?` that does that work >> for you. It could also be `List.more_than?/2` if we don't want it in Enum. >> Any thoughts? >> >> -- >> 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-core+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/263d7c39-a32b-4294-93d8-40f248c9b3c8n%40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-core/263d7c39-a32b-4294-93d8-40f248c9b3c8n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> -- >> 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-core+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JX4NE1yWH1G5L_DjF18v8zejF0%2BSkb_oz%3DPiUHM8Mz1w%40mail.gmail.com >> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JX4NE1yWH1G5L_DjF18v8zejF0%2BSkb_oz%3DPiUHM8Mz1w%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> -- >> 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-core+unsubscr...@googlegroups.com. >> >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0BBGCrgbZamFs%2BeqLUis6mFQgvUHkKK1htSN5rDDWwMRQ%40mail.gmail.com >> <https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0BBGCrgbZamFs%2BeqLUis6mFQgvUHkKK1htSN5rDDWwMRQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> -- >> 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-core+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/DB7PR07MB3899C92933992464F17898E1FAF20%40DB7PR07MB3899.eurprd07.prod.outlook.com >> <https://groups.google.com/d/msgid/elixir-lang-core/DB7PR07MB3899C92933992464F17898E1FAF20%40DB7PR07MB3899.eurprd07.prod.outlook.com?utm_medium=email&utm_source=footer> >> . >> > -- > 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-core+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0BO2QESHcaL7-svOoAGqvr6hJi%3D8AHFqi-qNZdoFEMMwA%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0BO2QESHcaL7-svOoAGqvr6hJi%3D8AHFqi-qNZdoFEMMwA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3CuGqUgFwHsUUOQ9RhKuaVMOnSPQfP9tgjvFjWqnzQFUnA%40mail.gmail.com.