lazy_stream 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/lazy_stream.rb +113 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 166defa34d053f972bbed9af3675bc3f4b6c3da0
|
4
|
+
data.tar.gz: 9f003140bb1e8bea9d54c32a15d36644a9d15dcc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4302666af3e03ae2c8f7faa57431addd7b8c7598fa66c7587f86c64026b77168992f3af6bd8718faed5cd7bf1ac172aaa3d51a5113b488979b1b9059043417ed
|
7
|
+
data.tar.gz: d6e7f3d96e1715f5743d8a85a8349d2414b5957a66810c803a2dbdbf9d0b0d924380cc07ee87a8d23048c5583ee8827da72b7565f7924c7d58f5f1f8047c1a99
|
data/lib/lazy_stream.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class LazyStream
|
4
|
+
def initialize(first=nil, &proc)
|
5
|
+
@first = first
|
6
|
+
@proc = block_given? ? proc : lambda { LazyStream.new }
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :first
|
10
|
+
|
11
|
+
def rest
|
12
|
+
@rest ||= @proc.call
|
13
|
+
end
|
14
|
+
|
15
|
+
def empty?
|
16
|
+
first.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def at(n)
|
20
|
+
if empty?
|
21
|
+
nil
|
22
|
+
elsif n == 0
|
23
|
+
first
|
24
|
+
else
|
25
|
+
rest.at(n - 1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def drop(n)
|
30
|
+
empty? || n < 1 ? self : rest.drop(n - 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
def each(&proc)
|
34
|
+
unless empty?
|
35
|
+
proc.call(first)
|
36
|
+
rest.each(&proc)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def map(&proc)
|
41
|
+
empty? ? self : LazyStream.new(proc.call(first)) { rest.map(&proc) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def reduce(initial=0, &proc)
|
45
|
+
empty? ? initial : rest.reduce(proc.call(initial, first), &proc)
|
46
|
+
end
|
47
|
+
|
48
|
+
def select(&pred)
|
49
|
+
if empty?
|
50
|
+
self
|
51
|
+
elsif pred.call(first)
|
52
|
+
LazyStream.new(first) { rest.select(&pred) }
|
53
|
+
else
|
54
|
+
rest.select(&pred)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def take(n)
|
59
|
+
empty? || n < 1 ? LazyStream.new :
|
60
|
+
LazyStream.new(first) { rest.take(n - 1) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_a
|
64
|
+
reduce([]) { |a, x| a << x }
|
65
|
+
end
|
66
|
+
|
67
|
+
def print
|
68
|
+
each { |x| puts x }
|
69
|
+
end
|
70
|
+
|
71
|
+
def scale(factor)
|
72
|
+
map { |x| x * factor }
|
73
|
+
end
|
74
|
+
|
75
|
+
def partial_sums(initial=0)
|
76
|
+
partial_sum = initial + first
|
77
|
+
LazyStream.new(partial_sum) { rest.partial_sums(partial_sum) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def map_successive_pairs(&proc)
|
81
|
+
if empty? || rest.empty?
|
82
|
+
LazyStream.new
|
83
|
+
else
|
84
|
+
LazyStream.new(proc.call(first, rest.first)) do
|
85
|
+
rest.rest.map_successive_pairs(&proc)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.map(*streams, &proc)
|
91
|
+
if streams.first.empty?
|
92
|
+
LazyStream.new
|
93
|
+
else
|
94
|
+
LazyStream.new(proc.call(*streams.map(&:first))) do
|
95
|
+
map(*streams.map(&:rest), &proc)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.add(*streams)
|
101
|
+
map(*streams) { |*args| args.reduce(&:+) }
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.interleave(s1, s2)
|
105
|
+
s1.empty? ? s2 : LazyStream.new(s1.first) { interleave(s2, s1.rest) }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
module Kernel
|
110
|
+
def lazy_stream(first=nil, &rest)
|
111
|
+
LazyStream.new(first, &rest)
|
112
|
+
end
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_stream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mingmin Xie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby infinite lazy stream
|
14
|
+
email: melvinxie@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/lazy_stream.rb
|
20
|
+
homepage: https://github.com/melvinxie/lazy_stream
|
21
|
+
licenses: []
|
22
|
+
metadata: {}
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - '>='
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
requirements: []
|
38
|
+
rubyforge_project:
|
39
|
+
rubygems_version: 2.0.3
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: lazy_stream
|
43
|
+
test_files: []
|