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

Commit

Permalink
Add reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanvanzuriak committed Mar 13, 2018
1 parent f061103 commit b15ec63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -147,6 +147,12 @@ Zap.chunk(['a', 'b', 'c', 4], 2) # [['a', 'b'], ['c', 4]]
array # [3, 2, 1]
```

* `reduce(array, func)` - Combines all elements in the collection by applying a binary operation, specified by a block, so as to reduce them to a single value

```crystal
Zap.reduce([1, 2, 3], -> (acc: Int32, element: Int32) {acc + element}) # 6
```

* `sorted_uniq(array)` - Sort array and returns a new array by removing duplicate values in self

```crystal
Expand Down
6 changes: 6 additions & 0 deletions spec/zap_spec.cr
Expand Up @@ -103,6 +103,12 @@ describe Zap do
)
end

it "@reduce Combines all elements in the collection by applying a binary operation, specified by a block, so as to reduce them to a single value" do
Zap.reduce([1, 2, 3], ->(acc : Int32, element : Int32) { acc + element }).should eq(
6
)
end

it "@reverse Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on" do
array = [1, 2, 3]

Expand Down
10 changes: 4 additions & 6 deletions src/zap.cr
Expand Up @@ -169,6 +169,10 @@ module Zap
def remove
end

def reduce(array, func)
array.reduce { |acc, i| func.call(acc, i) }
end

def reverse(array)
array.reverse!
end
Expand Down Expand Up @@ -285,9 +289,6 @@ module Zap
def every
end

def filter
end

def find
end

Expand Down Expand Up @@ -330,9 +331,6 @@ module Zap
def partition
end

def reduce
end

def reduce_right
end

Expand Down

0 comments on commit b15ec63

Please sign in to comment.