syntheticore-perlize 0.1.1 → 0.1.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.
Files changed (3) hide show
  1. data/README.textile +21 -3
  2. data/perlize.rb +60 -2
  3. metadata +1 -1
data/README.textile CHANGED
@@ -1,7 +1,7 @@
1
1
  h3. The Ruby perlizer
2
2
 
3
- This module implements some of the cooler features
4
- of Perl6 in Ruby, like topics, junctions and hyperoperators.
3
+ This module implements some of the coolest features
4
+ of Perl6 in Ruby, like topics, junctions, lazy lists and hyperoperators.
5
5
  Extremely hackish currently, needs to be generalized
6
6
  somewhat in the future.
7
7
 
@@ -49,7 +49,7 @@ that in parallel.
49
49
  <pre>
50
50
  <code>
51
51
  all(1,2,3,4,5).each{ puts $__ }
52
- #=>
52
+ # prints:
53
53
  3
54
54
  5
55
55
  2
@@ -59,6 +59,24 @@ that in parallel.
59
59
  </pre>
60
60
 
61
61
 
62
+ h3. Lazy lists
63
+
64
+ <pre>
65
+ <code>
66
+ results = [1,2,3].lazy.map{ $__ * 2 }.map{ $__ - 1 }
67
+ </code>
68
+ </pre>
69
+ The results array is instantly available, without ever running the map methods.
70
+ Only when you request one of its values, these are actually calculated.
71
+ You can force the evaluation of all elements using the eager method.
72
+ <pre>
73
+ <code>
74
+ results[2] #=> 5
75
+ results.eager #=> [1,3,5]
76
+ </code>
77
+ </pre>
78
+
79
+
62
80
  h3. Hyperoperators
63
81
 
64
82
  <pre>
data/perlize.rb CHANGED
@@ -2,13 +2,68 @@
2
2
  #
3
3
  # Created by Björn Breitgoff on 17.7.2008.
4
4
  #
5
- # Implements some of the cooler features of Perl6
5
+ # Implements some of the coolest features of Perl6
6
6
  # in Ruby, like topics, junctions and hyperoperators.
7
7
  # Extremely hackish currently, needs to be generalized
8
8
  # somewhat in the future
9
9
 
10
10
 
11
11
 
12
+ ###------------ Lazy lists ------------###
13
+
14
+ class Array
15
+ def lazy
16
+ LazyList.new self
17
+ end
18
+ end
19
+
20
+ class LazyList
21
+ attr_accessor :ary, :meths
22
+ def initialize ary
23
+ @ary = ary
24
+ @meths = []
25
+ end
26
+
27
+ def [] i
28
+ eager[i]
29
+ end
30
+
31
+ def method_missing( meth, *args, &b )
32
+ @cache = nil
33
+ meth = meth.to_s
34
+ list = if meth =~ /\w!/
35
+ meth[-1] = ""
36
+ self
37
+ else
38
+ dup
39
+ end
40
+ list.meths << [meth, args, b]
41
+ list
42
+ end
43
+
44
+ def to_a
45
+ unless @cache
46
+ a = @ary
47
+ for meth, args, b in @meths
48
+ a = a.send( meth, *args, &b )
49
+ end
50
+ @cache = a
51
+ end
52
+ @cache.dup
53
+ end
54
+ alias eager to_a
55
+
56
+ def dup
57
+ copy = super
58
+ copy.ary = @ary.dup
59
+ copy.meths = @meths.dup
60
+ copy
61
+ end
62
+ end
63
+
64
+
65
+
66
+
12
67
  ###------------ Topicalization ------------###
13
68
 
14
69
  class Module
@@ -39,7 +94,7 @@ def given o
39
94
  end
40
95
 
41
96
  class Array
42
- _topicalize_ *%w(each map select sort_by any? all?)
97
+ _topicalize_ *%w(each map map! select sort_by any? all?)
43
98
  end
44
99
 
45
100
  class Range
@@ -214,3 +269,6 @@ end
214
269
  class String
215
270
  _junctionize_
216
271
  end
272
+
273
+
274
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntheticore-perlize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Bj\xC3\xB6rn Breitgoff"