ruby-nuggets 0.0.3.159 → 0.0.4.171
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/nuggets/enumerable/minmax.rb +117 -0
- data/lib/nuggets/version.rb +1 -1
- metadata +7 -6
data/README
CHANGED
@@ -0,0 +1,117 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Enumerable
|
29
|
+
|
30
|
+
alias_method :original_max, :max
|
31
|
+
alias_method :original_min, :min
|
32
|
+
|
33
|
+
# call-seq:
|
34
|
+
# enum.minmax_by(meth, by) => aValue
|
35
|
+
#
|
36
|
+
# Finds the maximum/minimum (or whatever +meth+ is) value in _enum_ according
|
37
|
+
# to +by+ (which may be a symbol/string that is sent to each value, or a proc
|
38
|
+
# that receives each value as parameter).
|
39
|
+
def minmax_by(meth, by)
|
40
|
+
_by = by.is_a?(Proc) ? by : lambda { |i| i.send(by) }
|
41
|
+
send(meth) { |a, b| _by[a] <=> _by[b] }
|
42
|
+
end
|
43
|
+
|
44
|
+
# call-seq:
|
45
|
+
# enum.max_by(by) => aValue
|
46
|
+
#
|
47
|
+
# Maximum #minmax_by.
|
48
|
+
def max_by(by)
|
49
|
+
minmax_by(:max, by)
|
50
|
+
end
|
51
|
+
|
52
|
+
# call-seq:
|
53
|
+
# enum.min_by(by) => aValue
|
54
|
+
#
|
55
|
+
# Minimum #minmax_by.
|
56
|
+
def min_by(by)
|
57
|
+
minmax_by(:min, by)
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# enum.minmax(meth, what) => anObject
|
62
|
+
#
|
63
|
+
# Finds the #minmax_by according to +what+ and returns that "what".
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
# %w[a bcd ef].max(:length) #=> 3
|
67
|
+
def minmax(meth, what)
|
68
|
+
#m = minmax_by(meth, what)
|
69
|
+
#what.is_a?(Proc) ? what[m] : m.send(what)
|
70
|
+
|
71
|
+
_what = what.is_a?(Proc) ? what : lambda { |i| i.send(what) }
|
72
|
+
map { |i| _what[i] }.send(meth)
|
73
|
+
|
74
|
+
# Benchmark (:max, :length; enum.size = 20, N = 100_000):
|
75
|
+
#
|
76
|
+
# max_by(:length).length 7.920000 0.890000 8.810000 ( 8.991915)
|
77
|
+
# map(:length).max 4.800000 0.600000 5.400000 ( 5.418114)
|
78
|
+
end
|
79
|
+
|
80
|
+
# call-seq:
|
81
|
+
# enum.max(what) => aValue
|
82
|
+
#
|
83
|
+
# Maximum #minmax. If +what+ is omitted, or nil, the original Enumerable#max
|
84
|
+
# is called (available as +original_max+).
|
85
|
+
def max(what = nil, &block)
|
86
|
+
what ? minmax(:max, what) : original_max(&block)
|
87
|
+
end
|
88
|
+
|
89
|
+
# call-seq:
|
90
|
+
# enum.min(what) => aValue
|
91
|
+
#
|
92
|
+
# Minimum #minmax. If +what+ is omitted, or nil, the original Enumerable#min
|
93
|
+
# is called (available as +original_min+).
|
94
|
+
def min(what = nil, &block)
|
95
|
+
what ? minmax(:min, what) : original_min(&block)
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
if $0 == __FILE__
|
101
|
+
e = %w[quux quuux quix]
|
102
|
+
p e
|
103
|
+
|
104
|
+
p e.max
|
105
|
+
p e.max_by(:length)
|
106
|
+
p e.max(:length)
|
107
|
+
|
108
|
+
e = [3, 222, 45]
|
109
|
+
p e
|
110
|
+
|
111
|
+
# the last digit counts ;-)
|
112
|
+
l = lambda { |i| i.to_s.split(//).last.to_i }
|
113
|
+
|
114
|
+
p e.max
|
115
|
+
p e.max_by(l)
|
116
|
+
p e.max(l)
|
117
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4.171
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-01-
|
12
|
+
date: 2008-01-10 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,6 +27,7 @@ files:
|
|
27
27
|
- lib/nuggets/version.rb
|
28
28
|
- lib/nuggets/object/singleton_class.rb
|
29
29
|
- lib/nuggets/object/blank.rb
|
30
|
+
- lib/nuggets/enumerable/minmax.rb
|
30
31
|
- lib/nuggets/all.rb
|
31
32
|
- lib/nuggets/string/msub.rb
|
32
33
|
- lib/nuggets/string/case.rb
|
@@ -54,15 +55,15 @@ has_rdoc: true
|
|
54
55
|
homepage: http://prometheus.rubyforge.org/ruby-nuggets
|
55
56
|
post_install_message:
|
56
57
|
rdoc_options:
|
57
|
-
- --all
|
58
|
-
- --inline-source
|
59
58
|
- --charset
|
60
59
|
- UTF-8
|
61
|
-
- --
|
62
|
-
- README
|
60
|
+
- --all
|
63
61
|
- --title
|
64
62
|
- ruby-nuggets Application documentation
|
63
|
+
- --main
|
64
|
+
- README
|
65
65
|
- --line-numbers
|
66
|
+
- --inline-source
|
66
67
|
require_paths:
|
67
68
|
- lib
|
68
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|