j8-functional 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module J8
4
+ class Predicate
5
+ include J8::Common
6
+
7
+ def self.equal?(target)
8
+ J8::Predicate.new(->(o) { o == target })
9
+ end
10
+
11
+ def test(o)
12
+ @callable.call(o)
13
+ end
14
+
15
+ def negate
16
+ J8::Predicate.new(->(o) { !test(o) })
17
+ end
18
+
19
+ def and(other = nil, &block)
20
+ callable = from_callable(other, block)
21
+
22
+ J8::Predicate.new(->(o) { test(o) && callable.test(o) })
23
+ end
24
+
25
+ def or(other = nil, &block)
26
+ callable = from_callable(other, block)
27
+
28
+ J8::Predicate.new(->(o) { test(o) || callable.test(o) })
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ module J8
4
+ class Stream # rubocop:disable Metrics/ClassLength
5
+ include J8::Common
6
+
7
+ attr_reader :enumerator
8
+
9
+ def initialize(enumerator)
10
+ @enumerator = enumerator
11
+ end
12
+
13
+ def self.concat(stream1, stream2)
14
+ J8::Stream.new(
15
+ Enumerator.new do |enumerator|
16
+ [stream1, stream2].each do |stream|
17
+ stream.enumerator.each { |o| enumerator << o }
18
+ end
19
+ end
20
+ )
21
+ end
22
+
23
+ def self.empty
24
+ [].j8_stream
25
+ end
26
+
27
+ def self.generate(supplier = nil, &block)
28
+ callable = J8::Common.from_callable_class(supplier, block, J8::Supplier)
29
+
30
+ J8::Stream.new(
31
+ Enumerator.new do |enumerator|
32
+ loop { enumerator << callable.get }
33
+ end
34
+ )
35
+ end
36
+
37
+ def self.iterate(seed, function = nil, &block)
38
+ callable = J8::Common.from_callable_class(function, block, J8::Function)
39
+
40
+ J8::Stream.new(
41
+ Enumerator.new do |enumerator|
42
+ loop { enumerator << callable.apply(seed) }
43
+ end
44
+ )
45
+ end
46
+
47
+ def self.of(*values)
48
+ values.j8_stream
49
+ end
50
+
51
+ def all_match?(predicate = nil, &block)
52
+ callable = from_callable_class(predicate, block, J8::Predicate)
53
+
54
+ # Handle Ruby's use of #all?
55
+ # [].all? { |n| n == true } => true
56
+ ran = false
57
+
58
+ ret = @enumerator.all? do |o|
59
+ ran = true
60
+ callable.test(o) == true
61
+ end
62
+
63
+ ran ? ret : false
64
+ end
65
+
66
+ def any_match?(predicate = nil, &block)
67
+ callable = from_callable_class(predicate, block, J8::Predicate)
68
+
69
+ @enumerator.any? { |o| callable.test(o) }
70
+ end
71
+
72
+ def collect
73
+ # figure it out
74
+ end
75
+
76
+ def count
77
+ @enumerator.count
78
+ end
79
+
80
+ def filter(predicate = nil, &block)
81
+ callable = from_callable_class(predicate, block, J8::Predicate)
82
+
83
+ @enumerator.select { |n| callable.test(n) }.j8_stream
84
+ end
85
+
86
+ def find_first
87
+ J8::Optional.new(@enumerator.first)
88
+ end
89
+ alias find_any find_first
90
+
91
+ def flat_map(function = nil, &block)
92
+ callable = from_callable_class(function, block, J8::Function)
93
+
94
+ @enumerator.map { |n| callable.apply(n) }.flatten.j8_stream
95
+ end
96
+
97
+ def each(consumer = nil, &block)
98
+ callable = from_callable_class(consumer, block, J8::Consumer)
99
+
100
+ @enumerator.each { |n| callable.accept(n) }
101
+ end
102
+
103
+ def limit(size)
104
+ @enumerator.first(size).j8_stream
105
+ end
106
+
107
+ def map(function = nil, &block)
108
+ callable = from_callable_class(function, block, J8::Function)
109
+
110
+ @enumerator.map { |object| callable.apply(object) }.j8_stream
111
+ end
112
+
113
+ def max(comparator = nil, &block)
114
+ callable = from_callable_class(comparator, block, J8::Comparator)
115
+
116
+ @enumerator.max { |a, b| callable.compare(a, b) }
117
+ end
118
+
119
+ def min(comparator = nil, &block)
120
+ callable = from_callable_class(comparator, block, J8::Comparator)
121
+
122
+ @enumerator.min { |a, b| callable.compare(a, b) }
123
+ end
124
+
125
+ def none_match?(predicate = nil, &block)
126
+ callable = from_callable_class(predicate, block, J8::Predicate)
127
+
128
+ # Handle Ruby's use of #all?
129
+ # [].all? { |n| n == true } => true
130
+ ran = false
131
+
132
+ ret = @enumerator.all? do |object|
133
+ ran = true
134
+ callable.test(object) != true
135
+ end
136
+
137
+ ran ? ret : false
138
+ end
139
+
140
+ def peek(consumer = nil, &block)
141
+ callable = from_callable_class(consumer, block, J8::Consumer)
142
+
143
+ @enumerator.entries.tap do |entries|
144
+ entries.each { |v| callable.accept(v) }
145
+ @enumerator.rewind
146
+ end.j8_stream
147
+ end
148
+
149
+ def reduce(binary_operator = nil, memo: nil, &block)
150
+ callable = from_callable_class(binary_operator, block, J8::BinaryOperator)
151
+
152
+ J8::Optional.of(
153
+ @enumerator.reduce(memo) do |accumulator, object|
154
+ callable.apply(accumulator, object)
155
+ end
156
+ )
157
+ end
158
+
159
+ def skip(amount)
160
+ @enumerator.slice_after(amount).to_a.last.j8_stream
161
+ end
162
+
163
+ def sort
164
+ @enumerator.sort.j8_stream
165
+ end
166
+
167
+ def sort_by(comparator = nil, &block)
168
+ callable = from_callable_class(comparator, block, J8::Comparator)
169
+
170
+ @enumerator.sort { |a, b| callable.compare(a, b) }.j8_stream
171
+ end
172
+
173
+ def to_a
174
+ @enumerator.to_a
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module J8
4
+ class Supplier
5
+ include J8::Common
6
+
7
+ def get
8
+ @callable.call
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: j8-functional
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Shields
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-21 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gem-release
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-doc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rb-inotify
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - john@shields.wtf
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - j8-functional.gemspec
145
+ - lib/j8-functional.rb
146
+ - lib/j8/bi_consumer.rb
147
+ - lib/j8/bi_function.rb
148
+ - lib/j8/bi_predicate.rb
149
+ - lib/j8/binary_operator.rb
150
+ - lib/j8/collector.rb
151
+ - lib/j8/collectors.rb
152
+ - lib/j8/common.rb
153
+ - lib/j8/comparator.rb
154
+ - lib/j8/consumer.rb
155
+ - lib/j8/function.rb
156
+ - lib/j8/functional.rb
157
+ - lib/j8/functional/exceptions.rb
158
+ - lib/j8/functional/version.rb
159
+ - lib/j8/optional.rb
160
+ - lib/j8/predicate.rb
161
+ - lib/j8/stream.rb
162
+ - lib/j8/supplier.rb
163
+ homepage: https://john.shields.wtf
164
+ licenses:
165
+ - MIT
166
+ metadata:
167
+ allowed_push_host: https://rubygems.org
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: 2.3.0
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubygems_version: 3.1.4
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Java 8 inspired functional utilities
187
+ test_files: []