facets 2.1.3 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +14 -12
- data/CHANGES +45 -2
- data/NOTES +9 -7
- data/lib/core/facets/1stclassmethod.rb +15 -11
- data/lib/core/facets/array.rb +0 -1
- data/lib/core/facets/array/conjoin.rb +40 -8
- data/lib/core/facets/array/delete.rb +8 -2
- data/lib/core/facets/array/indexable.rb +30 -3
- data/lib/core/facets/array/merge.rb +11 -0
- data/lib/core/facets/array/only.rb +3 -3
- data/lib/core/facets/array/pad.rb +4 -46
- data/lib/core/facets/array/rotate.rb +4 -4
- data/lib/core/facets/array/select.rb +2 -0
- data/lib/core/facets/array/splice.rb +16 -2
- data/lib/core/facets/array/stackable.rb +6 -40
- data/lib/core/facets/binding.rb +3 -0
- data/lib/core/facets/binding/cflow.rb +10 -64
- data/lib/core/facets/binding/defined.rb +10 -0
- data/lib/core/facets/binding/eval.rb +10 -76
- data/lib/core/facets/binding/here.rb +9 -0
- data/lib/core/facets/binding/self.rb +10 -0
- data/lib/core/facets/binding/vars.rb +0 -45
- data/lib/core/facets/boolean.rb +31 -103
- data/lib/core/facets/class/descendents.rb +17 -44
- data/lib/core/facets/comparable/bound.rb +8 -70
- data/lib/core/facets/comparable/cmp.rb +12 -92
- data/lib/core/facets/conversion.rb +122 -321
- data/lib/core/facets/dir/multiglob.rb +0 -13
- data/lib/core/facets/dir/traverse.rb +61 -111
- data/lib/core/facets/enumerable.rb +4 -2
- data/lib/core/facets/enumerable/cartesian.rb +36 -85
- data/lib/core/facets/enumerable/collect.rb +64 -152
- data/lib/core/facets/enumerable/combination.rb +40 -42
- data/lib/core/facets/enumerable/count.rb +99 -174
- data/lib/core/facets/enumerable/each.rb +86 -0
- data/lib/core/facets/enumerable/mash.rb +58 -0
- data/lib/core/facets/enumerable/permutation.rb +39 -44
- data/lib/core/facets/enumerable/probability.rb +16 -90
- data/lib/core/facets/enumerable/split.rb +113 -0
- data/lib/core/facets/exception/detail.rb +1 -2
- data/lib/core/facets/facets.rb +3 -2
- data/lib/core/facets/file/read.rb +4 -59
- data/lib/core/facets/file/topath.rb +8 -96
- data/lib/core/facets/file/write.rb +21 -49
- data/lib/core/facets/filetest/root.rb +4 -3
- data/lib/core/facets/functor.rb +5 -43
- data/lib/core/facets/hash/at.rb +7 -37
- data/lib/core/facets/hash/delete.rb +9 -58
- data/lib/core/facets/hash/has_keys.rb +4 -43
- data/lib/core/facets/hash/insert.rb +5 -38
- data/lib/core/facets/hash/inverse.rb +2 -41
- data/lib/core/facets/hash/iterate.rb +4 -45
- data/lib/core/facets/hash/keyize.rb +24 -92
- data/lib/core/facets/hash/merge.rb +4 -38
- data/lib/core/facets/hash/new.rb +5 -38
- data/lib/core/facets/hash/op.rb +20 -66
- data/lib/core/facets/hash/rekey.rb +9 -87
- data/lib/core/facets/hash/select.rb +1 -1
- data/lib/core/facets/hash/slice.rb +42 -0
- data/lib/core/facets/hash/traverse.rb +6 -46
- data/lib/core/facets/hash/update.rb +27 -67
- data/lib/core/facets/hash/weave.rb +22 -42
- data/lib/core/facets/indexable.rb +42 -14
- data/lib/core/facets/integer/bitmask.rb +69 -106
- data/lib/core/facets/integer/factorial.rb +1 -44
- data/lib/core/facets/integer/multiples.rb +27 -22
- data/lib/core/facets/integer/of.rb +1 -33
- data/lib/core/facets/kernel.rb +4 -2
- data/lib/core/facets/kernel/ask.rb +15 -3
- data/lib/core/facets/kernel/callstack.rb +38 -69
- data/lib/core/facets/kernel/constant.rb +2 -38
- data/lib/core/facets/kernel/deepcopy.rb +3 -55
- data/lib/core/facets/kernel/dir.rb +2 -0
- data/lib/core/facets/kernel/ergo.rb +2 -2
- data/lib/core/facets/kernel/instance.rb +51 -120
- data/lib/core/facets/kernel/metaid.rb +35 -73
- data/lib/core/facets/kernel/object.rb +14 -39
- data/lib/core/facets/kernel/op_esc.rb +24 -2
- data/lib/core/facets/kernel/populate.rb +6 -69
- data/lib/core/facets/kernel/report.rb +28 -33
- data/lib/core/facets/kernel/require.rb +7 -21
- data/lib/core/facets/kernel/respond.rb +11 -1
- data/lib/core/facets/kernel/returning.rb +50 -0
- data/lib/core/facets/kernel/silence.rb +12 -24
- data/lib/core/facets/kernel/super.rb +12 -76
- data/lib/core/facets/kernel/tap.rb +25 -107
- data/lib/core/facets/kernel/val.rb +7 -46
- data/lib/core/facets/kernel/withattr.rb +6 -64
- data/lib/core/facets/matchdata/matchset.rb +8 -50
- data/lib/core/facets/module.rb +3 -1
- data/lib/core/facets/module/abstract.rb +9 -47
- data/lib/core/facets/module/alias.rb +11 -68
- data/lib/core/facets/module/attr.rb +36 -15
- data/lib/core/facets/module/cattr.rb +8 -76
- data/lib/core/facets/module/clone.rb +15 -66
- data/lib/core/facets/module/include.rb +54 -114
- data/lib/core/facets/module/methods.rb +15 -13
- data/lib/core/facets/module/modify.rb +20 -206
- data/lib/core/facets/module/name.rb +19 -83
- data/lib/core/facets/module/require.rb +48 -53
- data/lib/core/facets/module/traits.rb +74 -33
- data/lib/core/facets/nilclass/status.rb +25 -2
- data/lib/core/facets/numeric/round.rb +24 -89
- data/lib/core/facets/proc/bind.rb +16 -59
- data/lib/core/facets/proc/compose.rb +6 -40
- data/lib/core/facets/proc/fn.rb +2 -0
- data/lib/core/facets/range/combine.rb +15 -51
- data/lib/core/facets/range/overlap.rb +8 -64
- data/lib/core/facets/regexp/arity.rb +6 -41
- data/lib/core/facets/stackable.rb +43 -4
- data/lib/core/facets/string.rb +2 -0
- data/lib/core/facets/string/align.rb +45 -55
- data/lib/core/facets/string/blank.rb +2 -46
- data/lib/core/facets/string/bracket.rb +14 -87
- data/lib/core/facets/string/case.rb +45 -32
- data/lib/core/facets/string/crypt.rb +3 -40
- data/lib/core/facets/string/filter.rb +7 -49
- data/lib/core/facets/string/format.rb +24 -126
- data/lib/core/facets/string/indexable.rb +50 -133
- data/lib/core/facets/string/interpolate.rb +24 -59
- data/lib/core/facets/string/natcmp.rb +5 -39
- data/lib/core/facets/string/nchar.rb +37 -45
- data/lib/core/facets/string/op.rb +13 -0
- data/lib/core/facets/string/partitions.rb +38 -99
- data/lib/core/facets/string/range.rb +8 -49
- data/lib/core/facets/string/regesc.rb +9 -42
- data/lib/core/facets/string/scan.rb +6 -52
- data/lib/core/facets/string/splice.rb +8 -13
- data/lib/core/facets/string/tabs.rb +16 -153
- data/lib/core/facets/symbol.rb +3 -1
- data/lib/core/facets/symbol/chomp.rb +11 -29
- data/lib/core/facets/symbol/generate.rb +4 -43
- data/lib/core/facets/symbol/not.rb +13 -25
- data/lib/core/facets/symbol/shadow.rb +2 -40
- data/lib/core/facets/symbol/succ.rb +14 -13
- data/lib/core/facets/symbol/to_proc.rb +33 -28
- data/lib/core/facets/time.rb +1 -0
- data/lib/core/facets/time/change.rb +5 -38
- data/lib/core/facets/time/elapse.rb +2 -41
- data/lib/core/facets/time/stamp.rb +47 -0
- data/lib/core/facets/unboundmethod.rb +3 -0
- data/lib/core/facets/unboundmethod/arguments.rb +27 -4
- data/lib/core/facets/unboundmethod/name.rb +21 -7
- data/lib/methods/facets/enumerable/cluster_by.rb +1 -1
- data/lib/methods/facets/enumerable/collate.rb +1 -0
- data/lib/methods/facets/enumerable/divide.rb +1 -1
- data/lib/methods/facets/enumerable/each_by.rb +1 -1
- data/lib/methods/facets/enumerable/each_pair.rb +1 -1
- data/lib/methods/facets/enumerable/eachn.rb +1 -1
- data/lib/methods/facets/enumerable/graph.rb +1 -1
- data/lib/methods/facets/enumerable/group_by.rb +1 -1
- data/lib/methods/facets/enumerable/inject.rb +1 -0
- data/lib/methods/facets/enumerable/map_send.rb +1 -0
- data/lib/methods/facets/enumerable/modulate.rb +1 -0
- data/lib/methods/facets/enumerable/partition_by.rb +1 -1
- data/lib/methods/facets/hash/collate.rb +1 -1
- data/lib/methods/facets/hash/except.rb +1 -0
- data/lib/methods/facets/hash/graph.rb +1 -1
- data/lib/methods/facets/hash/mash.rb +1 -0
- data/lib/methods/facets/kernel/Bit.rb +1 -0
- data/lib/methods/facets/kernel/complete.rb +1 -1
- data/lib/methods/facets/kernel/here.rb +1 -1
- data/lib/methods/facets/kernel/non_nil.rb +1 -0
- data/lib/methods/facets/kernel/respond_with_value.rb +1 -0
- data/lib/methods/facets/kernel/with.rb +1 -1
- data/lib/methods/facets/module/class_def.rb +1 -0
- data/lib/methods/facets/string/camelcase.rb +1 -0
- data/lib/methods/facets/string/op_minus.rb +1 -0
- data/lib/methods/facets/string/snakecase.rb +1 -0
- data/lib/more/facets/attributes.rb +5 -5
- data/lib/more/facets/basicobject.rb +0 -62
- data/lib/{core/facets/continuation/create.rb → more/facets/continuation.rb} +4 -21
- data/lib/more/facets/duration.rb +534 -0
- data/lib/more/facets/lazy.rb +3 -3
- data/lib/more/facets/namespace.rb +23 -71
- data/lib/more/facets/rbsystem.rb +42 -8
- data/lib/more/facets/stylize.rb +6 -3
- data/lib/more/facets/thread.rb +55 -0
- data/meta/MANIFEST +49 -18
- data/meta/ROLLRC +1 -1
- data/meta/project.yaml +2 -2
- data/task/install +1 -1
- data/task/test/general +8 -2
- data/test/unit/array/test_pad.rb +30 -45
- data/test/unit/array/test_stackable.rb +14 -28
- data/test/unit/binding/test_cflow.rb +34 -49
- data/test/unit/binding/test_defined.rb +17 -0
- data/test/unit/binding/test_eval.rb +11 -39
- data/test/unit/binding/test_here.rb +17 -0
- data/test/unit/binding/test_self.rb +17 -0
- data/test/unit/binding/test_vars.rb +22 -37
- data/test/unit/class/test_descendents.rb +15 -30
- data/test/unit/class/test_initializer.rb +1 -8
- data/test/unit/comparable/test_bound.rb +38 -53
- data/test/unit/comparable/test_cmp.rb +45 -60
- data/test/unit/dir/test_traverse.rb +56 -0
- data/test/unit/enumerable/test_cartesian.rb +34 -47
- data/test/unit/enumerable/test_collect.rb +16 -125
- data/test/unit/enumerable/test_combination.rb +1 -8
- data/test/unit/enumerable/test_count.rb +51 -66
- data/test/unit/enumerable/test_each.rb +77 -0
- data/test/unit/enumerable/test_mash.rb +51 -0
- data/test/unit/enumerable/test_permutation.rb +1 -7
- data/test/unit/enumerable/test_probability.rb +42 -55
- data/test/unit/enumerable/test_split.rb +52 -0
- data/test/unit/exception/test_detail.rb +19 -0
- data/test/unit/file/test_read.rb +34 -0
- data/test/unit/file/test_topath.rb +17 -25
- data/test/unit/file/test_write.rb +6 -12
- data/test/unit/filetest/test_root.rb +14 -0
- data/test/unit/hash/test_at.rb +9 -21
- data/test/unit/hash/test_delete.rb +22 -37
- data/test/unit/hash/test_has_keys.rb +13 -28
- data/test/unit/hash/test_insert.rb +9 -23
- data/test/unit/hash/test_inverse.rb +11 -26
- data/test/unit/hash/test_iterate.rb +16 -31
- data/test/unit/hash/test_keyize.rb +40 -55
- data/test/unit/hash/test_merge.rb +10 -25
- data/test/unit/hash/test_new.rb +9 -24
- data/test/unit/hash/test_op.rb +1 -7
- data/test/unit/hash/test_rekey.rb +47 -62
- data/test/unit/hash/test_traverse.rb +17 -32
- data/test/unit/hash/test_update.rb +30 -45
- data/test/unit/hash/test_weave.rb +10 -23
- data/test/unit/integer/test_bitmask.rb +48 -44
- data/test/unit/integer/test_factorial.rb +11 -26
- data/test/unit/integer/test_multiples.rb +1 -5
- data/test/unit/integer/test_of.rb +14 -29
- data/test/unit/kernel/test_callstack.rb +13 -28
- data/test/unit/kernel/test_constant.rb +14 -28
- data/test/unit/kernel/test_deepcopy.rb +20 -35
- data/test/unit/kernel/test_instance.rb +46 -61
- data/test/unit/kernel/test_metaid.rb +42 -57
- data/test/unit/kernel/test_object.rb +14 -29
- data/test/unit/kernel/test_populate.rb +38 -53
- data/test/unit/kernel/test_report.rb +1 -7
- data/test/unit/kernel/test_returning.rb +16 -0
- data/test/unit/kernel/test_silence.rb +8 -23
- data/test/unit/kernel/test_super.rb +41 -56
- data/test/unit/kernel/test_tap.rb +15 -39
- data/test/unit/kernel/test_val.rb +7 -20
- data/test/unit/kernel/test_withattr.rb +29 -44
- data/test/unit/matchdata/test_matchset.rb +20 -35
- data/test/unit/module/test_abstract.rb +15 -30
- data/test/unit/module/test_alias.rb +32 -45
- data/test/unit/module/test_cattr.rb +39 -54
- data/test/unit/module/test_clone.rb +31 -43
- data/test/unit/module/test_include.rb +1 -35
- data/test/unit/module/test_modify.rb +102 -114
- data/test/unit/module/test_name.rb +1 -7
- data/test/unit/numeric/test_round.rb +46 -61
- data/test/unit/proc/test_bind.rb +29 -44
- data/test/unit/proc/test_compose.rb +20 -35
- data/test/unit/range/test_combine.rb +14 -29
- data/test/unit/range/test_overlap.rb +34 -51
- data/test/unit/regexp/test_arity.rb +14 -29
- data/test/unit/string/test_align.rb +13 -27
- data/test/unit/string/test_blank.rb +12 -27
- data/test/unit/string/test_bracket.rb +52 -67
- data/test/unit/string/test_case.rb +19 -38
- data/test/unit/string/test_crypt.rb +10 -25
- data/test/unit/string/test_filter.rb +1 -7
- data/test/unit/string/test_format.rb +2 -7
- data/test/unit/string/test_indexable.rb +54 -69
- data/test/unit/string/test_interpolate.rb +8 -21
- data/test/unit/string/test_natcmp.rb +11 -26
- data/test/unit/string/test_nchar.rb +1 -14
- data/test/unit/string/test_op.rb +14 -0
- data/test/unit/string/test_partitions.rb +39 -54
- data/test/unit/string/test_range.rb +17 -32
- data/test/unit/string/test_regesc.rb +1 -7
- data/test/unit/string/test_scan.rb +23 -38
- data/test/unit/string/test_stackable.rb +79 -98
- data/test/unit/string/test_tabs.rb +1 -7
- data/test/unit/symbol/test_chomp.rb +10 -21
- data/test/unit/symbol/test_generate.rb +7 -22
- data/test/unit/symbol/test_not.rb +10 -25
- data/test/unit/symbol/test_shadow.rb +8 -23
- data/test/unit/symbol/test_succ.rb +1 -7
- data/test/unit/symbol/test_to_proc.rb +1 -7
- data/test/unit/test_attributes.rb +1 -1
- data/test/unit/test_continuation.rb +13 -0
- data/test/unit/test_conversion.rb +1 -14
- data/test/unit/test_namespace.rb +30 -3
- data/test/unit/test_thread.rb +23 -0
- data/test/unit/time/test_change.rb +11 -26
- data/test/unit/time/test_elapse.rb +10 -25
- data/test/unit/time/test_stamp.rb +28 -0
- metadata +65 -25
- data/lib/core/facets/array/unzip.rb +0 -14
- data/lib/core/facets/continuation.rb +0 -1
- data/lib/core/facets/enumerable/collate.rb +0 -104
- data/lib/core/facets/kernel/require_esc.rb +0 -44
- data/lib/more/facets/mapsend.rb +0 -98
- data/lib/more/facets/pp_s.rb +0 -30
- data/log/history.rd +0 -38
- data/log/todo.rd +0 -4
- data/task/special/quickopts +0 -15
- data/test/unit/continuation/test_create.rb +0 -28
- data/test/unit/enumerable/test_collate.rb +0 -51
- data/test/unit/kernel/test_require_esc.rb +0 -29
- data/test/unit/test_mapsend.rb +0 -18
- data/test/unit/test_pp_s.rb +0 -17
@@ -1,60 +1,40 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
# Dir Travserse Extensions
|
4
|
-
#
|
5
|
-
# SUMMARY:
|
6
|
-
#
|
7
|
-
# Travsersal extensions for Dir class.
|
8
|
-
#
|
9
|
-
# CREDITS:
|
10
|
-
#
|
11
|
-
# - Thomas Sawyer
|
12
|
-
# - Daniel Berger
|
13
|
-
# - Jeffrey Schwab
|
14
|
-
# - George Moschovitis
|
1
|
+
# TODO:
|
2
|
+
# - Make instance method versions.
|
15
3
|
|
16
|
-
#
|
17
4
|
class Dir
|
18
5
|
|
19
6
|
# Is a path parental to another?
|
20
|
-
|
21
|
-
#
|
22
|
-
|
23
|
-
def ancestor?(parent_path, child_path)
|
7
|
+
#
|
8
|
+
# TODO: Needs improvement.
|
9
|
+
|
10
|
+
def self.ancestor?(parent_path, child_path)
|
24
11
|
%r|^#{Regexp.escape(parent_path)}| =~ child_path
|
25
12
|
end
|
26
13
|
|
27
14
|
# Is a path parental to another?
|
28
|
-
|
29
|
-
#
|
30
|
-
#++
|
31
|
-
def parent?(parent_path, child_path)
|
32
|
-
%r|^#{Regexp.escape(parent_path)}| =~ child_path
|
33
|
-
end
|
15
|
+
#
|
16
|
+
# TODO: Needs improvement.
|
34
17
|
|
35
|
-
|
36
|
-
|
37
|
-
def ls_r(path='.', &block)
|
38
|
-
list = []
|
39
|
-
stoplist = ['.', '..']
|
40
|
-
Dir.foreach(path) do |f|
|
41
|
-
next if stoplist.include?(f)
|
42
|
-
filename = path + '/' + f
|
43
|
-
list << filename
|
44
|
-
block.call(filename) if block
|
45
|
-
if FileTest.directory?(filename) and not FileTest.symlink?(filename)
|
46
|
-
list.concat( Dir.recurse(filename, &block) )
|
47
|
-
end
|
48
|
-
end
|
49
|
-
list
|
18
|
+
def self.parent?(parent_path, child_path)
|
19
|
+
%r|^#{Regexp.escape(parent_path)}| =~ child_path
|
50
20
|
end
|
51
21
|
|
52
|
-
# Alias for Dir#ls_r
|
53
|
-
alias_method :recurse, :ls_r
|
54
|
-
|
55
22
|
# Ascend a directory path.
|
56
|
-
|
57
|
-
|
23
|
+
#
|
24
|
+
# Dir.ascend("/var/log") do |path|
|
25
|
+
# p path
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# _produces_
|
29
|
+
#
|
30
|
+
# /var/log
|
31
|
+
# /var
|
32
|
+
# /
|
33
|
+
#
|
34
|
+
# CREDIT: Daniel Berger
|
35
|
+
# CREDIT: Jeffrey Schwab
|
36
|
+
|
37
|
+
def self.ascend(dir, inclusive=true, &blk)
|
58
38
|
dir = dir.dup
|
59
39
|
blk.call(dir) if inclusive
|
60
40
|
ri = dir.rindex('/')
|
@@ -69,80 +49,50 @@ class Dir
|
|
69
49
|
end
|
70
50
|
|
71
51
|
# Descend a directory path.
|
72
|
-
|
73
|
-
|
52
|
+
#
|
53
|
+
# Dir.descend("/var/log") do |path|
|
54
|
+
# p path
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# _produces_
|
58
|
+
#
|
59
|
+
# /
|
60
|
+
# /var
|
61
|
+
# /var/log
|
62
|
+
#
|
63
|
+
# CREDIT: Daniel Berger
|
64
|
+
# CREDIT: Jeffrey Schwab
|
65
|
+
|
66
|
+
def self.descend(path) #:yield:
|
74
67
|
paths = path.split('/')
|
75
68
|
paths.size.times do |n|
|
76
|
-
yield File.join(
|
69
|
+
yield File.join(*paths[0..n])
|
77
70
|
end
|
78
71
|
end
|
79
72
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
#
|
84
|
-
# |_ _|__ ___| |_
|
85
|
-
# | |/ _ \/ __| __|
|
86
|
-
# | | __/\__ \ |_
|
87
|
-
# |_|\___||___/\__|
|
88
|
-
#
|
89
|
-
=begin #no test
|
90
|
-
|
91
|
-
require 'test/unit'
|
92
|
-
|
93
|
-
# TODO Need to mockup dir for tests.
|
94
|
-
|
95
|
-
class TestDirNavigate < Test::Unit::TestCase
|
96
|
-
|
97
|
-
def test_ancestor
|
98
|
-
assert( Dir.ancestor?( "a/b/c", "a/b/c/d" ) )
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_ls_r
|
102
|
-
td = Dir.pwd
|
103
|
-
Dir.chdir $TESTDIR
|
104
|
-
r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e|
|
105
|
-
File.join( 'ls_r', e )
|
106
|
-
}
|
107
|
-
fs = Dir.ls_r( 'ls_r' )
|
108
|
-
assert_equal( r, fs, Dir.pwd )
|
109
|
-
Dir.chdir td
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_recurse
|
113
|
-
td = Dir.pwd
|
114
|
-
Dir.chdir $TESTDIR
|
115
|
-
r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e|
|
116
|
-
File.join( 'ls_r', e )
|
117
|
-
}
|
118
|
-
fs = Dir.recurse( 'ls_r' )
|
119
|
-
assert_equal( r, fs, Dir.pwd )
|
120
|
-
Dir.chdir td
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_parent
|
124
|
-
assert( Dir.ancestor?( "a/b/c", "a/b/c/d" ) )
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_ascend_01
|
128
|
-
c = []
|
129
|
-
Dir.ascend( "this/path/up" ) do |path|
|
130
|
-
c << path
|
131
|
-
end
|
132
|
-
assert_equal( 'this/path/up', c[0] )
|
133
|
-
assert_equal( 'this/path', c[1] )
|
134
|
-
assert_equal( 'this', c[2] )
|
135
|
-
end
|
73
|
+
# Recursively scan a directory and pass each file
|
74
|
+
# to the given block.
|
75
|
+
#
|
76
|
+
# CREDIT: George Moschovitis
|
136
77
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
78
|
+
def self.ls_r(path='.', &block)
|
79
|
+
list = []
|
80
|
+
stoplist = ['.', '..']
|
81
|
+
Dir.foreach(path) do |f|
|
82
|
+
next if stoplist.include?(f)
|
83
|
+
filename = path + '/' + f
|
84
|
+
list << filename
|
85
|
+
block.call(filename) if block
|
86
|
+
if FileTest.directory?(filename) and not FileTest.symlink?(filename)
|
87
|
+
list.concat( Dir.recurse(filename, &block) )
|
141
88
|
end
|
142
|
-
assert_equal( 'this/path', c[0] )
|
143
|
-
assert_equal( 'this', c[1] )
|
144
89
|
end
|
90
|
+
list
|
91
|
+
end
|
145
92
|
|
93
|
+
class << self
|
94
|
+
# Alias for Dir#ls_r
|
95
|
+
alias_method :recurse, :ls_r
|
146
96
|
end
|
147
97
|
|
148
|
-
|
98
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'facets/enumerable/cartesian.rb'
|
2
2
|
require 'facets/enumerable/combination.rb'
|
3
|
-
require 'facets/enumerable/collate.rb'
|
4
3
|
require 'facets/enumerable/collect.rb'
|
5
|
-
require 'facets/enumerable/permutation.rb'
|
6
4
|
require 'facets/enumerable/count.rb'
|
5
|
+
require 'facets/enumerable/each.rb'
|
6
|
+
require 'facets/enumerable/mash.rb'
|
7
|
+
require 'facets/enumerable/permutation.rb'
|
7
8
|
require 'facets/enumerable/probability.rb'
|
9
|
+
require 'facets/enumerable/split.rb'
|
@@ -1,47 +1,36 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Cartesian
|
4
|
-
#
|
5
|
-
# SUMMARY:
|
6
|
-
#
|
7
|
-
# Cartesian product and similar methods.
|
8
|
-
#
|
9
|
-
# AUTHORS:
|
10
|
-
#
|
11
|
-
# - Thomas Hafner
|
12
|
-
|
13
|
-
#
|
14
1
|
module Enumerable
|
15
2
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
3
|
+
# Provides the cross-product of two or more Enumerables.
|
4
|
+
# This is the class-level method. The instance method
|
5
|
+
# calls on this.
|
6
|
+
#
|
7
|
+
# Enumerable.cart([1,2], [4], ["apple", "banana"])
|
8
|
+
# #=> [[1, 4, "apple"], [1, 4, "banana"], [2, 4, "apple"], [2, 4, "banana"]]
|
9
|
+
#
|
10
|
+
# Enumerable.cart([1,2], [3,4])
|
11
|
+
# #=> [[1, 3], [1, 4], [2, 3], [2, 4]]
|
12
|
+
#
|
13
|
+
# CREDIT: Thomas Hafner
|
14
|
+
|
15
|
+
def self.cartesian_product(*enums, &block)
|
16
|
+
result = [[]]
|
17
|
+
while [] != enums
|
18
|
+
t, result = result, []
|
19
|
+
b, *enums = enums
|
20
|
+
t.each do |a|
|
21
|
+
b.each do |n|
|
22
|
+
result << a + [n]
|
36
23
|
end
|
37
24
|
end
|
38
|
-
if block_given?
|
39
|
-
result.each{ |e| block.call(e) }
|
40
|
-
else
|
41
|
-
result
|
42
|
-
end
|
43
25
|
end
|
26
|
+
if block_given?
|
27
|
+
result.each{ |e| block.call(e) }
|
28
|
+
else
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
44
32
|
|
33
|
+
class << self
|
45
34
|
alias_method :cart, :cartesian_product
|
46
35
|
end
|
47
36
|
|
@@ -50,18 +39,22 @@ module Enumerable
|
|
50
39
|
# a = []
|
51
40
|
# [1,2].cart([4,5]){|elem| a << elem }
|
52
41
|
# a #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
|
42
|
+
#
|
43
|
+
# CREDIT: Thomas Hafner
|
53
44
|
|
54
45
|
def cartesian_product(*enums, &block)
|
55
46
|
Enumerable.cartesian_product(self, *enums, &block)
|
56
47
|
end
|
57
48
|
|
58
|
-
|
49
|
+
alias_method :cart, :cartesian_product
|
59
50
|
|
60
51
|
# Operator alias for cross-product.
|
61
52
|
#
|
62
53
|
# a = [1,2] ** [4,5]
|
63
54
|
# a #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
|
64
55
|
#
|
56
|
+
# CREDIT: Trans
|
57
|
+
|
65
58
|
def **(enum)
|
66
59
|
Enumerable.cartesian_product(self, enum)
|
67
60
|
end
|
@@ -79,6 +72,8 @@ module Enumerable
|
|
79
72
|
# [1, 2]
|
80
73
|
# [1, 3]
|
81
74
|
#
|
75
|
+
# CREDIT: Trans
|
76
|
+
|
82
77
|
def each_combo
|
83
78
|
a = collect{ |x|
|
84
79
|
x.respond_to?(:to_a) ? x.to_a : 0..x
|
@@ -101,6 +96,8 @@ module Enumerable
|
|
101
96
|
|
102
97
|
# As with each_combo but returns combos collected in an array.
|
103
98
|
#
|
99
|
+
# CREDIT: Trans
|
100
|
+
|
104
101
|
def combos
|
105
102
|
a = []
|
106
103
|
each_combo{ |c| a << c }
|
@@ -108,49 +105,3 @@ module Enumerable
|
|
108
105
|
end
|
109
106
|
|
110
107
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
# _____ _
|
115
|
-
# |_ _|__ ___| |_
|
116
|
-
# | |/ _ \/ __| __|
|
117
|
-
# | | __/\__ \ |_
|
118
|
-
# |_|\___||___/\__|
|
119
|
-
#
|
120
|
-
=begin test
|
121
|
-
|
122
|
-
require 'test/unit'
|
123
|
-
|
124
|
-
class TestEnumerableClass < Test::Unit::TestCase
|
125
|
-
|
126
|
-
def test_cart_01
|
127
|
-
i = [[1,2], [3,4]]
|
128
|
-
o = [[1, 3], [1, 4], [2, 3], [2, 4]]
|
129
|
-
assert_equal( o, Enumerable.cart(*i) )
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_cart_02
|
133
|
-
i = [[1,2], [4], ["apple", "banana"]]
|
134
|
-
o = [[1, 4, "apple"], [1, 4, "banana"], [2, 4, "apple"], [2, 4, "banana"]]
|
135
|
-
assert_equal( o, Enumerable.cart(*i) )
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_cart_03
|
139
|
-
a = [1,2,3].cart([4,5,6])
|
140
|
-
assert_equal( [[1, 4],[1, 5],[1, 6],[2, 4],[2, 5],[2, 6],[3, 4],[3, 5],[3, 6]], a )
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_cart_04
|
144
|
-
a = []
|
145
|
-
[1,2,3].cart([4,5,6]) {|elem| a << elem }
|
146
|
-
assert_equal( [[1, 4],[1, 5],[1, 6],[2, 4],[2, 5],[2, 6],[3, 4],[3, 5],[3, 6]], a )
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_op_pow
|
150
|
-
a = [1,2,3] ** [4,5,6]
|
151
|
-
assert_equal( [[1, 4],[1, 5],[1, 6],[2, 4],[2, 5],[2, 6],[3, 4],[3, 5],[3, 6]], a )
|
152
|
-
end
|
153
|
-
|
154
|
-
end
|
155
|
-
|
156
|
-
=end
|
@@ -1,48 +1,12 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Collect
|
4
|
-
#
|
5
|
-
# DESCRIPTION:
|
6
|
-
#
|
7
|
-
# Variations of Enumerable collection and partition methods.
|
8
|
-
#
|
9
|
-
# AUTHORS:
|
10
|
-
#
|
11
|
-
# - Florian Gross
|
12
|
-
# - Thomas Sawyer
|
13
|
-
# - Gavin Sinclair
|
14
|
-
# - Thibaut Barrère
|
15
|
-
# - WhyTheLuckyStiff
|
16
|
-
# - Daniel Sheppard
|
17
|
-
# - Paul Battley
|
18
|
-
#
|
19
|
-
# NOTES:
|
20
|
-
#
|
21
|
-
# - TODO Deprecate #group_by when released for Ruby 1.9.
|
22
|
-
# - TODO Suggest Enumerator's #each_slice use block arity if no parameter is given.
|
23
|
-
|
24
|
-
require 'enumerator' # for each_slice
|
25
|
-
|
26
|
-
#
|
27
1
|
module Enumerable
|
28
2
|
|
29
|
-
# Partition an array into parts of given length.
|
30
|
-
#
|
31
|
-
# CREDIT WhyTheLuckyStiff
|
32
|
-
|
33
|
-
# def / len
|
34
|
-
# inject([]) do |ary, x|
|
35
|
-
# ary << [] if [*ary.last].nitems % len == 0
|
36
|
-
# ary.last << x
|
37
|
-
# ary
|
38
|
-
# end
|
39
|
-
# end
|
40
|
-
|
41
3
|
# Same as #collect but with an iteration counter.
|
42
4
|
#
|
43
5
|
# a = [1,2,3].collect_with_index { |e,i| e*i }
|
44
6
|
# a #=> [0,2,6]
|
45
7
|
#
|
8
|
+
# CREDIT: Gavin Sinclair
|
9
|
+
|
46
10
|
def collect_with_index
|
47
11
|
r = []
|
48
12
|
each_index do |i|
|
@@ -51,6 +15,8 @@ module Enumerable
|
|
51
15
|
r
|
52
16
|
end
|
53
17
|
|
18
|
+
# Alias for collect_with_index.
|
19
|
+
|
54
20
|
alias_method :map_with_index, :collect_with_index
|
55
21
|
|
56
22
|
# Collects/Maps and filters items out in one single step.
|
@@ -64,6 +30,8 @@ module Enumerable
|
|
64
30
|
# end
|
65
31
|
#
|
66
32
|
# Also see Enumerable#collect, Enumerable#find_all.
|
33
|
+
#
|
34
|
+
# CREDIT: Florian Gross
|
67
35
|
|
68
36
|
def filter_collect #:yield:
|
69
37
|
result = []
|
@@ -76,6 +44,8 @@ module Enumerable
|
|
76
44
|
return result
|
77
45
|
end
|
78
46
|
|
47
|
+
# Alias for #filter_collect.
|
48
|
+
|
79
49
|
alias_method :filter_map, :filter_collect
|
80
50
|
|
81
51
|
# Collects/Maps and compacts items in one single step.
|
@@ -86,6 +56,8 @@ module Enumerable
|
|
86
56
|
# persons.compact_collect { |person| person.telephone_no }
|
87
57
|
#
|
88
58
|
# Also see Enumerable#collect, Enumerable#map, Array#compact.
|
59
|
+
#
|
60
|
+
# CREDIT: Florian Gross
|
89
61
|
|
90
62
|
def compact_collect #:yield:
|
91
63
|
filter_collect do |item|
|
@@ -95,146 +67,89 @@ module Enumerable
|
|
95
67
|
end
|
96
68
|
end
|
97
69
|
|
70
|
+
# Alias for #compact_collect.
|
71
|
+
|
98
72
|
alias_method :compact_map, :compact_collect
|
99
73
|
|
100
|
-
#
|
101
|
-
#
|
102
|
-
# some_text.injecting(Hash.new(0)) {|h,l| h[l] += 1}
|
103
|
-
#
|
104
|
-
# vs
|
74
|
+
# Conditional collect.
|
105
75
|
#
|
106
|
-
#
|
76
|
+
# [1,2,3].collect_if { |e| e > 1 }
|
107
77
|
#
|
108
|
-
#
|
78
|
+
# CREDIT: ? Mauricio Fernandez
|
109
79
|
|
110
|
-
def
|
111
|
-
|
112
|
-
|
113
|
-
|
80
|
+
def collect_if(&b)
|
81
|
+
a = map(&b)
|
82
|
+
# to get the same semantics as select{|e| e}
|
83
|
+
a.delete(false)
|
84
|
+
a.compact!
|
85
|
+
a
|
114
86
|
end
|
115
87
|
|
116
|
-
#
|
117
|
-
#def injecting(res, &block)
|
118
|
-
# ([res]*length).zip(to_a).each(&block)
|
119
|
-
# res
|
120
|
-
#end
|
88
|
+
# Alias for #collect_if.
|
121
89
|
|
122
|
-
|
123
|
-
|
124
|
-
#
|
125
|
-
# #=> { 0 => [3], 1 => [1, 4], 2 => [2,5] }
|
126
|
-
#
|
127
|
-
# ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class }
|
128
|
-
# #=> { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
|
129
|
-
#
|
130
|
-
# #group_by is used to group items in a collection by something they
|
131
|
-
# have in common. The common factor is the key in the resulting hash, the
|
132
|
-
# array of like elements is the value.
|
90
|
+
alias_method :map_if, :collect_if
|
91
|
+
|
92
|
+
# Send a message to each element and collect the result.
|
133
93
|
#
|
134
|
-
#
|
94
|
+
# CREDIT: Sean O'Halpin
|
135
95
|
|
136
|
-
def
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
96
|
+
def map_send(meth, *args) #:yield:
|
97
|
+
if block_given?
|
98
|
+
map{|e| yield(e.send(meth, *args))}
|
99
|
+
else
|
100
|
+
map{|e| e.send(meth, *args)}
|
101
|
+
end
|
141
102
|
end
|
142
103
|
|
143
|
-
#
|
144
|
-
#
|
145
|
-
|
146
|
-
|
147
|
-
#
|
148
|
-
#
|
104
|
+
# # Cascade actions on each enumerated element.
|
105
|
+
# #
|
106
|
+
# # [9, 19, 29].cascade :succ, :to_s, :reverse
|
107
|
+
# # => ["01", "02", "03"]
|
108
|
+
# #
|
109
|
+
# # See ruby-talk:199877.
|
149
110
|
#
|
150
|
-
#
|
111
|
+
# def cascade(*methods)
|
112
|
+
# methods.inject(self){ |ary, method| ary.map{ |x| x.send(method)}}
|
113
|
+
# end
|
114
|
+
|
115
|
+
# Say you want to count letters--
|
151
116
|
#
|
152
|
-
#
|
117
|
+
# some_text.inject!(Hash.new(0)) {|h,l| h[l] += 1}
|
153
118
|
#
|
154
|
-
#
|
119
|
+
# vs
|
155
120
|
#
|
156
|
-
#
|
157
|
-
|
158
|
-
def cluster_by(&b)
|
159
|
-
group_by(&b).sort.transpose.pop || [] # group_by(&b).values ?
|
160
|
-
end
|
161
|
-
|
162
|
-
# Split on matching pattern.
|
121
|
+
# some_text.inject(Hash.new(0)) {|h,l| h[l] +=1; h}
|
163
122
|
#
|
164
|
-
#
|
165
|
-
# => [['a1,'b1'],['a2','b2']]
|
123
|
+
# CREDIT: David Black
|
166
124
|
|
167
|
-
def
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
memo
|
172
|
-
end
|
125
|
+
def inject!(s)
|
126
|
+
k = s
|
127
|
+
each { |i| yield(k, i) }
|
128
|
+
k
|
173
129
|
end
|
174
130
|
|
175
|
-
#
|
176
|
-
# given, the the arity if the block is used.
|
131
|
+
# Say you want to count letters--
|
177
132
|
#
|
178
|
-
#
|
179
|
-
# [1,2,3,4].each_by{ |a,b| x << [a,b] }
|
180
|
-
# x #=> [ [1,2], [3,4] ]
|
133
|
+
# some_text.injecting(Hash.new(0)) {|h,l| h[l] += 1}
|
181
134
|
#
|
182
|
-
#
|
183
|
-
# [1,2,3,4,5,6].each_by(3){ |a| x << a }
|
184
|
-
# x #=> [ [1,2,3], [4,5,6] ]
|
185
|
-
|
186
|
-
def each_by(step=nil, &yld)
|
187
|
-
if step
|
188
|
-
each_slice(step,&yld)
|
189
|
-
else
|
190
|
-
step = yld.arity.abs
|
191
|
-
each_slice(step,&yld)
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Iterators over each element pairing.
|
135
|
+
# vs
|
196
136
|
#
|
197
|
-
#
|
137
|
+
# some_text.inject(Hash.new(0)) {|h,l| h[l] +=1; h}
|
198
138
|
#
|
199
|
-
#
|
139
|
+
# This may be deprecated in favor of inject!. We make both
|
140
|
+
# available for now to make sure (via the test of time) that
|
141
|
+
# they are 100% equivalent.
|
200
142
|
#
|
201
|
-
#
|
202
|
-
# c -> d
|
143
|
+
# CREDIT: Louis J Scoras
|
203
144
|
|
204
|
-
def
|
205
|
-
|
206
|
-
|
207
|
-
if i % 2 == 0
|
208
|
-
e1 = e
|
209
|
-
next
|
210
|
-
else
|
211
|
-
yield(e1,e)
|
212
|
-
end
|
145
|
+
def injecting(s)
|
146
|
+
inject(s) do |k, i|
|
147
|
+
yield(k, i); k
|
213
148
|
end
|
214
149
|
end
|
215
150
|
|
216
|
-
# Collect each n items.
|
217
|
-
#
|
218
|
-
# CREDIT Martin DeMello
|
219
|
-
|
220
|
-
def eachn(&block)
|
221
|
-
n = block.arity.abs
|
222
|
-
each_slice(n) {|i| block.call(*i)}
|
223
|
-
end
|
224
|
-
|
225
|
-
# Conditional collect.
|
226
|
-
|
227
|
-
def collect_if(&b)
|
228
|
-
a = map(&b)
|
229
|
-
# to get the same semantics as select{|e| e}
|
230
|
-
a.delete(false)
|
231
|
-
a.compact!
|
232
|
-
a
|
233
|
-
end
|
234
|
-
|
235
|
-
alias_method :map_if, :collect_if
|
236
151
|
|
237
|
-
# DEPRECATED
|
152
|
+
# DEPRECATED:
|
238
153
|
#
|
239
154
|
# # Why the term counter? There may be a change in Ruby 2.0
|
240
155
|
# # to use this word instead of index. Index will
|
@@ -244,9 +159,6 @@ module Enumerable
|
|
244
159
|
# alias_method( :collect_with_counter, :collect_with_index )
|
245
160
|
# alias_method( :map_with_counter, :collect_with_index )
|
246
161
|
#
|
247
|
-
# #
|
248
|
-
# # neccesarily "indexed", as is an Array or Hash.
|
249
|
-
# alias_method :each_with_counter, :each_with_index
|
162
|
+
# #instance_map and #instance_select --too invasive for convenience.
|
250
163
|
|
251
164
|
end
|
252
|
-
|