epitools 0.5.59 → 0.5.60
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/VERSION +1 -1
- data/lib/epitools/core_ext/array.rb +36 -3
- data/lib/epitools/core_ext/enumerable.rb +73 -0
- data/lib/epitools/hexdump.rb +3 -3
- data/lib/epitools/iter.rb +14 -5
- data/lib/epitools/path.rb +1 -1
- data/spec/autoreq_spec.rb +26 -26
- data/spec/core_ext_spec.rb +51 -14
- data/spec/path_spec.rb +1 -1
- metadata +2 -4
- data/lib/epitools/ezdb.rb +0 -100
- data/spec/ezdb_spec.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f76bdcc4911578c98d5b2ab7e37fb34f9033205
|
4
|
+
data.tar.gz: fd983d2ffc809874983ff153f8c87e31d65f39d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 881360cb002b0d78c2c313dcb6465b417aa472d08f3a5686cec612fdd0892ede20b6daa22502ef643189618312d43b872d57437ab38ab93279ad4e52717bb6cf
|
7
|
+
data.tar.gz: 1853d1fd9df5a4ac19901a480b672d54888061e3843c302962326b819ea4cd4f43648dd5156b0d02410dadbfaa553ffce2c4effa17030e3180c0ae4a83b86815
|
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.60
|
@@ -1,6 +1,12 @@
|
|
1
1
|
|
2
2
|
class Array
|
3
3
|
|
4
|
+
#
|
5
|
+
# Better names
|
6
|
+
#
|
7
|
+
alias_method :lpush, :unshift
|
8
|
+
alias_method :lpop, :shift
|
9
|
+
|
4
10
|
#
|
5
11
|
# flatten.compact.uniq
|
6
12
|
#
|
@@ -20,13 +26,13 @@ class Array
|
|
20
26
|
#
|
21
27
|
# nums = [1,2,3,4,5,6,7,8,9,10,11,12]
|
22
28
|
# even = nums.remove_if { |n| n.even? } # remove all even numbers from the "nums" array and return them
|
23
|
-
# odd
|
29
|
+
# odd = nums # "nums" now only contains odd numbers
|
24
30
|
#
|
25
31
|
def remove_if(&block)
|
26
32
|
removed = []
|
27
33
|
|
28
34
|
delete_if do |x|
|
29
|
-
if
|
35
|
+
if yield(x)
|
30
36
|
removed << x
|
31
37
|
true
|
32
38
|
else
|
@@ -91,7 +97,7 @@ class Array
|
|
91
97
|
#
|
92
98
|
unless instance_methods.include? :shuffle
|
93
99
|
def shuffle
|
94
|
-
sort_by{rand}
|
100
|
+
sort_by { rand }
|
95
101
|
end
|
96
102
|
end
|
97
103
|
|
@@ -181,6 +187,33 @@ class Array
|
|
181
187
|
|
182
188
|
end
|
183
189
|
|
190
|
+
|
191
|
+
alias_method :old_multiply, :*
|
192
|
+
private :old_multiply
|
193
|
+
|
194
|
+
#
|
195
|
+
# Overridden multiplication operator. Now lets you multiply the Array by another Array or Enumerable.
|
196
|
+
#
|
197
|
+
# Array * Integer == a new array with <Integer> copies of itself inside
|
198
|
+
# Array * String == a new string containing the elements, joined by the <String>
|
199
|
+
# Array * {Array or Enumerable} == the cross product (aka. cartesian product) of both arrays
|
200
|
+
#
|
201
|
+
def *(other)
|
202
|
+
case other
|
203
|
+
when Integer, String
|
204
|
+
old_multiply(other)
|
205
|
+
when Enumerable
|
206
|
+
cross_product(other).to_a
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
#
|
211
|
+
# Multiply the array by itself `n` times.
|
212
|
+
#
|
213
|
+
def **(n)
|
214
|
+
super.to_a
|
215
|
+
end
|
216
|
+
|
184
217
|
end
|
185
218
|
|
186
219
|
|
@@ -390,6 +390,34 @@ module Enumerable
|
|
390
390
|
end
|
391
391
|
alias_method :grouped, :groups
|
392
392
|
|
393
|
+
|
394
|
+
#
|
395
|
+
# Multiplies this Enumerable by something. (Same behaviour as Enumerator#*)
|
396
|
+
#
|
397
|
+
def *(other)
|
398
|
+
case other
|
399
|
+
when Integer, String
|
400
|
+
to_enum * other
|
401
|
+
when Enumerable
|
402
|
+
to_enum.cross_product(other)
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
#
|
407
|
+
# Multiplies this Enumerable by itself `n` times.
|
408
|
+
#
|
409
|
+
def **(n)
|
410
|
+
[self].cycle(n).reduce(:*)
|
411
|
+
end
|
412
|
+
|
413
|
+
#
|
414
|
+
# Same behaviour as Enumerator#cross_product
|
415
|
+
#
|
416
|
+
def cross_product(other)
|
417
|
+
to_enum.cross_product(other)
|
418
|
+
end
|
419
|
+
alias_method :cross, :cross_product
|
420
|
+
|
393
421
|
end
|
394
422
|
|
395
423
|
|
@@ -419,4 +447,49 @@ class Enumerator
|
|
419
447
|
end
|
420
448
|
end
|
421
449
|
|
450
|
+
|
451
|
+
#
|
452
|
+
# Concatenates two Enumerators, returning a new Enumerator.
|
453
|
+
#
|
454
|
+
def +(other)
|
455
|
+
raise "Can only concatenate Enumerable things to Enumerators" unless Enumerable === other
|
456
|
+
|
457
|
+
Enumerator.new do |yielder|
|
458
|
+
each { |e| yielder << e }
|
459
|
+
other.each { |e| yielder << e }
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
|
464
|
+
#
|
465
|
+
# Multiplies this Enumerator by something else.
|
466
|
+
#
|
467
|
+
# Enumerator * Integer == a new Enumerator that repeats the original one <Integer> times
|
468
|
+
# Enumerator * String == joins the Enumerator's elements into a new string, with <String> in between each pair of elements
|
469
|
+
# Enumerator * Enumerable == the cross product (aka. cartesian product) of the Enumerator and the Enumerable
|
470
|
+
#
|
471
|
+
def *(other)
|
472
|
+
case other
|
473
|
+
when Integer
|
474
|
+
cycle(other)
|
475
|
+
when String
|
476
|
+
join(other)
|
477
|
+
when Enumerable
|
478
|
+
cross(other)
|
479
|
+
else
|
480
|
+
raise "#{other.class} is not something that can be multiplied by an Enumerator"
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
#
|
485
|
+
# Takes the cross product (aka. cartesian product) of the Enumerator and the argument,
|
486
|
+
# returning a new Enumerator. (The argument must be some kind of Enumerable.)
|
487
|
+
#
|
488
|
+
def cross_product(other)
|
489
|
+
Enumerator.new do |yielder|
|
490
|
+
each { |a| other.each { |b| yielder << [a,b] } }
|
491
|
+
end
|
492
|
+
end
|
493
|
+
alias_method :cross, :cross_product
|
494
|
+
|
422
495
|
end
|
data/lib/epitools/hexdump.rb
CHANGED
data/lib/epitools/iter.rb
CHANGED
@@ -70,7 +70,7 @@ class Iter
|
|
70
70
|
class Elem < BasicObject
|
71
71
|
|
72
72
|
attr_accessor :val, :visited
|
73
|
-
|
73
|
+
|
74
74
|
def initialize(iter, val)
|
75
75
|
@iter = iter
|
76
76
|
@val = val.is_a?(Elem) ? val.value : val
|
@@ -81,10 +81,6 @@ class Iter
|
|
81
81
|
true
|
82
82
|
end
|
83
83
|
|
84
|
-
def ==(other)
|
85
|
-
self.eql?(other)
|
86
|
-
end
|
87
|
-
|
88
84
|
def container
|
89
85
|
@iter.container
|
90
86
|
end
|
@@ -161,6 +157,19 @@ class Iter
|
|
161
157
|
def inspect
|
162
158
|
"<Elem: #{@val.inspect}>"
|
163
159
|
end
|
160
|
+
|
161
|
+
|
162
|
+
include ::Comparable
|
163
|
+
|
164
|
+
def <=>(other)
|
165
|
+
case other
|
166
|
+
when Elem
|
167
|
+
@val <=> other.value
|
168
|
+
else
|
169
|
+
@val <=> other
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
164
173
|
end
|
165
174
|
|
166
175
|
|
data/lib/epitools/path.rb
CHANGED
data/spec/autoreq_spec.rb
CHANGED
@@ -2,37 +2,37 @@ require 'epitools/minimal'
|
|
2
2
|
|
3
3
|
describe "autoreq" do
|
4
4
|
|
5
|
-
it "should have Haml and Units installed" do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
5
|
+
# it "should have Haml and Units installed" do
|
6
|
+
# gems = Gem.source_index.to_a.map{|name, spec| spec.name}.uniq
|
7
|
+
# gems.include?("haml").should == true
|
8
|
+
# gems.include?("units").should == true
|
9
|
+
# end
|
10
10
|
|
11
|
-
it "autoreqs a gem" do
|
12
|
-
|
11
|
+
# it "autoreqs a gem" do
|
12
|
+
# defined?(Haml).should == nil
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
14
|
+
# autoreq :Haml, 'haml'
|
15
|
+
# !!defined?(Haml).should == false
|
16
|
+
# lambda { Haml }.should_not raise_error
|
17
|
+
# end
|
18
18
|
|
19
|
-
it "autoreqs a regular ruby file" do
|
20
|
-
|
19
|
+
# it "autoreqs a regular ruby file" do
|
20
|
+
# defined?(Net).should == nil
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
# module Net
|
23
|
+
# autoreq :HTTP, 'net/http'
|
24
|
+
# end
|
25
|
+
# lambda { Net::HTTP }.should_not raise_error
|
26
|
+
# end
|
27
27
|
|
28
|
-
it "autoreqs a gem with a block" do
|
29
|
-
|
28
|
+
# it "autoreqs a gem with a block" do
|
29
|
+
# defined?(Units).should == nil
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
31
|
+
# autoreq :Units do
|
32
|
+
# gem 'units', '~> 1.0'
|
33
|
+
# require 'units'
|
34
|
+
# end
|
35
|
+
# lambda { Units }.should_not raise_error
|
36
|
+
# end
|
37
37
|
|
38
38
|
end
|
data/spec/core_ext_spec.rb
CHANGED
@@ -445,24 +445,24 @@ end
|
|
445
445
|
|
446
446
|
describe Enumerable do
|
447
447
|
|
448
|
-
|
449
|
-
|
448
|
+
it "maps deeply" do
|
449
|
+
[["a\n", "b\n"], ["c\n", "d\n"]].map_recursively(&:strip).should == [ %w[a b], %w[c d] ]
|
450
450
|
|
451
|
-
|
452
|
-
|
453
|
-
|
451
|
+
[[1,2],[3,4]].deep_map {|e| e ** 2}.should == [[1,4],[9,16]]
|
452
|
+
[1,2,3,4].deep_map {|e| e ** 2}.should == [1,4,9,16]
|
453
|
+
[[],[],1,2,3,4].deep_map {|e| e ** 2}.should == [[], [], 1, 4, 9, 16]
|
454
454
|
|
455
|
-
|
456
|
-
|
455
|
+
{1=>2, 3=>{4=>5, 6=>7}}.deep_map {|k,v| [k, v**2] }.should == [ [1,4], [3, [[4,25], [6,49]]] ]
|
456
|
+
end
|
457
457
|
|
458
|
-
|
459
|
-
|
460
|
-
|
458
|
+
it "selects deeply" do
|
459
|
+
[[1,2],[3,4]].deep_select {|e| e % 2 == 0 }.should == [[2],[4]]
|
460
|
+
puts
|
461
461
|
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
462
|
+
{1=>2, 3=>{4=>5, 6=>7}}.deep_select {|k,v| k == 1 }.should == {1=>2}
|
463
|
+
#[1,2,3,4].deep_select {|e| e ** 2}.should == [1,4,9,16]
|
464
|
+
#[[],[],1,2,3,4].deep_select {|e| e ** 2}.should == [[], [], 1, 4, 9, 16]
|
465
|
+
end
|
466
466
|
|
467
467
|
it "splits" do
|
468
468
|
[1,2,3,4,5].split_at {|e| e == 3}.should == [ [1,2], [4,5] ]
|
@@ -540,6 +540,13 @@ describe Enumerable do
|
|
540
540
|
result.should == 10.times.permutation(2).to_a
|
541
541
|
end
|
542
542
|
|
543
|
+
it "cartesian products" do
|
544
|
+
a = [1,2]
|
545
|
+
b = [3,4]
|
546
|
+
|
547
|
+
(a * b).to_a.should == [[1,3],[1,4],[2,3],[2,4]]
|
548
|
+
end
|
549
|
+
|
543
550
|
end
|
544
551
|
|
545
552
|
describe Enumerator do
|
@@ -550,6 +557,36 @@ describe Enumerator do
|
|
550
557
|
}.should_not raise_error
|
551
558
|
end
|
552
559
|
|
560
|
+
it "concatenates" do
|
561
|
+
e = [1,2,3].to_enum + [4,5,6].to_enum
|
562
|
+
e.to_a.should == [1,2,3,4,5,6]
|
563
|
+
|
564
|
+
e = [1,2,3].to_enum + [4,5,6]
|
565
|
+
e.to_a.should == [1,2,3,4,5,6]
|
566
|
+
end
|
567
|
+
|
568
|
+
it "multiplies" do
|
569
|
+
e = [1,2,3].to_enum * 3
|
570
|
+
e.to_a.should == [1,2,3]*3
|
571
|
+
|
572
|
+
e = [1,2].to_enum * [3,4].to_enum
|
573
|
+
e.to_a.should == [[1,3],[1,4],[2,3],[2,4]]
|
574
|
+
|
575
|
+
lambda { [].old_multiply }.should raise_error(NoMethodError)
|
576
|
+
end
|
577
|
+
|
578
|
+
it "exponentiates" do
|
579
|
+
e = [1,2].to_enum ** 2
|
580
|
+
squared = [[1,1], [1,2], [2,1], [2,2]]
|
581
|
+
|
582
|
+
e.to_a.should == squared
|
583
|
+
|
584
|
+
e = [1,2].to_enum ** 3
|
585
|
+
|
586
|
+
e.to_a.should == squared * [1,2]
|
587
|
+
e.to_a.should == ([1,2]**3)
|
588
|
+
end
|
589
|
+
|
553
590
|
end
|
554
591
|
|
555
592
|
|
data/spec/path_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epitools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.60
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- epitron
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -61,7 +61,6 @@ files:
|
|
61
61
|
- lib/epitools/core_ext/range.rb
|
62
62
|
- lib/epitools/core_ext/string.rb
|
63
63
|
- lib/epitools/core_ext/truthiness.rb
|
64
|
-
- lib/epitools/ezdb.rb
|
65
64
|
- lib/epitools/hexdump.rb
|
66
65
|
- lib/epitools/iter.rb
|
67
66
|
- lib/epitools/its.rb
|
@@ -90,7 +89,6 @@ files:
|
|
90
89
|
- spec/clitools_spec.rb
|
91
90
|
- spec/colored_spec.rb
|
92
91
|
- spec/core_ext_spec.rb
|
93
|
-
- spec/ezdb_spec.rb
|
94
92
|
- spec/histogram_spec.rb
|
95
93
|
- spec/iter_spec.rb
|
96
94
|
- spec/lcs_spec.rb
|
data/lib/epitools/ezdb.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'epitools'
|
2
|
-
require 'dbm'
|
3
|
-
require 'delegate'
|
4
|
-
|
5
|
-
class Ezdc < DelegateClass(Hash)
|
6
|
-
|
7
|
-
attr_reader :db, :path, :dirty
|
8
|
-
|
9
|
-
@@dirty = Set.new
|
10
|
-
|
11
|
-
def initialize(filename)
|
12
|
-
@path = Path[filename]
|
13
|
-
|
14
|
-
if @path.ext.nil?
|
15
|
-
@path.ext = "db"
|
16
|
-
else
|
17
|
-
@path.ext += ".db" if @path.ext != 'db'
|
18
|
-
end
|
19
|
-
|
20
|
-
@db = DBM::open(@path.with(:ext=>nil))
|
21
|
-
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
|
-
class Observed < BasicObject
|
26
|
-
MUTATORS = ::Set.new [
|
27
|
-
:<<, :push, :pop, :slice, :[]=
|
28
|
-
]
|
29
|
-
|
30
|
-
def __send__(meth, *args)
|
31
|
-
if MUTATORS.include? meth
|
32
|
-
@@dirty.add self
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def observed(obj)
|
39
|
-
obj.using(Observed)
|
40
|
-
end
|
41
|
-
|
42
|
-
def [](key)
|
43
|
-
observed(super[key])
|
44
|
-
end
|
45
|
-
|
46
|
-
def []=(key, val)
|
47
|
-
end
|
48
|
-
|
49
|
-
def keys
|
50
|
-
db.keys.map(&:unmarshal)
|
51
|
-
end
|
52
|
-
|
53
|
-
def delete!
|
54
|
-
@path.rm
|
55
|
-
end
|
56
|
-
|
57
|
-
def flush!
|
58
|
-
dirty.each do |key|
|
59
|
-
db[key.marshal] = super[key].marshal
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
class Ezdb
|
67
|
-
|
68
|
-
attr_reader :db, :path
|
69
|
-
|
70
|
-
def initialize(filename)
|
71
|
-
@path = Path[filename]
|
72
|
-
|
73
|
-
if @path.ext.nil?
|
74
|
-
@path.ext = "db"
|
75
|
-
else
|
76
|
-
@path.ext += ".db" if @path.ext != 'db'
|
77
|
-
end
|
78
|
-
|
79
|
-
@db = DBM::open(@path.with(:ext=>nil))
|
80
|
-
end
|
81
|
-
|
82
|
-
def [](key)
|
83
|
-
val = db[key.marshal]
|
84
|
-
val = val.unmarshal if val
|
85
|
-
val
|
86
|
-
end
|
87
|
-
|
88
|
-
def []=(key, val)
|
89
|
-
db[key.marshal] = val.marshal
|
90
|
-
end
|
91
|
-
|
92
|
-
def keys
|
93
|
-
db.keys.map(&:unmarshal)
|
94
|
-
end
|
95
|
-
|
96
|
-
def delete!
|
97
|
-
@path.rm
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
data/spec/ezdb_spec.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'epitools/ezdb'
|
2
|
-
require 'epitools/path'
|
3
|
-
|
4
|
-
class Test < Struct.new(:a, :b); end
|
5
|
-
|
6
|
-
describe Ezdb do
|
7
|
-
|
8
|
-
attr_accessor :db
|
9
|
-
|
10
|
-
before :each do
|
11
|
-
@dbfile = Path["test.db"]
|
12
|
-
@dbfile.rm if @dbfile.exists?
|
13
|
-
@db = Ezdb.new @dbfile
|
14
|
-
end
|
15
|
-
|
16
|
-
after :each do
|
17
|
-
@db.delete!
|
18
|
-
end
|
19
|
-
|
20
|
-
it "stores/retrieves" do
|
21
|
-
db[1].should == nil
|
22
|
-
db[1] = :blah
|
23
|
-
db[1].should == :blah
|
24
|
-
db.keys.should == [1]
|
25
|
-
|
26
|
-
s = Test.new("what", true)
|
27
|
-
db[s] = false
|
28
|
-
db[s].should == false
|
29
|
-
end
|
30
|
-
|
31
|
-
it "handles nil extensions" do
|
32
|
-
x = Ezdb.new "testdb"
|
33
|
-
x[1].should == nil
|
34
|
-
x.delete!
|
35
|
-
end
|
36
|
-
|
37
|
-
it "pushes" do
|
38
|
-
db[:a] ||= []
|
39
|
-
db[:a].should == []
|
40
|
-
db[:a] << 1
|
41
|
-
db[:a] << 2
|
42
|
-
db[:a] << 3
|
43
|
-
db[:a] << 4
|
44
|
-
db[:a].should == [1,2,3,4]
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|