Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Add for_each
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanvanzuriak committed Mar 14, 2018
1 parent 778bc96 commit b2a39c7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -95,6 +95,11 @@ Zap.chunk(['a', 'b', 'c', 4], 2) # [['a', 'b'], ['c', 4]]
```crystal
Zap.flattenDeep([1, [2, [3, [4]], 5]]) # [1, 2, 3, 4, 5]
```
* `for_each(array, func)` - Iterates over elements of collection and invokes function for each element

```crystal
Zap.for_each([1, 2, 3], ->(element: Int32, index: Int32) { puts element }) # Prints 1, 2, 3
```

* `head(array)` - Gets the first element of array

Expand Down
7 changes: 7 additions & 0 deletions spec/zap_spec.cr
Expand Up @@ -49,6 +49,13 @@ describe Zap do
Zap.filter([1, 2, 3], ->(element : Int32, index : Int32) { element > 3 }).should eq([] of Int32)
end

it "@for_each" do
test = 0

Zap.for_each([1, 2, 3], ->(element : Int32, index : Int32) { test += 1 })
test.should eq(3)
end

it "@flatten" do
# __⚠️ Broken__
# Zap.flatten([1, 2]).should eq([1, 2])
Expand Down
3 changes: 2 additions & 1 deletion src/zap.cr
Expand Up @@ -310,7 +310,8 @@ module Zap
def flat_map_depth
end

def for_each
def for_each(array, func)
array.each_with_index { |value, index| func.call(value, index) }
end

def for_each_right
Expand Down

0 comments on commit b2a39c7

Please sign in to comment.