facets 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.index +11 -7
- data/CONTRIBUTING.md +112 -0
- data/HISTORY.md +38 -0
- data/{README.rdoc → README.md} +59 -59
- data/demo/core/array/after.md +13 -0
- data/demo/core/array/arrange.md +17 -0
- data/demo/core/array/before.md +14 -0
- data/demo/core/array/collapse.md +15 -0
- data/demo/core/array/collisions.md +30 -0
- data/demo/core/array/commonality.md +31 -0
- data/demo/core/array/conjoin.md +47 -0
- data/demo/core/array/contains.md +9 -0
- data/demo/core/array/delete_unless.md +13 -0
- data/demo/core/array/delete_values.md +10 -0
- data/demo/core/array/delete_values_at.md +15 -0
- data/demo/core/array/divide.md +14 -0
- data/demo/core/array/duplicates.md +14 -0
- data/demo/core/array/each_overlap.md +12 -0
- data/demo/core/array/each_pair.md +13 -0
- data/demo/core/array/each_value.md +12 -0
- data/demo/core/array/entropy.md +13 -0
- data/demo/core/array/extract_options.md +13 -0
- data/demo/core/array/first.md +11 -0
- data/demo/core/array/from.md +11 -0
- data/demo/core/array/ideal_entropy.md +11 -0
- data/demo/core/array/intersection.md +11 -0
- data/demo/core/array/last.md +11 -0
- data/demo/core/array/median.md +23 -0
- data/demo/core/array/merge.md +15 -0
- data/demo/core/array/missing.md +10 -0
- data/demo/core/array/mode.md +9 -0
- data/demo/core/array/nonuniq.md +16 -0
- data/demo/core/array/not_empty.md +8 -0
- data/demo/core/array/occur.md +18 -0
- data/demo/core/array/occurrence.md +13 -0
- data/demo/core/array/only.md +9 -0
- data/demo/core/array/op_pow.md +10 -0
- data/demo/core/array/pad.md +28 -0
- data/demo/core/array/peek.md +10 -0
- data/demo/core/array/poke.md +15 -0
- data/demo/core/array/probability +8 -0
- data/demo/core/array/pull.md +10 -0
- data/demo/core/array/recurse.md +10 -0
- data/demo/core/array/recursively.md +20 -0
- data/demo/core/array/reject_values.md +10 -0
- data/demo/core/array/splice.md +14 -0
- data/demo/core/array/split.md +13 -0
- data/demo/core/array/squeeze.md +14 -0
- data/demo/core/array/step.md +16 -0
- data/demo/core/array/store.md +9 -0
- data/demo/core/array/thru.md +12 -0
- data/demo/core/array/to_h.md +11 -0
- data/demo/core/array/traverse.md +20 -0
- data/demo/core/array/uniq_by.md +16 -0
- data/demo/core/array/unique_permutation.md +14 -0
- data/demo/core/array/zip.md +8 -0
- data/demo/core/binding/caller.md +57 -0
- data/demo/core/binding/defined.md +10 -0
- data/demo/core/binding/opvars.md +16 -0
- data/demo/core/binding/self.md +6 -0
- data/demo/core/class/descendants.md +13 -0
- data/demo/core/class/to_proc.md +11 -0
- data/demo/core/comparable/at_least.md +11 -0
- data/demo/core/comparable/at_most.md +11 -0
- data/demo/core/comparable/bound.md +13 -0
- data/demo/core/comparable/cap.md +11 -0
- data/demo/core/comparable/clip.md +29 -0
- data/demo/core/comparable/cmp.md +18 -0
- data/demo/core/comparable/op_get.md +32 -0
- data/demo/core/dir/ascend.md +24 -0
- data/demo/core/dir/descend.md +16 -0
- data/demo/core/dir/multiglob.md +41 -0
- data/demo/core/dir/parent.md +6 -0
- data/demo/core/dir/recurse.md +41 -0
- data/demo/core/exception/detail.md +19 -0
- data/demo/core/{facets.rdoc → facets.md} +1 -1
- data/demo/core/filetest/root.md +13 -0
- data/demo/core/hash/recursively.md +21 -0
- data/demo/core/indexable.md +95 -0
- data/demo/core/integer/bitmask.md +46 -0
- data/demo/core/integer/{factorial.rdoc → factorial.md} +1 -1
- data/demo/core/integer/{length.rdoc → length.md} +1 -1
- data/demo/core/integer/{multiple.rdoc → multiple.md} +3 -1
- data/demo/core/integer/of.md +11 -0
- data/demo/core/integer/{ordinal.rdoc → ordinal.md} +3 -1
- data/demo/core/integer/{of.rdoc → times_collect.md} +2 -7
- data/demo/core/matchdata/match.md +7 -0
- data/demo/core/matchdata/matchset.md +7 -0
- data/demo/core/matchdata/matchtree.md +10 -0
- data/demo/core/module/cattr.md +43 -0
- data/demo/core/module/class_extend.md +64 -0
- data/demo/core/module/{class_inheritor.rdoc → class_inheritor.md} +2 -2
- data/demo/core/module/{copy_inheritor.rdoc → copy_inheritor.md} +4 -4
- data/demo/core/module/include_as.md +31 -0
- data/demo/core/module/method_space.md +24 -0
- data/demo/core/string/acronym.md +9 -0
- data/demo/core/string/{align.rdoc → align.md} +10 -10
- data/demo/core/string/ascii_only.md +37 -0
- data/demo/core/string/{bracket.rdoc → bracket.md} +10 -10
- data/demo/core/string/camelcase.md +47 -0
- data/demo/core/string/{capitalized.rdoc → capitalized.md} +9 -9
- data/demo/core/string/characters.md +11 -0
- data/demo/core/string/{cleanlines.rdoc → cleanlines.md} +3 -3
- data/demo/core/string/cleave.md +49 -0
- data/demo/core/string/compress_lines.md +22 -0
- data/demo/core/string/crypt.md +15 -0
- data/demo/core/string/divide.md +9 -0
- data/demo/core/string/each_word.md +11 -0
- data/demo/core/string/edit_distance.md +15 -0
- data/demo/core/string/exclude.md +15 -0
- data/demo/core/string/expand_tabs.md +69 -0
- data/demo/core/string/fold.md +14 -0
- data/demo/core/string/indent.md +65 -0
- data/demo/core/string/index_all.md +14 -0
- data/demo/core/string/interpolate.md +8 -0
- data/demo/core/string/lchomp.md +14 -0
- data/demo/core/string/line_wrap.md +7 -0
- data/demo/core/string/lowercase.md +12 -0
- data/demo/core/string/methodize.md +22 -0
- data/demo/core/string/modulize.md +22 -0
- data/demo/core/string/mscan.md +10 -0
- data/demo/core/string/natcmp.md +10 -0
- data/demo/core/string/nchar.md +9 -0
- data/demo/core/string/{newlines.rdoc → newlines.md} +3 -3
- data/demo/core/string/number.md +27 -0
- data/demo/core/string/op_div.md +10 -0
- data/demo/core/string/op_sub.md +7 -0
- data/demo/core/string/pathize.md +22 -0
- data/demo/core/string/quote.md +20 -0
- data/demo/core/string/random.md +25 -0
- data/demo/core/string/range.md +19 -0
- data/demo/core/string/remove.md +33 -0
- data/demo/core/string/rewrite.md +9 -0
- data/demo/core/string/shatter.md +9 -0
- data/demo/core/string/similarity.md +12 -0
- data/demo/core/string/snakecase.md +14 -0
- data/demo/core/string/splice.md +28 -0
- data/demo/core/string/squish.md +27 -0
- data/demo/core/string/store.md +10 -0
- data/demo/core/string/subtract.md +8 -0
- data/demo/core/string/titlecase.md +8 -0
- data/demo/core/string/to_re.md +22 -0
- data/demo/core/string/{margin.rdoc → trim.md} +14 -14
- data/demo/core/string/unbracket.md +24 -0
- data/demo/core/string/underscore.md +17 -0
- data/demo/core/string/unfold.md +9 -0
- data/demo/core/string/unindent.md +65 -0
- data/demo/core/string/unquote.md +6 -0
- data/demo/core/string/uppercase.md +15 -0
- data/demo/core/string/variablize.md +7 -0
- data/demo/core/string/word_wrap.md +54 -0
- data/demo/core/string/words.md +13 -0
- data/demo/core/string/xor.md +8 -0
- data/lib/core/facets.yml +11 -7
- data/lib/core/facets/array.rb +1 -0
- data/lib/core/facets/array/average.rb +16 -0
- data/lib/core/facets/array/collapse.rb +4 -0
- data/lib/core/facets/array/delete_values_at.rb +1 -0
- data/lib/core/facets/array/divide.rb +7 -0
- data/lib/core/facets/array/duplicates.rb +2 -0
- data/lib/core/facets/array/each_pair.rb +1 -4
- data/lib/core/facets/array/first.rb +1 -0
- data/lib/core/facets/array/from.rb +11 -16
- data/lib/core/facets/array/intersection.rb +1 -1
- data/lib/core/facets/array/last.rb +1 -0
- data/lib/core/facets/array/mode.rb +2 -2
- data/lib/core/facets/array/occurrence.rb +4 -3
- data/lib/core/facets/array/op_pow.rb +0 -2
- data/lib/core/facets/array/probability.rb +1 -1
- data/lib/core/facets/array/recursively.rb +2 -2
- data/lib/core/facets/array/splice.rb +1 -1
- data/lib/core/facets/array/standard_deviation.rb +23 -0
- data/lib/core/facets/array/to_h.rb +0 -3
- data/lib/core/facets/array/uniq_by.rb +4 -0
- data/lib/core/facets/array/variance.rb +20 -0
- data/lib/core/facets/binding.rb +0 -1
- data/lib/core/facets/enumerable/cluster.rb +6 -4
- data/lib/core/facets/enumerable/hashify.rb +27 -31
- data/lib/core/facets/enumerable/sum.rb +12 -2
- data/lib/core/facets/file/atomic_open.rb +1 -1
- data/lib/core/facets/hash/except.rb +13 -1
- data/lib/core/facets/indexable.rb +1 -1
- data/lib/core/facets/kernel/ergo.rb +3 -3
- data/lib/core/facets/kernel/like.rb +1 -1
- data/lib/core/facets/matchdata/matchtree.rb +2 -0
- data/lib/core/facets/numeric/delimit.rb +20 -0
- data/lib/core/facets/range/nudge.rb +29 -0
- data/lib/core/facets/string.rb +0 -2
- data/lib/core/facets/string/indent.rb +0 -8
- data/lib/core/facets/string/margin.rb +0 -4
- data/lib/core/facets/string/random.rb +13 -3
- data/lib/core/facets/string/similarity.rb +7 -6
- data/lib/standard/facets/enumargs.rb +5 -0
- data/lib/standard/facets/math/percentile.rb +1 -1
- data/lib/standard/facets/net/http.rb +33 -14
- data/lib/standard/facets/timer.rb +3 -3
- data/test/core/array/test_average.rb +16 -0
- data/test/core/array/test_before.rb +21 -0
- data/test/core/array/test_collapse.rb +10 -1
- data/test/core/array/test_delete_unless.rb +17 -0
- data/test/core/array/test_delete_values.rb +31 -0
- data/test/core/array/test_divide.rb +10 -0
- data/test/core/array/test_each_pair.rb +16 -0
- data/test/core/array/test_each_value.rb +16 -0
- data/test/core/array/test_from.rb +42 -0
- data/test/core/array/test_op_pow.rb +13 -0
- data/test/core/array/test_standard_deviation.rb +18 -0
- data/test/core/array/test_to_h.rb +14 -0
- data/test/core/array/test_uniq_by.rb +7 -0
- data/test/core/array/test_variance.rb +17 -0
- data/test/core/array/test_zip.rb +13 -0
- data/test/core/dir/test_each_child.rb +1 -1
- data/test/core/dir/test_multiglob.rb +1 -1
- data/test/core/enumerable/test_cluster.rb +36 -0
- data/test/core/enumerable/test_hashify.rb +113 -0
- data/test/core/hash/test_expect.rb +11 -1
- data/test/core/kernel/test_ergo.rb +7 -5
- data/test/core/{object/test_replace.rb → kernel/test_instance_replace.rb} +6 -28
- data/test/core/numeric/test_delimit.rb +68 -0
- data/test/core/numeric/{test_round.rb → test_round_to.rb} +27 -27
- data/test/core/range/test_nudge.rb +32 -0
- data/test/core/range/{test_to_r.rb → test_to_rng.rb} +3 -3
- data/test/core/string/test_acronym.rb +14 -0
- data/test/core/string/test_ascii_only.rb +21 -0
- data/test/core/string/test_compress_lines.rb +9 -0
- data/test/core/string/test_crypt.rb +17 -0
- data/test/core/string/test_index_all.rb +4 -0
- data/test/core/string/test_random.rb +25 -0
- data/test/core/string/test_remove.rb +14 -0
- data/test/core/string/test_similarity.rb +9 -1
- data/test/core/struct/test_replace.rb +18 -0
- data/test/standard/test_enumargs.rb +26 -10
- data/test/standard/test_timer.rb +1 -1
- metadata +634 -588
- data/CONTRIBUTE.md +0 -78
- data/demo/core/array/combination.rdoc +0 -19
- data/demo/core/array/delete.rdoc +0 -23
- data/demo/core/array/index.rdoc +0 -14
- data/demo/core/array/merge.rdoc +0 -15
- data/demo/core/array/only.rdoc +0 -9
- data/demo/core/array/pad.rdoc +0 -28
- data/demo/core/array/permutation.rdoc +0 -10
- data/demo/core/array/product.rdoc +0 -26
- data/demo/core/array/recursively.rdoc +0 -16
- data/demo/core/array/rotate.rdoc +0 -30
- data/demo/core/array/select.rdoc +0 -8
- data/demo/core/array/uniq_by.rdoc +0 -9
- data/demo/core/binding/caller.rdoc +0 -57
- data/demo/core/binding/defined.rdoc +0 -10
- data/demo/core/binding/eval.rdoc +0 -8
- data/demo/core/binding/local_variables.rdoc +0 -26
- data/demo/core/binding/opvars.rdoc +0 -16
- data/demo/core/binding/self.rdoc +0 -6
- data/demo/core/class/descendants.rdoc +0 -10
- data/demo/core/class/to_proc.rdoc +0 -8
- data/demo/core/comparable/cap.rdoc +0 -20
- data/demo/core/comparable/clip.rdoc +0 -37
- data/demo/core/comparable/cmp.rdoc +0 -18
- data/demo/core/comparable/op_get.rdoc +0 -29
- data/demo/core/dir/ascend.rdoc +0 -24
- data/demo/core/dir/descend.rdoc +0 -14
- data/demo/core/dir/multiglob.rdoc +0 -41
- data/demo/core/dir/parent.rdoc +0 -6
- data/demo/core/dir/recurse.rdoc +0 -41
- data/demo/core/exception/detail.rdoc +0 -12
- data/demo/core/filetest/root.rdoc +0 -6
- data/demo/core/hash/recursively.rdoc +0 -21
- data/demo/core/indexable.rdoc +0 -95
- data/demo/core/integer/bitmask.rdoc +0 -46
- data/demo/core/integer/even.rdoc +0 -12
- data/demo/core/integer/odd.rdoc +0 -12
- data/demo/core/matchdata/match.rdoc +0 -7
- data/demo/core/matchdata/matchset.rdoc +0 -15
- data/demo/core/module/cattr.rdoc +0 -43
- data/demo/core/module/class_extend.rdoc +0 -64
- data/demo/core/module/method_space.rdoc +0 -55
- data/demo/core/string/bytes.rdoc +0 -8
- data/demo/core/string/camelcase.rdoc +0 -47
- data/demo/core/string/characters.rdoc +0 -11
- data/demo/core/string/cleave.rdoc +0 -49
- data/demo/core/string/divide.rdoc +0 -9
- data/demo/core/string/each_char.rdoc +0 -9
- data/demo/core/string/each_word.rdoc +0 -9
- data/demo/core/string/end_with.rdoc +0 -6
- data/demo/core/string/expand_tabs.rdoc +0 -69
- data/demo/core/string/fold.rdoc +0 -14
- data/demo/core/string/indent.rdoc +0 -65
- data/demo/core/string/interpolate.rdoc +0 -7
- data/demo/core/string/lchomp.rdoc +0 -14
- data/demo/core/string/line_wrap.rdoc +0 -7
- data/demo/core/string/lines.rdoc +0 -8
- data/demo/core/string/methodize.rdoc +0 -22
- data/demo/core/string/modulize.rdoc +0 -22
- data/demo/core/string/mscan.rdoc +0 -10
- data/demo/core/string/natcmp.rdoc +0 -10
- data/demo/core/string/nchar.rdoc +0 -9
- data/demo/core/string/op_sub.rdoc +0 -7
- data/demo/core/string/pathize.rdoc +0 -22
- data/demo/core/string/quote.rdoc +0 -20
- data/demo/core/string/range.rdoc +0 -19
- data/demo/core/string/rewrite.rdoc +0 -9
- data/demo/core/string/shatter.rdoc +0 -9
- data/demo/core/string/snakecase.rdoc +0 -10
- data/demo/core/string/splice.rdoc +0 -28
- data/demo/core/string/start_with.rdoc +0 -7
- data/demo/core/string/subtract.rdoc +0 -8
- data/demo/core/string/tab.rdoc +0 -16
- data/demo/core/string/tabto.rdoc +0 -29
- data/demo/core/string/titlecase.rdoc +0 -8
- data/demo/core/string/to_re.rdoc +0 -22
- data/demo/core/string/unfold.rdoc +0 -9
- data/demo/core/string/unindent.rdoc +0 -65
- data/demo/core/string/unquote.rdoc +0 -6
- data/demo/core/string/variablize.rdoc +0 -7
- data/demo/core/string/word_wrap.rdoc +0 -54
- data/demo/core/string/words.rdoc +0 -13
- data/demo/core/string/xor.rdoc +0 -8
- data/lib/core/facets/binding/local_variables.rb +0 -16
- data/lib/core/facets/string/tab.rb +0 -2
- data/lib/core/facets/string/tabto.rb +0 -2
- data/test/core/array/test_after.rb +0 -27
- data/test/core/array/test_combination.rb +0 -25
- data/test/core/array/test_index.rb +0 -20
- data/test/core/array/test_permutation.rb +0 -20
- data/test/core/array/test_product.rb +0 -64
- data/test/core/array/test_rotate.rb +0 -38
- data/test/core/array/test_select.rb +0 -16
- data/test/core/binding/test_eval.rb +0 -15
- data/test/core/binding/test_local_variables.rb +0 -36
- data/test/core/enumerable/test_cluster_by.rb +0 -30
- data/test/core/enumerable/test_count.rb +0 -32
- data/test/core/enumerable/test_each_with_object.rb +0 -18
- data/test/core/enumerable/test_group_by.rb +0 -36
- data/test/core/enumerable/test_none.rb +0 -29
- data/test/core/enumerable/test_one.rb +0 -34
- data/test/core/enumerable/test_take.rb +0 -15
- data/test/core/hash/test_select.rb +0 -44
- data/test/core/hash/test_url_params.rb +0 -26
- data/test/core/integer/test_even.rb +0 -24
- data/test/core/integer/test_odd.rb +0 -24
- data/test/core/kernel/test_require_relative.rb +0 -12
- data/test/core/nilclass/test_to_f.rb +0 -13
- data/test/core/proc/test_curry.rb +0 -38
- data/test/core/string/test_bytes.rb +0 -13
- data/test/core/string/test_each_char.rb +0 -17
- data/test/core/string/test_end_with.rb +0 -24
- data/test/core/string/test_lines.rb +0 -15
- data/test/core/string/test_start_with.rb +0 -24
- data/test/core/string/test_tab.rb +0 -16
- data/test/core/symbol/test_to_proc.rb +0 -35
- data/test/core/to_hash_tc.rb +0 -210
- data/test/core/unboundmethod/test_name.rb +0 -19
@@ -0,0 +1 @@
|
|
1
|
+
require 'facets/array/indexable'
|
@@ -1,32 +1,27 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
|
-
# Returns
|
3
|
+
# Returns elements from `index` until the end.
|
4
4
|
#
|
5
|
-
# %w{W o r l d}.from(3) #=>
|
5
|
+
# %w{W o r l d}.from(3) #=> ["l", "d"]
|
6
|
+
# %w{W o r l d}.from(9) #=> []
|
6
7
|
#
|
7
|
-
def from(
|
8
|
-
return
|
9
|
-
self[
|
8
|
+
def from(index)
|
9
|
+
return [] if index >= size
|
10
|
+
self[index..-1]
|
10
11
|
end unless method_defined?(:from)
|
11
12
|
|
12
13
|
# Fetch values from a start index thru an end index.
|
13
14
|
#
|
14
|
-
# [1,2,3,4,5].thru(0,2) #=> [1,2,3]
|
15
|
-
# [1,2,3,4,5].thru(2,4) #=> [3,4,5]
|
16
|
-
#
|
17
15
|
# [1,2,3,4,5].thru(2) #=> [1,2,3]
|
18
16
|
# [1,2,3,4,5].thru(4) #=> [1,2,3,4,5]
|
19
17
|
#
|
18
|
+
# [1,2,3,4,5].thru(0,2) #=> [1,2,3]
|
19
|
+
# [1,2,3,4,5].thru(2,4) #=> [3,4,5]
|
20
|
+
#
|
20
21
|
def thru(from, to=nil)
|
21
22
|
from, to = 0, from unless to
|
22
|
-
|
23
|
-
|
24
|
-
i = from
|
25
|
-
while i <= to
|
26
|
-
a << self[i]
|
27
|
-
i += 1
|
28
|
-
end
|
29
|
-
a
|
23
|
+
return [] if from >= size
|
24
|
+
self[from..to]
|
30
25
|
end unless method_defined?(:thru)
|
31
26
|
|
32
27
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'facets/array/indexable'
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
|
-
# In Statistics mode is the value that occurs most frequently
|
3
|
+
# In Statistics. mode is the value that occurs most frequently
|
4
4
|
# in a given set of data. This method returns an array in case
|
5
|
-
#
|
5
|
+
# there is a tie.
|
6
6
|
#
|
7
7
|
# [:a, :b, :c, :b, :d].mode #=> [:b]
|
8
8
|
# [:a, :b, :c, :b, :a].mode #=> [:a, :b]
|
@@ -1,13 +1,14 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
|
-
# Create a
|
3
|
+
# Create a hash of each uniq element of the array
|
4
|
+
# and how many time each appears.
|
4
5
|
#
|
5
6
|
# Examples
|
6
7
|
#
|
7
8
|
# [:a,:a,:b,:c,:c,:c].occurrence
|
8
|
-
# #=> { :a => 2, :b => 1, :c => 3 }
|
9
|
+
# #=> { :a => 2, :b => 1, :c => 3 }
|
9
10
|
#
|
10
|
-
# [2,2,3,4,4,4].
|
11
|
+
# [2,2,3,4,4,4].occurrence{|i| i % 2}
|
11
12
|
# #=> { 0 => 5, 1 => 1 }
|
12
13
|
#
|
13
14
|
def occurrence
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
3
|
# Generates a hash mapping each unique element in the array to the
|
4
|
-
# relative frequency, i.e. the
|
4
|
+
# relative frequency, i.e. the probability, of it appearance.
|
5
5
|
#
|
6
6
|
# [:a, :b, :c, :c].probability #=> {:a=> 0.25, :b=>0.25, :c=>0.5}
|
7
7
|
#
|
@@ -6,11 +6,11 @@ class Array
|
|
6
6
|
# Apply a method to array, and recursively apply that method
|
7
7
|
# to each sub-array or given +types+.
|
8
8
|
#
|
9
|
-
# By default the sub-types are passed
|
9
|
+
# By default the sub-types are passed through unaffected. Passing
|
10
10
|
# a block to #recursively can be used to change this.
|
11
11
|
#
|
12
12
|
# types - List of class types to recurse. [Array<Class>]
|
13
|
-
# block - Optional filter procedure to apply on each recursion.
|
13
|
+
# block - Optional filter procedure to apply on each recursion.
|
14
14
|
#
|
15
15
|
# Examples
|
16
16
|
#
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'facets/array/variance'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
|
5
|
+
# Calculate the standard_deviation of an array of numbers
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
#
|
9
|
+
# [].standard_deviation #=> nil
|
10
|
+
# [1, 2, 3].standard_deviation #=> 0.816496580927726
|
11
|
+
# [96, 35, 72, 30, 75, 33, 68, 13, 49, 71].standard_deviation #=> 24.69331893448104
|
12
|
+
# [36, -67, -17, 85, -46, -64, -23, -13, 89, -47].standard_deviation #=> 54.67183918618432
|
13
|
+
# [60.7829, 31.2622, 20.626, 78.8907, 61.5328].standard_deviation #=> 21.428815505053002
|
14
|
+
#
|
15
|
+
def standard_deviation
|
16
|
+
return nil if empty?
|
17
|
+
Math.sqrt(variance)
|
18
|
+
end
|
19
|
+
|
20
|
+
alias sd standard_deviation
|
21
|
+
end
|
22
|
+
|
23
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'facets/array/average'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
|
5
|
+
# Calculate the variance of an array of numbers
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
#
|
9
|
+
# [].variance #=> nil
|
10
|
+
# [1, 2, 3].variance #=> 0.6666666666666666
|
11
|
+
# [96, 35, 72, 30, 75, 33, 68, 13, 49, 71].variance #=> 609.76
|
12
|
+
# [36, -67, -17, 85, -46, -64, -23, -13, 89, -47].variance #=> 2989.0099999999993
|
13
|
+
# [60.7829, 31.2622, 20.626, 78.8907, 61.5328].variance #=> 459.1941339495999
|
14
|
+
#
|
15
|
+
def variance
|
16
|
+
avg = average
|
17
|
+
map {|n| (n - avg) ** 2 }.average
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/lib/core/facets/binding.rb
CHANGED
@@ -2,7 +2,6 @@ require_relative 'binding/caller.rb'
|
|
2
2
|
require_relative 'binding/callstack.rb'
|
3
3
|
require_relative 'binding/call_stack.rb'
|
4
4
|
require_relative 'binding/defined.rb'
|
5
|
-
#require_relative 'binding/local_variables.rb' # uncommon
|
6
5
|
require_relative 'binding/op.rb'
|
7
6
|
require_relative 'binding/op_get.rb'
|
8
7
|
require_relative 'binding/op_set.rb'
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module Enumerable
|
2
2
|
|
3
|
-
# Clusters together adjacent elements into a list of
|
4
|
-
# sub-arrays.
|
3
|
+
# Clusters together adjacent elements into a list of sub-arrays.
|
5
4
|
#
|
6
|
-
#
|
7
|
-
#
|
5
|
+
# [2,2,2,3,3,4,2,2,1].cluster{ |x| x }
|
6
|
+
# => [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
|
7
|
+
#
|
8
|
+
# ["dog", "duck", "cat", "dude"].cluster{ |x| x[0] }
|
9
|
+
# => [["dog", "duck"], ["cat"], ["dude"]]
|
8
10
|
#
|
9
11
|
# @author Oleg K
|
10
12
|
|
@@ -4,13 +4,13 @@ require 'facets/enumerable/value_by'
|
|
4
4
|
|
5
5
|
module Enumerable
|
6
6
|
|
7
|
-
# The hashify methods is a higher-order message used to
|
7
|
+
# The hashify methods is a higher-order message used to
|
8
8
|
# convert an enumerable object into a hash. Converting an
|
9
9
|
# enumerable object into a hash is not a one-to-one conversion,
|
10
10
|
# for this reason #hashify is used to provide variant approches
|
11
11
|
# for the conversion most suited to the use case at hand.
|
12
12
|
# Here are some (but not a complete set of) examples.
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# If the enumerable is a collection of perfect pairs, like that
|
15
15
|
# which Hash#to_a generates, then #assoc can be used.
|
16
16
|
#
|
@@ -23,13 +23,13 @@ module Enumerable
|
|
23
23
|
# a = [ [:a,1,2], [:b,2], [:c], [:d] ]
|
24
24
|
# a.hashify.concat #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
25
25
|
#
|
26
|
-
# If the array
|
26
|
+
# If the array contains objects other then arrays then
|
27
27
|
# the #splat method might do the trick.
|
28
28
|
#
|
29
29
|
# a = [ [:a,1,2], 2, :b, [:c,3], 9 ]
|
30
|
-
# a.hashify.splat #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
30
|
+
# a.hashify.splat #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
31
31
|
#
|
32
|
-
# Also, the particular dispatch can be left up the Hashify
|
32
|
+
# Also, the particular dispatch can be left up the Hashify
|
33
33
|
# using the #auto method. See Hashify#auto for details on this.
|
34
34
|
#
|
35
35
|
# TODO: This method takes arguments only for the sake of the old
|
@@ -64,11 +64,9 @@ module Enumerable
|
|
64
64
|
#
|
65
65
|
# a = [:a,1,:b,2,:c]
|
66
66
|
# a.to_h_splat #=> { :a=>1, :b=>2, :c=>nil }
|
67
|
-
#
|
68
|
-
# TODO: Can this be done without #size?
|
69
67
|
def splat
|
70
|
-
a = @enum.
|
71
|
-
a << nil if a.size
|
68
|
+
a = @enum.to_a
|
69
|
+
a << nil if a.size.odd?
|
72
70
|
Hash[*a]
|
73
71
|
end
|
74
72
|
|
@@ -78,48 +76,46 @@ module Enumerable
|
|
78
76
|
#
|
79
77
|
# a = [:a,1,[:b,2,:c]]
|
80
78
|
# a.to_h_flat #=> { :a=>1, :b=>2, :c=>nil }
|
81
|
-
#
|
82
|
-
# TODO: Need to do this without #size.
|
83
79
|
def flat
|
84
|
-
a = @enum.flatten
|
85
|
-
a << nil if a.size
|
80
|
+
a = @enum.to_a.flatten
|
81
|
+
a << nil if a.size.odd?
|
86
82
|
Hash[*a]
|
87
83
|
end
|
88
84
|
|
89
|
-
# When a mixed or multi-element
|
85
|
+
# When a mixed or multi-element associative array
|
90
86
|
# is used, the result is as follows:
|
91
87
|
#
|
92
88
|
# a = [ [:a,1,2], [:b,2], [:c], :d ]
|
93
89
|
# a.hashify.assoc #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
94
90
|
#
|
95
91
|
# If the first entry of any subelements are the same, then
|
96
|
-
# the value will be set to the last
|
92
|
+
# the value will be set to the last occurring value.
|
97
93
|
#
|
98
94
|
# a = [ :x, [:x], [:x,1,2], [:x,3], [:x,4] ]
|
99
95
|
# a.hashify.assoc #=> { :x=>[4] }
|
100
96
|
#
|
101
97
|
def associate
|
102
98
|
h = {}
|
103
|
-
each do |k,*v|
|
99
|
+
each do |k,*v|
|
104
100
|
h[k] = v
|
105
101
|
end
|
106
102
|
h
|
107
103
|
end
|
108
104
|
|
109
105
|
# Alias for #associate.
|
110
|
-
alias :assoc
|
106
|
+
alias :assoc :associate
|
111
107
|
|
112
108
|
# Like associate but does force values into an array.
|
113
109
|
#
|
114
110
|
def merge
|
115
111
|
h = {}
|
116
|
-
each do |k,v|
|
112
|
+
each do |k,v|
|
117
113
|
h[k] = v
|
118
114
|
end
|
119
115
|
h
|
120
116
|
end
|
121
117
|
|
122
|
-
# When a mixed or multi-element
|
118
|
+
# When a mixed or multi-element associative array
|
123
119
|
# is used, the result is as follows:
|
124
120
|
#
|
125
121
|
# a = [ [:a,1,2], [:b,2], [:c], :d ]
|
@@ -129,11 +125,11 @@ module Enumerable
|
|
129
125
|
# the values will be merged using #concat.
|
130
126
|
#
|
131
127
|
# a = [ [:a,1,2], [:a,3], [:a,4], [:a], :a ]
|
132
|
-
# a.
|
128
|
+
# a.hashify.concat #=> { :a=>[1,2,3,4] }
|
133
129
|
#
|
134
130
|
def concat
|
135
131
|
h = {}
|
136
|
-
each do |k,*v|
|
132
|
+
each do |k,*v|
|
137
133
|
h[k] ||= []
|
138
134
|
h[k].concat(v)
|
139
135
|
end
|
@@ -141,7 +137,7 @@ module Enumerable
|
|
141
137
|
end
|
142
138
|
|
143
139
|
# Older name for #concat.
|
144
|
-
alias :multi
|
140
|
+
alias :multi :concat
|
145
141
|
|
146
142
|
# Convert enumerable object to Hash using index as keys.
|
147
143
|
#
|
@@ -158,11 +154,11 @@ module Enumerable
|
|
158
154
|
# Converts enumerable object into a hash. Converting an array
|
159
155
|
# into a hash is not a one-to-one conversion, for this
|
160
156
|
# reason #auto examines the enumerable being converted
|
161
|
-
# and then dispatches the conversion to the most
|
162
|
-
# specialized function. There are three
|
157
|
+
# and then dispatches the conversion to the most suitable
|
158
|
+
# specialized function. There are three possibilities for this.
|
163
159
|
#
|
164
160
|
# If the enumerable is a collection of perfect pairs, like that
|
165
|
-
# which Hash#to_a generates, then conversion is handled by
|
161
|
+
# which Hash#to_a generates, then conversion is handled by
|
166
162
|
# #flat.
|
167
163
|
#
|
168
164
|
# a = [ [:a,1], [:b,2] ]
|
@@ -174,14 +170,14 @@ module Enumerable
|
|
174
170
|
# a = [ [:a,1,2], [:b,2], [:c], [:d] ]
|
175
171
|
# a.hashify.auto #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
176
172
|
#
|
177
|
-
# If it
|
173
|
+
# If it contains objects other then arrays then the #splat method
|
178
174
|
# is called.
|
179
175
|
#
|
180
176
|
# a = [ [:a,1,2], 2, :b, [:c,3], 9 ]
|
181
|
-
# a.hashify.auto #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
177
|
+
# a.hashify.auto #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
182
178
|
#
|
183
179
|
# Be aware this is not as efficient as using the underlying methods
|
184
|
-
# directly
|
180
|
+
# directly because it must perform an initial iteration over the
|
185
181
|
# enumerable to determine its contents.
|
186
182
|
|
187
183
|
def auto(&block)
|
@@ -206,18 +202,18 @@ module Enumerable
|
|
206
202
|
end
|
207
203
|
|
208
204
|
# Alias for `#auto`.
|
209
|
-
alias :automatic
|
205
|
+
alias :automatic :auto
|
210
206
|
|
211
207
|
private
|
212
208
|
|
213
209
|
#
|
214
210
|
def each(&b)
|
215
|
-
@enum.each(&b)
|
211
|
+
@enum.each(&b)
|
216
212
|
end
|
217
213
|
|
218
214
|
#
|
219
215
|
def each_with_index(&b)
|
220
|
-
@enum.each_with_index(&b)
|
216
|
+
@enum.each_with_index(&b)
|
221
217
|
end
|
222
218
|
|
223
219
|
end
|
@@ -4,9 +4,19 @@ module Enumerable
|
|
4
4
|
#
|
5
5
|
# [1,2,3].sum #=> 6
|
6
6
|
# [3,3,3].sum #=> 9
|
7
|
-
# [].sum #=> 0
|
8
7
|
#
|
9
|
-
#
|
8
|
+
# Note that Facets' sum method is completely generic -- it can work
|
9
|
+
# on any objects that respond to #+.
|
10
|
+
#
|
11
|
+
# [[1],[2],[3]].sum #=> [1,2,3]
|
12
|
+
#
|
13
|
+
# For this reason it is usually a good idea to provide a default
|
14
|
+
# value. Consider the difference between the two expressions below.
|
15
|
+
#
|
16
|
+
# [].sum #=> nil
|
17
|
+
# [].sum(0) #=> 0
|
18
|
+
#
|
19
|
+
# This default value also acts as an initial value.
|
10
20
|
#
|
11
21
|
# [].sum(5) #=> 5
|
12
22
|
# [1,2,3].sum(10) #=> 16
|
@@ -12,7 +12,7 @@ class File
|
|
12
12
|
#
|
13
13
|
def self.atomic_open(file_name, mode="r", temp_dir=nil, &block)
|
14
14
|
temp_dir = temp_dir || Dir.tmpdir
|
15
|
-
temp_file = Tempfile.new("#{
|
15
|
+
temp_file = Tempfile.new("#{atomic_id}-" + basename(file_name), temp_dir)
|
16
16
|
|
17
17
|
if File.exist?(file_name)
|
18
18
|
FileUtils.cp(file_name, temp_file)
|