array_zip_with 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4d851dfeae378a6228fe49937457e3b55b0ca20
4
+ data.tar.gz: a58e75fc90fe48dd7093a5580256729d30a15a05
5
+ SHA512:
6
+ metadata.gz: 954569ecd4581e0657b9ee2d11a6d02e7e2d677c2cf7bdf05bc2f238d70370e38d2ee84fbcdffb80f71e081d4951b8e75fe8ebfe88fe8833b0ad9b503af8926a
7
+ data.tar.gz: c00fd8bdf7316a0336c444fa73c3d9bbb27739270ff3049b3092bb49e19fb2cb35866a07a6463cd4bdc0bab5afc61588f9ce3e1cdc4afaf08bda8f5ac8b844a9
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Vasanth Balakrishnan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'array_zip_with'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require 'array_zip_with/version'
2
+ require 'array_zip_with/core_ext/array.rb'
@@ -0,0 +1,37 @@
1
+ # @author Vasanth Balakrishnan <vasantheb@gmail.com>
2
+ class Array
3
+ # @overload zip_with(*args, &block)
4
+ # Zips the input arrays with `self` and invokes block with the elements of
5
+ # the zipped array.
6
+ # @param [Array] args One or more arrays
7
+ # @example With one input array
8
+ # a = [1, 2, 3]
9
+ # a.zip_with([3, 2, 1]) { |x, y| x + y } # => [4, 4, 4]
10
+ # @example With multiple input arrays
11
+ # a = [1, 2, 3]
12
+ # a.zip_with([4, 5, 6], [7, 8, 9]) do |x, y, z|
13
+ # y - x * z
14
+ # end # => [21, 24, 27]
15
+ # @return [Array]
16
+ # @overload zip_with(*args, operator)
17
+ # Zips the input arrays with `self` and reduces the zipped array with
18
+ # the +:operator+
19
+ # @param [Array] args One or more arrays
20
+ # @param [Symbol] operator Operator to reduce the zipped array with
21
+ # @example With one input array
22
+ # a = [1, 2, 3]
23
+ # a.zip_with([3, 2, 1], :+) { |x, y| x + y } # => [4, 4, 4]
24
+ # @example With multiple input arrays
25
+ # a = [1, 2, 3]
26
+ # a.zip_with([4, 5, 6], [7, 8, 9], :+) # => [12, 15, 18]
27
+ # @return [Array]
28
+ # @raise [ArgumentError] If there is no symbol or a block as the last
29
+ # parameter
30
+ def zip_with(*args)
31
+ return zip(*args).map(&Proc.new) if block_given?
32
+ operator = args.pop
33
+ raise ArgumentError, "A block or symbol \
34
+ must be provided" unless operator.is_a?(Symbol)
35
+ zip(*args).map { |elems| elems.reduce(operator) }
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module ArrayZipWith
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ describe '#zipWith' do
5
+ subject { [1, 2, 3] }
6
+ context 'when no block or symbol given' do
7
+ it 'raises ArgumentError' do
8
+ expect { subject.zip_with([4, 5, 6]) }.to raise_error(ArgumentError)
9
+ end
10
+ end
11
+ context 'when a block is given' do
12
+ it 'yields the block with zipped array' do
13
+ expect(subject.zip_with([4, 5, 6]) { |x, y| x + y }).to eq([5, 7, 9])
14
+ end
15
+ end
16
+ context 'when a symbol is given' do
17
+ it 'should reduce the array with the given operator' do
18
+ expect(subject.zip_with([4, 5, 6], :+)).to eq([5, 7, 9])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe ArrayZipWith do
4
+ it 'has a version number' do
5
+ expect(ArrayZipWith::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'array_zip_with'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_zip_with
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vasanth Balakrishnan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.39.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.39.0
83
+ description: |2
84
+ Array#zip_with inspired by Haskell's zipWith function.It takes either
85
+ yields a zipped array to a block or reduces the zipped array with the
86
+ given operator.
87
+ email:
88
+ - vasantheb@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - LICENSE.txt
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - lib/array_zip_with.rb
98
+ - lib/array_zip_with/core_ext/array.rb
99
+ - lib/array_zip_with/version.rb
100
+ - spec/lib/array_zip_with/core_ext/array_spec.rb
101
+ - spec/lib/array_zip_with_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/hallucinations/array_zip_with
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Array#zip_with inspired by Haskell's zipWith.
127
+ test_files: []
128
+ has_rdoc: