rubylude 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/MIT-LICENSE +20 -0
- data/README.md +38 -0
- data/lib/rubylude.rb +101 -0
- metadata +60 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bruno Michel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
Rubylude
|
2
|
+
========
|
3
|
+
|
4
|
+
[Marc Chantreux](https://twitter.com/#!/marcchantreux) has written a lib to
|
5
|
+
emulate haskell lazyness in Perl:
|
6
|
+
[Perlude](https://github.com/eiro/p5-perlude). Rubylude is port of Perlude to
|
7
|
+
Ruby 1.9 that profits of the fibers.
|
8
|
+
|
9
|
+
Install
|
10
|
+
-------
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install rubylude
|
14
|
+
```
|
15
|
+
|
16
|
+
Example
|
17
|
+
-------
|
18
|
+
|
19
|
+
To print the first 10 Fibonacci numbers that are a mutiple of 5, just do :
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "rubylude"
|
23
|
+
|
24
|
+
fibo = ->() {
|
25
|
+
a, b = 0, 1
|
26
|
+
loop do
|
27
|
+
a, b = b, a + b
|
28
|
+
Fiber.yield a
|
29
|
+
end
|
30
|
+
}
|
31
|
+
|
32
|
+
Rubylude.new(fibo).filter { |x| x % 5 == 0 }.take(10).traverse { |x| puts x }
|
33
|
+
```
|
34
|
+
|
35
|
+
Credits
|
36
|
+
-------
|
37
|
+
|
38
|
+
♡2011 by Bruno Michel. Copying is an act of love. Please copy and share.
|
data/lib/rubylude.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
class Rubylude
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
|
4
|
+
Infinity = 1.0 / 0
|
5
|
+
|
6
|
+
# Generator: create a Rubylude from a lambda function
|
7
|
+
def initialize(generator)
|
8
|
+
@generator = Fiber.new { generator.call; nil }
|
9
|
+
end
|
10
|
+
|
11
|
+
# Generator: takes an array and inject one element at a time
|
12
|
+
def self.unfold(values)
|
13
|
+
Rubylude.new ->() {
|
14
|
+
values.each {|v| Fiber.yield v }
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Generator: inject these values indefinitely
|
19
|
+
def self.cycle(values)
|
20
|
+
Rubylude.new ->() {
|
21
|
+
loop do
|
22
|
+
values.each {|v| Fiber.yield v }
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Generator: generate values from `start` to `stop`, separated by `step`
|
28
|
+
def self.range(start, stop=Infinity, step=1)
|
29
|
+
Rubylude.new ->() {
|
30
|
+
current = start - step
|
31
|
+
if step > 0
|
32
|
+
while (current += step) <= stop
|
33
|
+
Fiber.yield current
|
34
|
+
end
|
35
|
+
else
|
36
|
+
while (current += step) >= stop
|
37
|
+
Fiber.yield current
|
38
|
+
end
|
39
|
+
end
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Transformer: let pass only the elements that return true for the block
|
44
|
+
def filter(&blk)
|
45
|
+
Rubylude.new ->() {
|
46
|
+
while value = @generator.resume
|
47
|
+
Fiber.yield value if blk.call(value)
|
48
|
+
end
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
# Transformer: modify each element with the given block
|
53
|
+
def apply(&blk)
|
54
|
+
Rubylude.new ->() {
|
55
|
+
while value = @generator.resume
|
56
|
+
Fiber.yield blk.call(value)
|
57
|
+
end
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Transformer: let pass only the first `nb` elements
|
62
|
+
def take(nb)
|
63
|
+
Rubylude.new ->() {
|
64
|
+
nb.times { Fiber.yield @generator.resume }
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
# Transformer: if one element returns false, block it and all the following
|
69
|
+
def takeWhile(&blk)
|
70
|
+
Rubylude.new ->() {
|
71
|
+
while value = @generator.resume
|
72
|
+
return unless blk.call(value)
|
73
|
+
Fiber.yield value
|
74
|
+
end
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
# Transformer: drop the first `nb` elements
|
79
|
+
def drop(nb)
|
80
|
+
Rubylude.new ->() {
|
81
|
+
nb.times { @generator.resume }
|
82
|
+
while value = @generator.resume
|
83
|
+
Fiber.yield value
|
84
|
+
end
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
# Consumer: do an operation on each element
|
89
|
+
def traverse(&blk)
|
90
|
+
while value = @generator.resume
|
91
|
+
blk.call(value)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Consumer: returns an array
|
96
|
+
def to_a
|
97
|
+
results = []
|
98
|
+
traverse { |value| results << value }
|
99
|
+
results
|
100
|
+
end
|
101
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubylude
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bruno Michel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-07-14 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: &75191740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *75191740
|
25
|
+
description: Rubylude is a port of Perlude to Ruby 1.9
|
26
|
+
email: bruno.michel@af83.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README.md
|
31
|
+
files:
|
32
|
+
- MIT-LICENSE
|
33
|
+
- README.md
|
34
|
+
- Gemfile
|
35
|
+
- lib/rubylude.rb
|
36
|
+
homepage: http://github.com/nono/rubylude
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Rubylude is a port of Perlude to Ruby 1.9
|
60
|
+
test_files: []
|