lazzay 0.0.1

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. checksums.yaml +7 -0
  2. data/lib/lazzay.rb +90 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 176423842880afadfd3a6c1822c9c9c33ea9a4909a4f00f1b80a01a61102a29b
4
+ data.tar.gz: a207e0829f458b387db7f6114622f863140089d2ba8e459a9df8567d6375a29e
5
+ SHA512:
6
+ metadata.gz: c932ba6992803dd05a46e8a8e85e98714a9b2c4610b8a0c6af57cec997321c8af1d1e2bae27dd005d14199e33b2e260b473f3daf27406058e605918f2228231c
7
+ data.tar.gz: 0f972a40a537bf09fcc2884b15a1cc3a78d78832653567c85e995468d97ded9a8cc29464c01550283c472d2d717308992fbc7fedccf4959e42cd35d591e19cb3
data/lib/lazzay.rb ADDED
@@ -0,0 +1,90 @@
1
+ =begin
2
+ Lazy Array
3
+
4
+ Example
5
+ -----
6
+ b = ForwardArray.new
7
+ b <= [1, 2] + b
8
+
9
+ puts b.to_s
10
+ puts b[5] => 2
11
+ =end
12
+
13
+ class Array
14
+ def +(other)
15
+ if other.is_a?(LazyArray)
16
+ AddArray.new(self, other)
17
+ else
18
+ super
19
+ end
20
+ end
21
+ end
22
+
23
+ class LazyArray < Array
24
+ # lazy array
25
+ def +(other)
26
+ if ! other.is_a?(LazyArray)
27
+ other = LazyArray.new(other)
28
+ end
29
+ AddArray.new(self, other)
30
+ end
31
+
32
+ end
33
+
34
+ class ForwardArray < LazyArray
35
+ attr_accessor :subarray
36
+
37
+ def initialize(subarray=[])
38
+ @subarray = subarray
39
+ end
40
+
41
+ def [](k)
42
+ @subarray[k]
43
+ end
44
+
45
+ def <=(other)
46
+ @subarray = other
47
+ end
48
+
49
+ def to_s
50
+ "ForwardArray"
51
+ end
52
+ end
53
+
54
+ class AddArray < LazyArray
55
+ <<~DOC
56
+ An array, concatenating tow arrays with +
57
+
58
+ Example:
59
+ b = ForwardArray.new # must define b here
60
+ b <= [1, 2] + b # b == [1, 2] + b i.e. b=[1,2,1,2,...]
61
+ puts b[0] # => 1
62
+ puts b[5] # => 2
63
+ DOC
64
+
65
+ attr_accessor :left, :right
66
+
67
+ def initialize(left=[], right=[])
68
+ <<~DOC
69
+ Arguments
70
+ ------
71
+ left: left side of +
72
+ right: right side of +
73
+ DOC
74
+ @left, @right = left, right
75
+ end
76
+
77
+ def [](k)
78
+ l = @left.length
79
+ if k < l
80
+ return @left[k]
81
+ else
82
+ return @right[k - l]
83
+ end
84
+ end
85
+
86
+ def to_s
87
+ "#{@left} + #{@right}"
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazzay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - William Song
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple lib for creating lazy arrays, named lazzay
14
+ email: songcwzjut@163.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/lazzay.rb
20
+ homepage: http://rubygems.org/gems/lazzay
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.7.7
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Lazy arrays.
44
+ test_files: []