piperator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e41e83c8480b4089277077c8c9e067a973113f2a
4
+ data.tar.gz: '0780f80b1baf8104a16abd923df4664f909b9756'
5
+ SHA512:
6
+ metadata.gz: 3b433fd2b72437ac5b02a0905c39fb94d1a4b62a084f4907f427c7103100347d2fdd318588d93ef081bcf1faca37174deca31018e0d720f954950750e14e7018
7
+ data.tar.gz: 59dcd7ce0c99342b7e02fa14d2705cc05a0abe6c5d8a65511fc632872659e03ca44347589b14e361424b9b50735062d12a4ddc7ebf3cd3eb25e45e7b212c7657
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in piperator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ville Lautanala
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.
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # Piperator
2
+
3
+ Pipelines for streaming large collections. The pipeline enables composition of streaming pipelines with lazy enumerables.
4
+
5
+ The library is heavily inspired by [Elixir pipe operator](https://elixirschool.com/lessons/basics/pipe-operator/) and [Node.js Stream](https://nodejs.org/api/stream.html).
6
+
7
+ [![Build Status](https://travis-ci.org/lautis/piperator.svg?branch=master)](https://travis-ci.org/lautis/piperator)
8
+
9
+
10
+ ## Installation
11
+
12
+ Piperator is distributed as a ruby gem and can be installed with
13
+
14
+ ```
15
+ $ gem install piperator
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Start by requiring the gem
21
+
22
+ ```ruby
23
+ require 'piperator'
24
+ ```
25
+
26
+ As an appetiser, here's a pipeline that triples all input values and then sums the values.
27
+
28
+ ```ruby
29
+ Piperator.
30
+ pipe(->(values) { values.lazy.map { |i| i * 3 } }).
31
+ pipe(->(values) { values.sum }).
32
+ call([1, 2, 3])
33
+ # => 18
34
+ ```
35
+
36
+ If desired, the input enumerable can also be given as the first pipe.
37
+
38
+ ```ruby
39
+ Piperator.
40
+ pipe([1, 2, 3]).
41
+ pipe(->(values) { values.lazy.map { |i| i * 3 } }).
42
+ pipe(->(values) { values.sum }).
43
+ call
44
+ # => 18
45
+ ```
46
+
47
+ There is, of course, a much more idiomatic alternative in Ruby:
48
+
49
+ ```ruby
50
+ [1, 2, 3].map { |i| i * 3 }.sum
51
+ ```
52
+
53
+ So why bother?
54
+
55
+ To run code before the stream processing start and after processing has ended. Let's use the same pattern to calculate the decompressed length of a GZip file fetched over HTTP with streaming.
56
+
57
+ ```ruby
58
+ require 'piperator'
59
+ require 'uri'
60
+ require 'em-http-request'
61
+ require 'net/http'
62
+
63
+ module HTTPFetch
64
+ def self.call(url)
65
+ uri = URI(url)
66
+ Enumerator.new do |yielder|
67
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
68
+ request = Net::HTTP::Get.new(uri.request_uri)
69
+ http.request request do |response|
70
+ response.read_body { |chunk| yielder << chunk }
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ module GZipDecoder
78
+ def self.call(enumerable)
79
+ Enumerator.new do |yielder|
80
+ decoder = EventMachine::HttpDecoders::GZip.new do |chunk|
81
+ yielder << chunk
82
+ end
83
+
84
+ enumerable.each { |chunk| decoder << chunk }
85
+ yielder << decoder.finalize.to_s
86
+ end
87
+ end
88
+ end
89
+
90
+ length = proc do |enumerable|
91
+ enumerable.lazy.reduce(0) { |aggregate, chunk| aggregate + chunk.length }
92
+ end
93
+
94
+ Piperator.
95
+ pipe(HTTPFetch).
96
+ pipe(GZipDecoder).
97
+ pipe(length).
98
+ call('http://ftp.gnu.org/gnu/gzip/gzip-1.2.4.tar.gz')
99
+ ```
100
+
101
+ At no point is it necessary to keep the full response or decompressed content in memory. This is a huge win when the file sizes grow beyond the 780kB seen in the example.
102
+
103
+ Pipelines themselves respond to `#call`. This enables using pipelines as pipes in other pipelines.
104
+
105
+ ```ruby
106
+ append_end = proc do |enumerator|
107
+ Enumerator.new do |yielder|
108
+ enumerator.each { |item| yielder << item }
109
+ yielder << 'end'
110
+ end
111
+ end
112
+
113
+ prepend_start = proc do |enumerator|
114
+ Enumerator.new do |yielder|
115
+ yielder << 'start'
116
+ enumerator.each { |item| yielder << item }
117
+ end
118
+ end
119
+
120
+ double = ->(enumerator) { enumerator.lazy.map { |i| i * 2 } }
121
+
122
+ prepend_append = Piperator.pipe(prepend_start).pipe(append_end)
123
+ Piperator.pipe(double).pipe(prepend_append).call([1, 2, 3]).to_a
124
+ # => ['start', 2, 4, 6, 'end']
125
+ ```
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lautis/piperator.
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'piperator'
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(__FILE__)
data/bin/setup ADDED
@@ -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
data/lib/piperator.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'piperator/version'
2
+ require 'piperator/pipeline'
3
+
4
+ # Top-level shortcuts
5
+ module Piperator
6
+ # Build a new pipeline from a callable or an enumerable object
7
+ #
8
+ # @see Piperator::Pipeline.pipe
9
+ # @param callable An object responding to call(enumerable)
10
+ # @return [Pipeline] A pipeline containing only the callable
11
+ def self.pipe(enumerable)
12
+ Pipeline.pipe(enumerable)
13
+ end
14
+ end
@@ -0,0 +1,57 @@
1
+ module Piperator
2
+ # Pipeline is responsible of composition of a lazy enumerable from callables.
3
+ # It contains a collection of pipes that respond to #call and return a
4
+ # enumerable.
5
+ #
6
+ # For streaming purposes, it usually is desirable to have pipes that takes
7
+ # a lazy Enumerator as an argument a return a (modified) lazy Enumerator.
8
+ class Pipeline
9
+ # Build a new pipeline from a callable or an enumerable object
10
+ #
11
+ # @param callable An object responding to call(enumerable)
12
+ # @return [Pipeline] A pipeline containing only the callable
13
+ def self.pipe(callable)
14
+ if callable.respond_to?(:call)
15
+ Pipeline.new([callable])
16
+ else
17
+ Pipeline.new([->(_) { callable }])
18
+ end
19
+ end
20
+
21
+ # Returns enumerable given as an argument without modifications. Usable when
22
+ # Pipeline is used as an identity transformation.
23
+ #
24
+ # @param enumerable [Enumerable]
25
+ # @return [Enumerable]
26
+ def self.call(enumerable = [])
27
+ enumerable
28
+ end
29
+
30
+ def initialize(pipes = [])
31
+ @pipes = pipes
32
+ end
33
+
34
+ # Compute the pipeline and return a lazy enumerable with all the pipes.
35
+ #
36
+ # @param enumerable Argument passed to the first pipe in the pipeline.
37
+ # @return [Enumerable] A lazy enumerable containing all the pipes
38
+ def call(enumerable = [])
39
+ @pipes.reduce(enumerable) { |pipe, memo| memo.call(pipe) }
40
+ end
41
+
42
+ # Compute the pipeline and strictly evaluate the result
43
+ #
44
+ # @return [Array]
45
+ def to_a(enumerable = [])
46
+ call(enumerable).to_a
47
+ end
48
+
49
+ # Add a new part to the pipeline
50
+ #
51
+ # @param other A pipe to append in pipeline. Responds to #call.
52
+ # @return [Pipeline] A new pipeline instance
53
+ def pipe(other)
54
+ Pipeline.new(@pipes + [other])
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Piperator
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/piperator.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'piperator/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'piperator'
9
+ spec.version = Piperator::VERSION
10
+ spec.authors = ['Ville Lautanala']
11
+ spec.email = ['lautis@gmail.com']
12
+
13
+ spec.summary = 'Composable pipelines for streaming large collections'
14
+ spec.description = 'Pipelines for streaming large collections with ' \
15
+ 'composition inspired by Elixir pipes.'
16
+ spec.homepage = 'https://github.com/lautis/piperator'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 12.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: piperator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ville Lautanala
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-23 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
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
+ description: Pipelines for streaming large collections with composition inspired by
56
+ Elixir pipes.
57
+ email:
58
+ - lautis@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/piperator.rb
73
+ - lib/piperator/pipeline.rb
74
+ - lib/piperator/version.rb
75
+ - piperator.gemspec
76
+ homepage: https://github.com/lautis/piperator
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Composable pipelines for streaming large collections
100
+ test_files: []