ruby-stream-api 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +36 -6
- data/lib/fromarray.rb +51 -0
- data/lib/version.rb +1 -1
- data/release.sh +1 -0
- data/ruby-stream-api.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13e90c8ce33e8514c95bb829fde6cb17d8a0cea449de2462286cf36e201dadce
|
4
|
+
data.tar.gz: ba63d2d3fa2c1dcf34c12acfff0b2e0d9dd31386e0245468d642d5f8c56dd864
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2312cb8ed1c8746af6ddd5eb099591e47b2a7a35b3dd6e064acde06bc830ca2f741a2ca3cf22d99194734889a7285e125299cbd32ee7938897879600c18c4afd
|
7
|
+
data.tar.gz: 63872e2f76fabdd64308459ce20aaf667625aed87caaf6338cf56f40da9f097928343ea33abdb3af236a25a76ef45aebcce687c6b4f4898de1ea9ae8a5ec1a07
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Ruby Stream API
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/ruby-stream-api)
|
3
4
|
[](https://travis-ci.org/ruby-ee/ruby-stream-api)
|
4
5
|
[](https://codecov.io/github/ruby-ee/ruby-stream-api?branch=master)
|
5
6
|
|
@@ -13,19 +14,48 @@ operations to modify and/or get information about the collection. The operations
|
|
13
14
|
* **Intermediate** operations (skip, filter, map etc) -- operations which are altering the Stream and still leave it open for further modifications.
|
14
15
|
* **Terminal** operations (count, collect etc) -- operations which are executed after all the modifications have been done and are returning a finite result.
|
15
16
|
|
16
|
-
First glance
|
17
|
+
First glance:
|
18
|
+
|
19
|
+
1) **Finite** Stream from an array:
|
17
20
|
|
18
21
|
```ruby
|
19
|
-
array = [1, 2,
|
20
|
-
stream = Stream::FromArray(array)
|
22
|
+
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
23
|
+
stream = Stream::FromArray.new(array)
|
21
24
|
collected = stream
|
22
|
-
.
|
25
|
+
.filter { |num| num % 2 == 0 }
|
23
26
|
.skip(2)
|
24
27
|
.collect
|
25
|
-
puts collected # [
|
28
|
+
puts collected # [6, 8, 10]
|
29
|
+
```
|
30
|
+
|
31
|
+
2) Generate a (potentially) **infinite** stream:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
stream = Stream.generate(150) { &seed }
|
26
35
|
```
|
27
36
|
|
28
|
-
|
37
|
+
The ``generate`` method takes a ``limit`` (max number of elements) and a ``&seed`` Block function which
|
38
|
+
returns a new element at each ``seed.call``. A limit is necessary as, without it, this Stream would be infinite.
|
39
|
+
If no limit is specified, the default is ``100`` elements.
|
40
|
+
|
41
|
+
This mechanism is useful, for instance, when you have to consume an incomming stream of objects from some ``IO`` objects.
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
```ruby
|
47
|
+
gem 'ruby-stream-api'
|
48
|
+
```
|
49
|
+
|
50
|
+
Or install it as a separate gem:
|
51
|
+
```bash
|
52
|
+
$gem install ruby-stream-api
|
53
|
+
```
|
54
|
+
|
55
|
+
To require it inside your Ruby program do:
|
56
|
+
```ruby
|
57
|
+
require 'stream'
|
58
|
+
```
|
29
59
|
|
30
60
|
## Not a Mixin
|
31
61
|
|
data/lib/fromarray.rb
CHANGED
@@ -26,12 +26,14 @@ module Stream
|
|
26
26
|
# A stream implemented based on an Array.
|
27
27
|
# This class is immutable and thread-safe.
|
28
28
|
# Author:: Mihai Andronache (amihaiemil@gmail.com)
|
29
|
+
# Since:: 0.0.1
|
29
30
|
class FromArray
|
30
31
|
def initialize(array)
|
31
32
|
@array = array
|
32
33
|
end
|
33
34
|
|
34
35
|
# Return the number of elements in this stream.
|
36
|
+
# This is a terminal operation.
|
35
37
|
def count
|
36
38
|
@array.length
|
37
39
|
end
|
@@ -43,6 +45,7 @@ module Stream
|
|
43
45
|
# collected = stream.filter {|num| num % 2 == 0}.collect
|
44
46
|
# puts collected # [2, 4]
|
45
47
|
#
|
48
|
+
# This is an intermediary operation.
|
46
49
|
# +condition+:: Ruby Block taking one parameter (the stream element) and
|
47
50
|
# returning a boolean check on it.
|
48
51
|
def filter(&condition)
|
@@ -60,6 +63,7 @@ module Stream
|
|
60
63
|
# collected = stream.map {|num| num.to_s}.collect
|
61
64
|
# puts collected # ['1', '2', '3']
|
62
65
|
#
|
66
|
+
# This is an intermediary operation.
|
63
67
|
# +function+:: Ruby Block function taking one parameter
|
64
68
|
# (the element in the stream).
|
65
69
|
def map(&function)
|
@@ -70,7 +74,18 @@ module Stream
|
|
70
74
|
FromArray.new(mapped)
|
71
75
|
end
|
72
76
|
|
77
|
+
# Remove all the duplicates from the stream.
|
78
|
+
# This is an intermediary operation.
|
79
|
+
def distinct
|
80
|
+
unique = []
|
81
|
+
@array.each do |val|
|
82
|
+
unique.push(val) unless unique.include? val
|
83
|
+
end
|
84
|
+
FromArray.new(unique)
|
85
|
+
end
|
86
|
+
|
73
87
|
# Skip the first n elements of the stream.
|
88
|
+
# This is an intermediary operation.
|
74
89
|
# +count+:: Number of elements to skip from the beginning of the stream.
|
75
90
|
def skip(count)
|
76
91
|
raise ArgumentError, 'count has to be positive integer' unless count.positive? and count.is_a? Integer
|
@@ -82,7 +97,43 @@ module Stream
|
|
82
97
|
FromArray.new(skipped)
|
83
98
|
end
|
84
99
|
|
100
|
+
# Returns true if all the elements of the Stream are matching the
|
101
|
+
# given predicate (a function which performs a test on the value and
|
102
|
+
# should return a boolean).
|
103
|
+
#
|
104
|
+
# If the stream is empty, the returned value is true and the predicate
|
105
|
+
# is not called at all.
|
106
|
+
#
|
107
|
+
# This is a terminal operation.
|
108
|
+
# +test+:: A function which should perform some boolean test on the
|
109
|
+
# given value.
|
110
|
+
def all_match(&test)
|
111
|
+
@array.each do |val|
|
112
|
+
return false unless test.call(val)
|
113
|
+
end
|
114
|
+
true
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns true if any of the elements of the Stream are matching
|
118
|
+
# the given predicate (a function which performs a test on the value and
|
119
|
+
# should return a boolean). Iteration will stop at the first match.
|
120
|
+
#
|
121
|
+
# If the stream is empty, the returned value is false and the predicate
|
122
|
+
# is not called at all.
|
123
|
+
#
|
124
|
+
# This is a terminal operation.
|
125
|
+
#
|
126
|
+
# +test+:: A function which should perform some boolean test on the
|
127
|
+
# given value.
|
128
|
+
def any_match(&test)
|
129
|
+
@array.each do |val|
|
130
|
+
return true if test.call(val)
|
131
|
+
end
|
132
|
+
false
|
133
|
+
end
|
134
|
+
|
85
135
|
# Collect the stream's data into an array and return it.
|
136
|
+
# This is a terminal operation.
|
86
137
|
def collect
|
87
138
|
@array.dup
|
88
139
|
end
|
data/lib/version.rb
CHANGED
data/release.sh
CHANGED
@@ -29,6 +29,7 @@ gem push *.gem --config-file /home/r/rubygems.yml
|
|
29
29
|
|
30
30
|
# set next dev version in version.rb
|
31
31
|
sed -i "s/'${tag}'.freeze # rultor/'${NEXT_VERSION}'.freeze # rultor/" ./lib/version.rb
|
32
|
+
bundle install
|
32
33
|
|
33
34
|
git commit -am "${NEXT_VERSION}"
|
34
35
|
git checkout master
|
data/ruby-stream-api.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
17
17
|
spec.metadata["source_code_uri"] = "https://github.com/ruby-ee/ruby-stream-api"
|
18
|
-
spec.metadata["changelog_uri"] = "https://github.com/ruby-ee/ruby-stream-api"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/ruby-ee/ruby-stream-api/releases"
|
19
19
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-stream-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- amihaiemil
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Stream API for Ruby, inspired by Java 8's Stream API
|
14
14
|
email:
|
@@ -40,7 +40,7 @@ metadata:
|
|
40
40
|
allowed_push_host: https://rubygems.org/
|
41
41
|
homepage_uri: https://github.com/ruby-ee/ruby-stream-api
|
42
42
|
source_code_uri: https://github.com/ruby-ee/ruby-stream-api
|
43
|
-
changelog_uri: https://github.com/ruby-ee/ruby-stream-api
|
43
|
+
changelog_uri: https://github.com/ruby-ee/ruby-stream-api/releases
|
44
44
|
post_install_message:
|
45
45
|
rdoc_options: []
|
46
46
|
require_paths:
|