facets 2.1.3 → 2.2.0
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.
- 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
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
# Like <tt>#map</tt>/<tt>#collect</tt>, but generates a Hash. The block
|
6
|
+
# is expected to return two values: the key and the value for the new hash.
|
7
|
+
#
|
8
|
+
# numbers = (1..3)
|
9
|
+
# squares = numbers.mash { |n| [n, n*n] } # { 1=>1, 2=>4, 3=>9 }
|
10
|
+
# sq_roots = numbers.mash { |n| [n*n, n] } # { 1=>1, 4=>2, 9=>3 }
|
11
|
+
#
|
12
|
+
# CREDIT: Trans
|
13
|
+
# CREDIT: Andrew Dudzik (adudzik)
|
14
|
+
#
|
15
|
+
# NOTE: Would #correlate would be better?
|
16
|
+
|
17
|
+
def mash(&yld)
|
18
|
+
if yld
|
19
|
+
inject({}) do |h, *kv| # Used to be inject({}) do |h,kv|
|
20
|
+
r = *yld[*kv] # The *-op works differnt from to_a on single element hash!!!
|
21
|
+
nk, nv = *r # Used to be nk, nv = *yld[*kv].to_a.flatten
|
22
|
+
h[nk] = nv
|
23
|
+
h
|
24
|
+
end
|
25
|
+
else
|
26
|
+
Enumerator.new(self,:graph) # Used to be Hash[*self.to_a] or Hash[*self.to_a.flatten]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Alias for #mash. This is the original name for this method.
|
31
|
+
alias_method :graph, :mash
|
32
|
+
|
33
|
+
# An alias for #mash that was used for a while b/c of it's similarity to #collect,
|
34
|
+
# but it seems like a misnomer and may well be deprecated in the future.
|
35
|
+
alias_method :collate, :mash
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
class Hash
|
41
|
+
|
42
|
+
# In place version of #mash.
|
43
|
+
#
|
44
|
+
# NOTE: Hash#mash! is only useful for Hash. It is not generally
|
45
|
+
# applicable to Enumerable.
|
46
|
+
|
47
|
+
def mash!(&yld)
|
48
|
+
replace(mash(&yld))
|
49
|
+
end
|
50
|
+
|
51
|
+
# Alias for #mash!. This is the original name for this method.
|
52
|
+
alias_method :graph!, :mash!
|
53
|
+
|
54
|
+
# An alias for #mash! that was used for a while b/c of it's similarity to #collect,
|
55
|
+
# but it seems like a misnomer and may well be deprecated in the future.
|
56
|
+
alias_method :collate!, :mash!
|
57
|
+
|
58
|
+
end
|
@@ -1,22 +1,12 @@
|
|
1
|
-
# TITLE:
|
2
|
-
# Permutations
|
3
|
-
#
|
4
|
-
# DESCRIPTION:
|
5
|
-
# Enumerble permutation extensions.
|
6
|
-
#
|
7
|
-
# AUTHORS:
|
8
|
-
# - Florian Gross
|
9
|
-
|
10
1
|
require 'facets/integer/factorial'
|
11
2
|
|
12
|
-
#
|
13
3
|
module Enumerable
|
14
4
|
|
15
5
|
# Permutation proves the possible orders of an enumerable.
|
16
6
|
# Each is index by a permutation number. The maximum number of
|
17
7
|
# arrangements is the factorial of the size of the array.
|
18
8
|
#
|
19
|
-
#
|
9
|
+
# CREDIT: Florian Gross
|
20
10
|
|
21
11
|
def permutation(number)
|
22
12
|
arr = to_a
|
@@ -24,7 +14,8 @@ module Enumerable
|
|
24
14
|
nextfactor = factor = 1
|
25
15
|
arr.each_with_index {|x,i|
|
26
16
|
case i
|
27
|
-
when 0
|
17
|
+
when 0
|
18
|
+
next
|
28
19
|
else
|
29
20
|
nextfactor = factor * (i+1)
|
30
21
|
out.insert(number % nextfactor / factor, x)
|
@@ -33,11 +24,12 @@ module Enumerable
|
|
33
24
|
}
|
34
25
|
out
|
35
26
|
end
|
36
|
-
|
27
|
+
|
28
|
+
alias_method :permute, :permutation
|
37
29
|
|
38
30
|
# Calculate permutation number.
|
39
31
|
#
|
40
|
-
#
|
32
|
+
# CREDIT: Florian Gross
|
41
33
|
|
42
34
|
def permutation_number(original_array=self.to_a.sort)
|
43
35
|
arr = to_a
|
@@ -66,41 +58,44 @@ module Enumerable
|
|
66
58
|
# cab
|
67
59
|
# cba
|
68
60
|
#
|
69
|
-
#
|
61
|
+
# CREDIT: Daniel Sheppard
|
70
62
|
|
71
63
|
def each_permutation()
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
0.upto(perm_count) do |v|
|
79
|
-
out = arr[0..0]
|
80
|
-
arr.each_with_index {|x,i|
|
81
|
-
case i
|
82
|
-
when 0: next
|
83
|
-
when s: out.insert(v / weights[i-1], x)
|
84
|
-
else out.insert(v % weights[i] / weights[i-1], x)
|
85
|
-
end
|
86
|
-
}
|
64
|
+
pos = Array.new(size) {|i| i}
|
65
|
+
s = (0...size).to_a.reverse
|
66
|
+
out = nil
|
67
|
+
while true
|
68
|
+
out = []
|
69
|
+
pos.each_with_index {|p,i| out.insert(p, self[i]) }
|
87
70
|
yield out
|
71
|
+
break if s.each do |i|
|
72
|
+
break if pos[i] > 0 && pos[i] -= 1
|
73
|
+
pos[i] = i
|
74
|
+
end
|
88
75
|
end
|
89
76
|
end
|
90
77
|
|
91
|
-
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
|
78
|
+
# OLD VERSION
|
79
|
+
# # CREDIT: Florian Gross
|
80
|
+
#
|
81
|
+
# def each_permutation()
|
82
|
+
# arr = to_a
|
83
|
+
# size = arr.size
|
84
|
+
# perm_count = (1...size).inject(0) { |s,x| s + x * x.factorial }
|
85
|
+
# weights = Array.new(size-1) {|i| (i+1).factorial }
|
86
|
+
# s = weights.size
|
87
|
+
# x,i,v,pos = nil
|
88
|
+
# 0.upto(perm_count) do |v|
|
89
|
+
# out = arr[0..0]
|
90
|
+
# arr.each_with_index {|x,i|
|
91
|
+
# case i
|
92
|
+
# when 0: next
|
93
|
+
# when s: out.insert(v / weights[i-1], x)
|
94
|
+
# else out.insert(v % weights[i] / weights[i-1], x)
|
95
|
+
# end
|
96
|
+
# }
|
97
|
+
# yield out
|
98
|
+
# end
|
99
|
+
# end
|
105
100
|
|
106
101
|
end
|
@@ -1,30 +1,10 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Enumerable Statistical Extensions
|
4
|
-
#
|
5
|
-
# FILE:
|
6
|
-
#
|
7
|
-
# statistics.rb
|
8
|
-
#
|
9
|
-
# DESCRIPTION:
|
10
|
-
#
|
11
|
-
# Extensions for statistical information.
|
12
|
-
#
|
13
|
-
# Note that String#entropy has been deprecated.
|
14
|
-
# Use str.chars.entropy instead.
|
15
|
-
#
|
16
|
-
# AUTHORS:
|
17
|
-
#
|
18
|
-
# CREDIT Derek
|
19
|
-
# CREDIT Brian Schröder
|
20
|
-
# CREDIT Florian Gross
|
21
|
-
|
22
|
-
#
|
23
1
|
module Enumerable
|
24
2
|
|
25
3
|
# In Statistics mode is the value that occurs most
|
26
4
|
# frequently in a given set of data.
|
27
5
|
#
|
6
|
+
# CREDIT: Trans
|
7
|
+
|
28
8
|
def mode
|
29
9
|
count = Hash.new(0)
|
30
10
|
each {|x| count[x] += 1 }
|
@@ -34,10 +14,9 @@ module Enumerable
|
|
34
14
|
# Generates a hash mapping each unique symbol in the array
|
35
15
|
# to the relative frequency, i.e. the probablity, of
|
36
16
|
# it appearence.
|
37
|
-
|
38
|
-
#
|
39
|
-
|
40
|
-
#++
|
17
|
+
#
|
18
|
+
# CREDIT: Brian Schröder
|
19
|
+
|
41
20
|
def probability
|
42
21
|
probs = Hash.new(0.0)
|
43
22
|
size = 0.0
|
@@ -64,8 +43,6 @@ module Enumerable
|
|
64
43
|
# probHash
|
65
44
|
# end
|
66
45
|
|
67
|
-
# CREDIT Derek
|
68
|
-
|
69
46
|
# Shannon's entropy for an array - returns the average
|
70
47
|
# bits per symbol required to encode the array.
|
71
48
|
# Lower values mean less "entropy" - i.e. less unique
|
@@ -73,6 +50,8 @@ module Enumerable
|
|
73
50
|
#
|
74
51
|
# %w{ a b c d e e e }.entropy #=>
|
75
52
|
#
|
53
|
+
# CREDIT: Derek
|
54
|
+
|
76
55
|
def entropy
|
77
56
|
arr = to_a
|
78
57
|
probHash = arr.probability
|
@@ -83,12 +62,12 @@ module Enumerable
|
|
83
62
|
h
|
84
63
|
end
|
85
64
|
|
86
|
-
# CREDIT Derek
|
87
|
-
|
88
65
|
# Returns the maximum possible Shannon entropy of the array
|
89
66
|
# with given size assuming that it is an "order-0" source
|
90
67
|
# (each element is selected independently of the next).
|
91
68
|
#
|
69
|
+
# CREDIT: Derek
|
70
|
+
|
92
71
|
def ideal_entropy
|
93
72
|
arr = to_a
|
94
73
|
unitProb = 1.0.to_f / arr.size.to_f
|
@@ -97,9 +76,9 @@ module Enumerable
|
|
97
76
|
|
98
77
|
# Generates a hash mapping each unique symbol in the array
|
99
78
|
# to the absolute frequency it appears.
|
100
|
-
|
101
|
-
#
|
102
|
-
|
79
|
+
#
|
80
|
+
# CREDIT: Brian Schröder
|
81
|
+
|
103
82
|
def frequency
|
104
83
|
#probs = Hash.new(0)
|
105
84
|
#each do |e|
|
@@ -121,9 +100,8 @@ module Enumerable
|
|
121
100
|
# # Returns all persons that share their last name with another person.
|
122
101
|
# persons.collisions { |person| person.last_name }
|
123
102
|
#
|
124
|
-
|
125
|
-
|
126
|
-
#++
|
103
|
+
# CREDIT: Florian Gross
|
104
|
+
|
127
105
|
def commonality( &block )
|
128
106
|
had_no_block = !block
|
129
107
|
block ||= lambda { |item| item }
|
@@ -152,6 +130,8 @@ end
|
|
152
130
|
|
153
131
|
# class String
|
154
132
|
#
|
133
|
+
# NOTE: String#entropy has been deprecated.
|
134
|
+
# Use str.chars.entropy instead.
|
155
135
|
# def entropy
|
156
136
|
# split(//).entropy
|
157
137
|
# end
|
@@ -162,57 +142,3 @@ end
|
|
162
142
|
# # end
|
163
143
|
#
|
164
144
|
# end
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
# _____ _
|
169
|
-
# |_ _|__ ___| |_
|
170
|
-
# | |/ _ \/ __| __|
|
171
|
-
# | | __/\__ \ |_
|
172
|
-
# |_|\___||___/\__|
|
173
|
-
#
|
174
|
-
=begin test
|
175
|
-
|
176
|
-
require 'test/unit'
|
177
|
-
|
178
|
-
class TestEnumerable < Test::Unit::TestCase
|
179
|
-
|
180
|
-
def test_entropy
|
181
|
-
assert_equal( 1.0, %w{ a b }.entropy )
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_ideal_entropy
|
185
|
-
assert_equal( 1.0, %w{ a b }.ideal_entropy )
|
186
|
-
assert_equal( 2.0, %w{ a a b b }.ideal_entropy )
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_probability
|
190
|
-
assert_equal( {'a'=>0.5,'b'=>0.5}, %w{a b}.probability )
|
191
|
-
assert_equal( {'tom'=>0.5,'boy'=>0.5}, %w{tom boy}.probability )
|
192
|
-
end
|
193
|
-
|
194
|
-
def test_frequency
|
195
|
-
assert_equal( {'a'=>1,'b'=>1}, %w{a b}.frequency )
|
196
|
-
assert_equal( {'tom'=>1,'boy'=>1}, %w{tom boy}.frequency )
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_commonality
|
200
|
-
a = [1,2,2,3,3,3]
|
201
|
-
r = { 2 => [2,2], 3 => [3,3,3] }
|
202
|
-
assert_equal( r, a.commonality )
|
203
|
-
a = [1,2,2,3,3,3]
|
204
|
-
r = {false=>[1, 2, 2], true=>[3, 3, 3]}
|
205
|
-
assert_equal( r, a.commonality { |x| x > 2 } )
|
206
|
-
end
|
207
|
-
|
208
|
-
# def test_collisions
|
209
|
-
# a = [1,2,2,3,3,3]
|
210
|
-
# assert_equal( [2,3], a.collisions )
|
211
|
-
# a = [1,2,2,3,3,3]
|
212
|
-
# r = {false=>[1, 2, 2], true=>[3, 3, 3]}
|
213
|
-
# assert_equal( r, a.collisions { |x| x > 2 } )
|
214
|
-
# end
|
215
|
-
|
216
|
-
end
|
217
|
-
|
218
|
-
=end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
4
|
+
# #group_by is used to group items in a collection by something they
|
5
|
+
# have in common. The common factor is the key in the resulting hash, the
|
6
|
+
# array of like elements is the value.
|
7
|
+
#
|
8
|
+
# (1..5).group_by { |n| n % 3 }
|
9
|
+
# #=> { 0 => [3], 1 => [1, 4], 2 => [2,5] }
|
10
|
+
#
|
11
|
+
# ["I had", 1, "dollar and", 50, "cents"].group_by { |e| e.class }
|
12
|
+
# #=> { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
|
13
|
+
#
|
14
|
+
# CREDIT: Erik Veenstra
|
15
|
+
#
|
16
|
+
# TODO:
|
17
|
+
# - Deprecate #group_by when released for Ruby 1.9.
|
18
|
+
|
19
|
+
def group_by #:yield:
|
20
|
+
#h = k = e = nil
|
21
|
+
r = Hash.new
|
22
|
+
each{ |e| (r[yield(e)] ||= []) << e }
|
23
|
+
r
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# This alias is the original Facets name for the method, but Ruby 1.9 has
|
28
|
+
# adopted #group_by as the name, so eventually #partition_by will be deprecated.
|
29
|
+
|
30
|
+
alias_method :partition_by, :group_by
|
31
|
+
|
32
|
+
# Split on matching pattern. Unlike #divide this does not include matching elements.
|
33
|
+
#
|
34
|
+
# ['a1','a2','b1','a3','b2','a4'].split(/^b/)
|
35
|
+
# => [['a1','a2'],['a3'],['a4']]
|
36
|
+
#
|
37
|
+
# CREDIT: Trans
|
38
|
+
|
39
|
+
def split(pattern)
|
40
|
+
memo = []
|
41
|
+
each do |obj|
|
42
|
+
if pattern === obj
|
43
|
+
memo.push []
|
44
|
+
else
|
45
|
+
memo.last << obj
|
46
|
+
end
|
47
|
+
end
|
48
|
+
memo
|
49
|
+
end
|
50
|
+
|
51
|
+
# Divide on matching pattern.
|
52
|
+
#
|
53
|
+
# ['a1','b1','a2','b2'].divide(/^a/)
|
54
|
+
# => [['a1,'b1'],['a2','b2']]
|
55
|
+
#
|
56
|
+
# CREDIT: Trans
|
57
|
+
|
58
|
+
def divide(pattern)
|
59
|
+
memo = []
|
60
|
+
each do |obj|
|
61
|
+
memo.push [] if pattern === obj
|
62
|
+
memo.last << obj
|
63
|
+
end
|
64
|
+
memo
|
65
|
+
end
|
66
|
+
|
67
|
+
# Similar to #group_by but returns an array of the groups.
|
68
|
+
# Returned elements are sorted by block.
|
69
|
+
#
|
70
|
+
# %w{this is a test}.cluster_by {|x| x[0]}
|
71
|
+
#
|
72
|
+
# _produces_
|
73
|
+
#
|
74
|
+
# [ ['a'], ['is'], ['this', 'test'] ]
|
75
|
+
#
|
76
|
+
# CREDIT Erik Veenstra
|
77
|
+
|
78
|
+
def cluster_by(&b)
|
79
|
+
group_by(&b).sort.transpose.pop || [] # group_by(&b).values ?
|
80
|
+
end
|
81
|
+
|
82
|
+
# Modulate. Divide an array into groups by modulo of the index.
|
83
|
+
#
|
84
|
+
# [2,4,6,8].modulate(2) #=> [[2,6],[4,8]]
|
85
|
+
#
|
86
|
+
# CREDIT: Trans
|
87
|
+
#
|
88
|
+
# NOTE: Would this be better named 'collate'?
|
89
|
+
|
90
|
+
def modulate(modulo)
|
91
|
+
return to_a if modulo == 1
|
92
|
+
raise ArgumentError, 'bad modulo' if size % modulo != 0
|
93
|
+
r = Array.new(modulo, [])
|
94
|
+
(0...size).each do |i|
|
95
|
+
r[i % level] += [self[i]]
|
96
|
+
end
|
97
|
+
r
|
98
|
+
end
|
99
|
+
|
100
|
+
# DEPRECATED -- Use 'each_slice(n).to_a' as of 1.9.
|
101
|
+
# Partition an array into parts of given length.
|
102
|
+
#
|
103
|
+
# CREDIT WhyTheLuckyStiff
|
104
|
+
#
|
105
|
+
# def / len
|
106
|
+
# inject([]) do |ary, x|
|
107
|
+
# ary << [] if [*ary.last].nitems % len == 0
|
108
|
+
# ary.last << x
|
109
|
+
# ary
|
110
|
+
# end
|
111
|
+
# end
|
112
|
+
|
113
|
+
end
|