stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

`list.count` and `iterator.count`

Open apainintheneck opened this issue 1 month ago • 8 comments

This would function like Array#count does when it takes a block in Ruby.

irb(main):001:0> list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> list.count { |x| x.odd? }
=> 5

In Gleam, this can obviously be hand-coded in a few different ways but this seems like something that happens often enough that it'd be nice to have a standard library function for it.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|> list.count(int.is_odd)
fn count(list: List(a), predicate: fn(a) -> Bool) -> Int

apainintheneck avatar May 16 '24 07:05 apainintheneck