facets 2.6.0 → 2.7.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/HISTORY.rdoc +331 -35
- data/MANIFEST +685 -826
- data/{doc/guide/notes.rd → NOTES} +0 -0
- data/README.rdoc +73 -25
- data/Rakefile +245 -2
- data/TODO +5 -0
- data/demo/hook.rd +47 -0
- data/demo/scenario_require.rd +9 -0
- data/doc/README.more +24 -6
- data/doc/manual/about.rb +47 -0
- data/doc/manual/annotations.rdoc +60 -0
- data/doc/manual/associations.rdoc +55 -0
- data/doc/manual/blockups.rdoc +101 -0
- data/doc/manual/capsule.rdoc +34 -0
- data/doc/manual/command.rdoc +177 -0
- data/doc/manual/core.rdoc +37 -0
- data/doc/manual/faq.rdoc +32 -0
- data/doc/manual/typecast.html +112 -0
- data/lib/core/facets.rb +359 -11
- data/lib/core/facets/array/combination.rb +3 -3
- data/lib/core/facets/array/index.rb +4 -1
- data/lib/core/facets/array/permutation.rb +2 -2
- data/lib/core/facets/array/product.rb +1 -1
- data/lib/core/facets/binding/eval.rb +1 -1
- data/lib/core/facets/denumerable.rb +76 -0
- data/lib/core/facets/duplicable.rb +34 -0
- data/lib/core/facets/enumerable/count.rb +10 -4
- data/lib/core/facets/enumerable/defer.rb +77 -0
- data/lib/core/facets/enumerable/each_by.rb +1 -1
- data/lib/core/facets/enumerable/every.rb +35 -0
- data/lib/{more/facets/elementwise.rb → core/facets/enumerable/ewise.rb} +0 -0
- data/lib/core/facets/enumerable/filter.rb +25 -0
- data/lib/core/facets/enumerable/group_by.rb +1 -1
- data/lib/core/facets/enumerable/none.rb +3 -2
- data/lib/core/facets/enumerable/one.rb +3 -2
- data/lib/core/facets/enumerable/per.rb +61 -0
- data/lib/core/facets/exception/raised.rb +14 -0
- data/lib/core/facets/integer/odd.rb +5 -1
- data/lib/core/facets/kernel/__dir__.rb +13 -3
- data/lib/core/facets/kernel/__here__.rb +14 -0
- data/lib/core/facets/kernel/__method__.rb +9 -3
- data/lib/core/facets/kernel/ask.rb +1 -0
- data/lib/core/facets/kernel/equate.rb +13 -0
- data/lib/core/facets/kernel/extension.rb +9 -0
- data/lib/core/facets/kernel/identical.rb +4 -0
- data/lib/core/facets/kernel/instance_exec.rb +2 -1
- data/lib/core/facets/kernel/method.rb +49 -0
- data/lib/core/facets/kernel/object_send.rb +2 -2
- data/lib/core/facets/kernel/{state.rb → object_state.rb} +23 -12
- data/lib/core/facets/kernel/require_all.rb +6 -1
- data/lib/core/facets/kernel/require_local.rb +8 -1
- data/lib/core/facets/kernel/require_relative.rb +52 -0
- data/lib/core/facets/kernel/source_location.rb +13 -0
- data/lib/core/facets/kernel/tap.rb +13 -6
- data/lib/core/facets/module/attr_setter.rb +57 -0
- data/lib/core/facets/module/instance_method.rb +24 -0
- data/lib/core/facets/module/module_load.rb +60 -44
- data/lib/core/facets/module/module_require.rb +1 -0
- data/lib/core/facets/nilclass/to_f.rb +1 -1
- data/lib/core/facets/objectspace/op_fetch.rb +3 -0
- data/lib/core/facets/proc/curry.rb +4 -3
- data/lib/core/facets/string/bytes.rb +10 -4
- data/lib/core/facets/string/camelcase.rb +6 -5
- data/lib/core/facets/string/chars.rb +5 -1
- data/lib/core/facets/string/each_char.rb +1 -1
- data/lib/core/facets/string/each_word.rb +1 -1
- data/lib/core/facets/string/lines.rb +11 -4
- data/lib/core/facets/string/start_with.rb +9 -2
- data/lib/core/facets/string/unfold.rb +27 -0
- data/lib/core/facets/symbol/succ.rb +3 -3
- data/lib/core/facets/symbol/thrown.rb +20 -0
- data/lib/core/facets/symbol/to_proc.rb +3 -2
- data/lib/core/facets/time/to_time.rb +1 -1
- data/lib/core/facets/to_hash.rb +41 -100
- data/lib/core/facets/unboundmethod/name.rb +20 -23
- data/lib/more/facets/ansicode.rb +1 -10
- data/lib/more/facets/autoarray.rb +3 -31
- data/lib/more/facets/basicobject.rb +73 -0
- data/lib/more/facets/blankslate.rb +2 -66
- data/lib/{lore → more}/facets/cgi.rb +0 -0
- data/lib/more/facets/class_extend.rb +1 -0
- data/lib/{lore → more}/facets/continuation.rb +0 -0
- data/lib/{lore → more}/facets/date.rb +3 -3
- data/lib/more/facets/enumargs.rb +192 -0
- data/lib/more/facets/enumerablepass.rb +2 -216
- data/lib/more/facets/enumerator.rb +62 -0
- data/lib/more/facets/{equatable.rb → equitable.rb} +11 -11
- data/lib/more/facets/expirable.rb +13 -41
- data/lib/{lore → more}/facets/fileutils.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/head.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/safe_ln.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/slice.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/tail.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/wc.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/whereis.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/which.rb +0 -0
- data/lib/{lore → more}/facets/getoptlong.rb +0 -0
- data/lib/more/facets/hook.rb +2 -29
- data/lib/more/facets/inheritor.rb +2 -2
- data/lib/more/facets/instance_eval.rb +50 -0
- data/lib/more/facets/instance_function.rb +78 -0
- data/lib/more/facets/main.rb +20 -15
- data/lib/more/facets/memoize.rb +1 -113
- data/lib/more/facets/module/attr.rb +83 -0
- data/lib/more/facets/module/attr_tester.rb +44 -0
- data/lib/more/facets/module/attr_toggler.rb +59 -0
- data/lib/more/facets/module/attr_validator.rb +34 -0
- data/lib/more/facets/{class_extension.rb → module/class_extend.rb} +21 -13
- data/lib/more/facets/once.rb +59 -0
- data/lib/more/facets/openmodule.rb +1 -0
- data/lib/more/facets/orderedhash.rb +1 -33
- data/lib/{lore → more}/facets/ostruct.rb +0 -0
- data/lib/more/facets/ostructable.rb +1 -4
- data/lib/more/facets/partial.rb +18 -16
- data/lib/{lore → more}/facets/pathname.rb +0 -0
- data/lib/more/facets/preinitialize.rb +157 -0
- data/lib/{lore → more}/facets/rbconfig.rb +0 -0
- data/lib/more/facets/recorder.rb +1 -2
- data/lib/{lore → more}/facets/set.rb +0 -0
- data/lib/{lore → more}/facets/shellwords.rb +0 -0
- data/lib/{lore → more}/facets/uri.rb +0 -0
- data/lib/{lore → more}/facets/yaml.rb +0 -0
- data/lib/{lore → more}/facets/zlib.rb +0 -0
- data/meta/loadpath +0 -1
- data/meta/sitemap +4 -0
- data/meta/version +1 -1
- data/test/core/enumerable/test_count.rb +1 -1
- data/test/{more/test_filter.rb → core/enumerable/test_defer.rb} +24 -22
- data/test/{more/test_elementor.rb → core/enumerable/test_every.rb} +2 -15
- data/test/core/enumerable/test_ewise.rb +23 -0
- data/test/core/enumerable/test_per.rb +18 -0
- data/test/core/enumerable/test_take.rb +13 -0
- data/test/core/kernel/test_deepcopy.rb +1 -1
- data/test/{more/test_1stclassmethod.rb → core/kernel/test_method.rb} +2 -7
- data/test/core/kernel/test_tap.rb +1 -1
- data/test/core/proc/test_curry.rb +11 -0
- data/test/core/string/test_bytes.rb +1 -1
- data/test/core/string/test_camelcase.rb +23 -6
- data/test/core/string/test_lines.rb +1 -1
- data/test/core/string/test_unfold.rb +14 -0
- data/test/{more → core}/test_blank.rb +0 -0
- data/test/{more → core}/test_boolean.rb +0 -0
- data/test/{more → core}/test_functor.rb +0 -0
- data/test/{lore → more}/test_basicobject.rb +0 -0
- data/test/more/{test_class_extension.rb → test_class_extend.rb} +6 -6
- data/test/{lore → more}/test_continuation.rb +0 -0
- data/test/{lore → more}/test_date.rb +0 -0
- data/test/more/{test_enumerablepass.rb → test_enumargs.rb} +2 -4
- data/test/more/{test_equatable.rb → test_equitable.rb} +2 -2
- data/test/more/{test_instantise.rb → test_instance_function.rb} +3 -2
- data/test/more/test_memoize.rb +1 -1
- data/test/{lore → more}/test_ostruct.rb +0 -0
- metadata +865 -1016
- data/RELEASE +0 -38
- data/doc/README.lore +0 -51
- data/doc/log/basic_stats/index.html +0 -39
- data/doc/log/changelog.html +0 -648
- data/doc/log/changelog.txt +0 -217
- data/doc/log/stats/index.html +0 -39
- data/doc/log/testlog.txt +0 -278
- data/doc/notes/CHANGES +0 -2529
- data/doc/rdoc/lore/classes/Array.html +0 -176
- data/doc/rdoc/lore/classes/CGI.html +0 -191
- data/doc/rdoc/lore/classes/Config.html +0 -135
- data/doc/rdoc/lore/classes/Continuation.html +0 -113
- data/doc/rdoc/lore/classes/Date.html +0 -631
- data/doc/rdoc/lore/classes/DateTime.html +0 -583
- data/doc/rdoc/lore/classes/Enumerable.html +0 -89
- data/doc/rdoc/lore/classes/Enumerable/Enumerator.html +0 -147
- data/doc/rdoc/lore/classes/File.html +0 -128
- data/doc/rdoc/lore/classes/FileUtils.html +0 -434
- data/doc/rdoc/lore/classes/GetoptLong.html +0 -118
- data/doc/rdoc/lore/classes/GetoptLong/DSL.html +0 -208
- data/doc/rdoc/lore/classes/Kernel.html +0 -135
- data/doc/rdoc/lore/classes/Logger.html +0 -229
- data/doc/rdoc/lore/classes/Logger/Ansicolor.html +0 -277
- data/doc/rdoc/lore/classes/Logger/LogDevice.html +0 -121
- data/doc/rdoc/lore/classes/NilClass.html +0 -119
- data/doc/rdoc/lore/classes/OpenStruct.html +0 -432
- data/doc/rdoc/lore/classes/Pathname.html +0 -353
- data/doc/rdoc/lore/classes/Set.html +0 -117
- data/doc/rdoc/lore/classes/Shellwords.html +0 -111
- data/doc/rdoc/lore/classes/String.html +0 -140
- data/doc/rdoc/lore/classes/Time.html +0 -154
- data/doc/rdoc/lore/classes/URI.html +0 -454
- data/doc/rdoc/lore/classes/URI/Hash.html +0 -105
- data/doc/rdoc/lore/classes/URI/Kernel.html +0 -122
- data/doc/rdoc/lore/classes/Zlib.html +0 -188
- data/doc/rdoc/lore/created.rid +0 -1
- data/doc/rdoc/lore/files/README.html +0 -286
- data/doc/rdoc/lore/files/doc/README_lore.html +0 -155
- data/doc/rdoc/lore/files/lib/lore/facets/basicobject_rb.html +0 -118
- data/doc/rdoc/lore/files/lib/lore/facets/cgi_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/continuation_rb.html +0 -147
- data/doc/rdoc/lore/files/lib/lore/facets/date_rb.html +0 -97
- data/doc/rdoc/lore/files/lib/lore/facets/enumerator_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/head_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/safe_ln_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/slice_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/tail_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/wc_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/whereis_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/which_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils_rb.html +0 -131
- data/doc/rdoc/lore/files/lib/lore/facets/getoptlong_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/logger_rb.html +0 -142
- data/doc/rdoc/lore/files/lib/lore/facets/ostruct_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/pathname_rb.html +0 -145
- data/doc/rdoc/lore/files/lib/lore/facets/rbconfig_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/set_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/shellwords_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/uri_rb.html +0 -125
- data/doc/rdoc/lore/files/lib/lore/facets/yaml_rb.html +0 -146
- data/doc/rdoc/lore/files/lib/lore/facets/zlib_rb.html +0 -97
- data/doc/rdoc/lore/fr_class_index.html +0 -73
- data/doc/rdoc/lore/fr_file_index.html +0 -71
- data/doc/rdoc/lore/fr_method_index.html +0 -177
- data/doc/rdoc/lore/index.html +0 -26
- data/doc/rdoc/lore/rdoc-style.css +0 -177
- data/doc/release-notes/RELEASE-2.0.5 +0 -8
- data/doc/release-notes/RELEASE-2.1.0 +0 -9
- data/doc/release-notes/RELEASE-2.1.1 +0 -5
- data/doc/release-notes/RELEASE-2.1.2 +0 -6
- data/doc/release-notes/RELEASE-2.1.3 +0 -5
- data/doc/release-notes/RELEASE-2.2.0 +0 -14
- data/doc/release-notes/RELEASE-2.2.1 +0 -4
- data/doc/release-notes/RELEASE-2.3.0 +0 -6
- data/doc/release-notes/RELEASE-2.4.0 +0 -70
- data/doc/release-notes/RELEASE-2.4.1 +0 -8
- data/doc/release-notes/RELEASE-2.4.2 +0 -4
- data/doc/release-notes/RELEASE-2.4.3 +0 -78
- data/doc/release-notes/RELEASE-2.4.4 +0 -38
- data/doc/release-notes/RELEASE-2.4.5 +0 -37
- data/doc/release-notes/RELEASE-2.5.0 +0 -83
- data/lib/core/facets/kernel/instance.rb +0 -19
- data/lib/lore/facets/basicobject.rb +0 -14
- data/lib/lore/facets/enumerator.rb +0 -67
- data/lib/lore/facets/logger.rb +0 -291
- data/lib/more/facets/1stclassmethod.rb +0 -140
- data/lib/more/facets/advisable.rb +0 -162
- data/lib/more/facets/association.rb +0 -210
- data/lib/more/facets/attr.rb +0 -209
- data/lib/more/facets/basex.rb +0 -37
- data/lib/more/facets/bbcode.rb +0 -397
- data/lib/more/facets/bicrypt.rb +0 -265
- data/lib/more/facets/binreadable.rb +0 -221
- data/lib/more/facets/censor.rb +0 -97
- data/lib/more/facets/classmethods.rb +0 -199
- data/lib/more/facets/consoleutils.rb +0 -99
- data/lib/more/facets/crypt.rb +0 -166
- data/lib/more/facets/dependency.rb +0 -151
- data/lib/more/facets/downloader.rb +0 -281
- data/lib/more/facets/duplicable.rb +0 -43
- data/lib/more/facets/elementor.rb +0 -133
- data/lib/more/facets/filter.rb +0 -121
- data/lib/more/facets/heap.rb +0 -22
- data/lib/more/facets/infinity.rb +0 -193
- data/lib/more/facets/ini.rb +0 -264
- data/lib/more/facets/instantise.rb +0 -1
- data/lib/more/facets/instantize.rb +0 -95
- data/lib/more/facets/interval.rb +0 -282
- data/lib/more/facets/iteration.rb +0 -65
- data/lib/more/facets/linkedlist.rb +0 -222
- data/lib/more/facets/lrucache.rb +0 -157
- data/lib/more/facets/matcher.rb +0 -140
- data/lib/more/facets/memoizer.rb +0 -74
- data/lib/more/facets/minitar.rb +0 -1063
- data/lib/more/facets/nackclass.rb +0 -41
- data/lib/more/facets/net/smtp_tls.rb +0 -131
- data/lib/more/facets/nilstatus.rb +0 -48
- data/lib/more/facets/overload.rb +0 -94
- data/lib/more/facets/paramix.rb +0 -202
- data/lib/more/facets/pool.rb +0 -91
- data/lib/more/facets/pqueue.rb +0 -449
- data/lib/more/facets/pry.rb +0 -32
- data/lib/more/facets/reflection.rb +0 -145
- data/lib/more/facets/semaphore.rb +0 -92
- data/lib/more/facets/settings.rb +0 -248
- data/lib/more/facets/snapshot.rb +0 -209
- data/lib/more/facets/sparse_array.rb +0 -809
- data/lib/more/facets/string/mask.rb +0 -278
- data/lib/more/facets/string/obfuscate.rb +0 -65
- data/lib/more/facets/string/stylize.rb +0 -169
- data/lib/more/facets/string/words.rb +0 -167
- data/lib/more/facets/syncarray.rb +0 -114
- data/lib/more/facets/synchash.rb +0 -157
- data/lib/more/facets/typecast.rb +0 -261
- data/lib/more/facets/uninheritable.rb +0 -50
- data/lib/more/facets/xmlhash.rb +0 -112
- data/lib/more/facets/xoxo.rb +0 -259
- data/lib/more/facets/ziputils.rb +0 -490
- data/task/conflicts +0 -63
- data/task/coverage.rake +0 -37
- data/task/methods +0 -49
- data/task/rdoc.rake +0 -17
- data/task/setup.rake +0 -38
- data/task/test.rake +0 -145
- data/test/more/test_advisable.rb +0 -71
- data/test/more/test_association.rb +0 -38
- data/test/more/test_bbcode.rb +0 -21
- data/test/more/test_binreadable.rb +0 -50
- data/test/more/test_buildable.rb +0 -73
- data/test/more/test_classmethods.rb +0 -56
- data/test/more/test_crypt.rb +0 -32
- data/test/more/test_dependency.rb +0 -69
- data/test/more/test_elementwise.rb +0 -25
- data/test/more/test_infinity.rb +0 -40
- data/test/more/test_interval.rb +0 -151
- data/test/more/test_linkedlist.rb +0 -41
- data/test/more/test_lrucache.rb +0 -14
- data/test/more/test_overload.rb +0 -160
- data/test/more/test_paramix.rb +0 -170
- data/test/more/test_prototype.rb +0 -35
- data/test/more/test_snapshot.rb +0 -21
- data/test/more/test_sparsearray.rb +0 -279
- data/test/more/test_syncarray.rb +0 -15
- data/test/more/test_synchash.rb +0 -16
- data/test/more/test_typecast.rb +0 -54
- data/test/more/test_uninheritable.rb +0 -31
- data/test/more/test_xoxo.rb +0 -274
- data/test/test_facets.rb +0 -9
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'facets/enumerator'
|
2
|
+
#require 'facets/enumerable/take'
|
3
|
+
|
4
|
+
# = Denumerable
|
5
|
+
#
|
6
|
+
# Classes which include Enumerable::Filterable will get versions
|
7
|
+
# of map, select etc. which return a Filter, so that they work
|
8
|
+
# horizontally without creating intermediate arrays.
|
9
|
+
#
|
10
|
+
module Denumerable
|
11
|
+
|
12
|
+
#
|
13
|
+
def map
|
14
|
+
Denumerator.new do |output|
|
15
|
+
each do |*input|
|
16
|
+
output.yield yield(*input)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
alias :collect :map
|
21
|
+
|
22
|
+
#
|
23
|
+
def select
|
24
|
+
Denumerator.new do |output|
|
25
|
+
each do |*input|
|
26
|
+
output.yield(*input) if yield(*input)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias :find_all :select
|
31
|
+
|
32
|
+
#
|
33
|
+
def reject
|
34
|
+
Denumerator.new do |output|
|
35
|
+
each do |*input|
|
36
|
+
output.yield(*input) unless yield(*input)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Limit to the first n items in the list
|
42
|
+
def take(n)
|
43
|
+
Denumerator.new do |output|
|
44
|
+
count = 0
|
45
|
+
each do |*input|
|
46
|
+
break if count >= n
|
47
|
+
output.yield(*input)
|
48
|
+
count += 1
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Skip the first n items in the list
|
54
|
+
def skip(n)
|
55
|
+
Denumerator.new do |output|
|
56
|
+
count = 0
|
57
|
+
each do |*input|
|
58
|
+
output.yield(*input) if count >= n
|
59
|
+
count += 1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# TODO: add more methods, e.g. grep, take_while etc.
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
# = Denumerator
|
70
|
+
#
|
71
|
+
# A class like Enumerator, but which has 'lazy' versions of map, select etc.
|
72
|
+
#
|
73
|
+
class Denumerator < Enumerator
|
74
|
+
include Denumerable
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Object #:nodoc:
|
2
|
+
# Can you safely call #dup on this object?
|
3
|
+
# False for nil, false, true, symbols, and numbers; true otherwise.
|
4
|
+
def dup? ; true ; end
|
5
|
+
def clone? ; true ; end
|
6
|
+
end
|
7
|
+
|
8
|
+
class NilClass #:nodoc:
|
9
|
+
def dup? ; false ; end
|
10
|
+
def clone? ; false ; end
|
11
|
+
end
|
12
|
+
|
13
|
+
class FalseClass #:nodoc:
|
14
|
+
def dup? ; false ; end
|
15
|
+
def clone? ; false ; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class TrueClass #:nodoc:
|
19
|
+
def dup? ; false ; end
|
20
|
+
def clone? ; false ; end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Symbol #:nodoc:
|
24
|
+
def dup? ; false ; end
|
25
|
+
def clone? ; false ; end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Numeric #:nodoc:
|
29
|
+
def dup? ; false ; end
|
30
|
+
def clone? ; false ; end
|
31
|
+
end
|
32
|
+
|
33
|
+
# :facets: extra
|
34
|
+
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
module Enumerable
|
2
2
|
|
3
|
-
|
3
|
+
unless method_defined?(:count)
|
4
4
|
|
5
5
|
# Count the number of items in an enumerable
|
6
6
|
# equal (==) to the given object.
|
@@ -16,8 +16,14 @@ unless (RUBY_VERSION[0,3] == '1.9')
|
|
16
16
|
#
|
17
17
|
# CREDIT: Trans
|
18
18
|
|
19
|
-
def count(
|
20
|
-
|
19
|
+
def count(item=ArgumentError, &blk)
|
20
|
+
return select(&blk).size if block_given?
|
21
|
+
return select{ |*i| item == i }.size if ArgumentError == item
|
22
|
+
begin
|
23
|
+
size
|
24
|
+
rescue
|
25
|
+
(i=0; each{|e| i+=1 }; i)
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'facets/denumerable'
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
# Without a block: wrap the Enumerable object in such a way that map,
|
6
|
+
# select and similar operations are performed "horizontally" across a
|
7
|
+
# series of blocks, instead of building an array of results at each step.
|
8
|
+
# This reduces memory usage, allows partial results to be provided
|
9
|
+
# early, and permits working with infinite series.
|
10
|
+
#
|
11
|
+
# a = (1..1_000_000_000).defer.select{ |i| i % 2 == 0 }.
|
12
|
+
# map{ |i| i + 100 }.
|
13
|
+
# take(10).to_a
|
14
|
+
#
|
15
|
+
# With a block: the block acts as an arbitrary filter on the data. Unlike
|
16
|
+
# map, it can choose to drop elements from the result, and/or add
|
17
|
+
# additional ones. The first object passed to the block is the receiver
|
18
|
+
# of the output.
|
19
|
+
#
|
20
|
+
# (1..1_000_000_000).
|
21
|
+
# defer { |out,i| out << i if i % 2 == 0 }. # like select
|
22
|
+
# defer { |out,i| out << i + 100 }. # like map
|
23
|
+
# take(10).
|
24
|
+
# each { |i| puts i }
|
25
|
+
#
|
26
|
+
# Using with a block, defer(&b), is equivalent to:
|
27
|
+
#
|
28
|
+
# defer.filter(&b)
|
29
|
+
#
|
30
|
+
# Use a method like to_a or to_h at the end of the chain when you want an
|
31
|
+
# Array or Hash built with the results, or each{...} if you just want
|
32
|
+
# to output each result and discard it.
|
33
|
+
#
|
34
|
+
def defer(&blk)
|
35
|
+
if block_given?
|
36
|
+
Denumerator.new do |output|
|
37
|
+
each do |*input|
|
38
|
+
yield output, *input
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
Denumerator.new do |output|
|
43
|
+
each do |*input|
|
44
|
+
output.yield *input
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
=begin
|
53
|
+
|
54
|
+
TODO: (Programming Challenge) Dynamically create this in a single
|
55
|
+
method using a Functor and without the need of Denumerable.
|
56
|
+
|
57
|
+
Below is my first shot at it. It's close, but it cannot handle
|
58
|
+
infinite series.
|
59
|
+
|
60
|
+
require 'facets/functor'
|
61
|
+
|
62
|
+
def defer
|
63
|
+
l = lambda do |enum|
|
64
|
+
Functor.new do |op, *a, &b|
|
65
|
+
case op.to_s
|
66
|
+
when /^(to_|each)/
|
67
|
+
enum.__send__(op, *a, &b)
|
68
|
+
else
|
69
|
+
l[enum.__send__(op, *a, &b)]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
l[self]
|
74
|
+
end
|
75
|
+
|
76
|
+
=end
|
77
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'facets/enumerable/per'
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
# Returns an elemental object. This allows
|
6
|
+
# you to map a method on to every element.
|
7
|
+
#
|
8
|
+
# r = [1,2,3].every + 3 #=> [4,5,6]
|
9
|
+
#
|
10
|
+
def every
|
11
|
+
@_every ||= per(:map)
|
12
|
+
end
|
13
|
+
|
14
|
+
# In place version of #every.
|
15
|
+
#
|
16
|
+
def every!
|
17
|
+
raise NoMethodError unless respond_to?(:map!)
|
18
|
+
@_every_inplace ||= per(:map!)
|
19
|
+
end
|
20
|
+
|
21
|
+
#def every
|
22
|
+
# @_every ||= Functor.new do |op,*args|
|
23
|
+
# map{ |a| a.send(op,*args) }
|
24
|
+
# end
|
25
|
+
#end
|
26
|
+
|
27
|
+
#def every!
|
28
|
+
# raise NoMethodError unless respond_to?(:map!)
|
29
|
+
# @_every_inplace ||= Functor.new do |op,*args|
|
30
|
+
# map!{ |a| a.send(op,*args) }
|
31
|
+
# end
|
32
|
+
#end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
# The block acts as an arbitrary filter on the data. Unlike
|
4
|
+
# map, it can choose to drop elements from the result, and/or add
|
5
|
+
# additional ones. The first object passed to the block is the receiver
|
6
|
+
# of the output.
|
7
|
+
#
|
8
|
+
# (1..1_000_000_000).
|
9
|
+
# filter { |out,i| out << i if i % 2 == 0 }. # like select
|
10
|
+
# filter { |out,i| out << i + 100 }. # like map
|
11
|
+
# take(10).each { |i| puts i }
|
12
|
+
#
|
13
|
+
def filter(output=[])
|
14
|
+
if block_given?
|
15
|
+
each do |*input|
|
16
|
+
yield(output, *input)
|
17
|
+
end
|
18
|
+
output
|
19
|
+
else
|
20
|
+
to_enum(:filter)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Enumerable
|
2
2
|
|
3
|
-
unless
|
3
|
+
unless method_defined?(:group_by) or defined?(::ActiveSupport) # 1.9 or ActiveSupport
|
4
4
|
|
5
5
|
# #group_by is used to group items in a collection by something they
|
6
6
|
# have in common. The common factor is the key in the resulting hash, the
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
module Enumerable
|
2
2
|
|
3
|
-
|
3
|
+
unless method_defined?(:none?) # 1.8.7+
|
4
4
|
|
5
5
|
# Enumerable#none? is the logical opposite of the builtin method
|
6
6
|
# Enumerable#any?. It returns +true+ if and only if _none_ of
|
@@ -29,3 +29,4 @@ unless (RUBY_VERSION[0,3] == '1.9')
|
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
32
|
+
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
module Enumerable
|
2
2
|
|
3
|
-
|
3
|
+
unless method_defined?(:one?) # 1.8.7+
|
4
4
|
|
5
5
|
# Enumerable#one? returns +true+ if and only if <em>exactly one</em>
|
6
6
|
# element in the collection satisfies the given predicate.
|
@@ -36,3 +36,4 @@ unless (RUBY_VERSION[0,3] == '1.9')
|
|
36
36
|
end
|
37
37
|
|
38
38
|
end
|
39
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'facets/functor'
|
2
|
+
|
3
|
+
# TODO: Consider Enumerator methods
|
4
|
+
|
5
|
+
module Enumerable #:nodoc:
|
6
|
+
|
7
|
+
# Per element meta-functor.
|
8
|
+
#
|
9
|
+
# [1,2,3].per + 3 #=> [4,5,6]
|
10
|
+
# [1,2,3].per(:map) + 3 #=> [4,5,6]
|
11
|
+
# [1,2,3].per(:select) > 1 #=> [2,3]
|
12
|
+
#
|
13
|
+
# [1,2,3].per.map + 3 #=> [4,5,6]
|
14
|
+
# [1,2,3].per.select > 1 #=> [2,3]
|
15
|
+
#
|
16
|
+
def per(enum_method=nil, *enum_args)
|
17
|
+
if enum_method
|
18
|
+
Permeator.new(self, enum_method, *enum_args)
|
19
|
+
#Functor.new do |op, *args|
|
20
|
+
# __send__(enum_method, *enum_args){ |x| x.__send__(op, *args) } #, &blk) }
|
21
|
+
#end
|
22
|
+
else
|
23
|
+
@__per__ ||= Functor.new do |enum_method, *enum_args|
|
24
|
+
Permeator.new(self, enum_method, *enum_args)
|
25
|
+
#Functor.new do |op, *args|
|
26
|
+
# __send__(enum_method, *enum_args){ |x| x.__send__(op, *args) } #, &blk) }
|
27
|
+
#end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Permeator is a Functor for operating over each element of an Enumearble.
|
33
|
+
# (Note: This used to be called an Elementor.)
|
34
|
+
#
|
35
|
+
# TODO: With Ruby 1.9+ this would not be required, as it can be replaced
|
36
|
+
# by a generic Functor, becuase then lambda definitions can pass blocks.
|
37
|
+
#
|
38
|
+
class Permeator #:nodoc:
|
39
|
+
private(*instance_methods.select{|x| x !~ /^__/ })
|
40
|
+
|
41
|
+
def initialize(enum_object, enum_method=nil, *enum_args)
|
42
|
+
@enum_object = enum_object
|
43
|
+
@enum_method = enum_method || :map
|
44
|
+
@enum_args = enum_args
|
45
|
+
end
|
46
|
+
|
47
|
+
def instance_delegate
|
48
|
+
@enum_object
|
49
|
+
end
|
50
|
+
|
51
|
+
def instance_operator
|
52
|
+
@enum_method
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing(sym, *args, &blk)
|
56
|
+
@enum_object.__send__(@enum_method){ |x| x.__send__(sym, *args, &blk) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|