Skip to content

Commit

Permalink
Add chunks iter method
Browse files Browse the repository at this point in the history
  • Loading branch information
bamless committed Dec 13, 2023
1 parent 1aab0bc commit 83c8027
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Binary file modified src/lib/core/iter.jsc
Binary file not shown.
21 changes: 21 additions & 0 deletions src/lib/core/iter.jsr
Expand Up @@ -109,6 +109,10 @@ class Iterable
return interleave(this, sep)
end

fun chunks(n)
return chunks(this, n)
end

fun repeat(n=null)
return repeat(this).take(n).flatten() if n else repeat(this).flatten()
end
Expand Down Expand Up @@ -571,6 +575,23 @@ fun interleave(iterable, sep)
end
end

fun chunks(iterable, size)
assert(size > 0, "size must be > 0", InvalidArgException)
var count, chunk = 0, []
for var e in iterable
chunk.add(e)
count += 1
if count == size
yield (...chunk,)
chunk.clear()
count = 0
end
end
if count != 0
yield (...chunk,)
end
end

fun sorted(iterable, comparator=null)
var lst = [...iterable]
lst.sort(comparator)
Expand Down

0 comments on commit 83c8027

Please sign in to comment.