mug 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug/and-or.rb +48 -48
- data/lib/mug/apply.rb +58 -58
- data/lib/mug/bool.rb +128 -128
- data/lib/mug/fragile-method-chain.rb +70 -70
- data/lib/mug/iterator.rb +2 -2
- data/lib/mug/iterator/for.rb +29 -29
- data/lib/mug/iterator/method.rb +29 -29
- data/lib/mug/iterator_c.rb +44 -44
- data/lib/mug/maybe.rb +59 -59
- data/lib/mug/self.rb +13 -0
- data/lib/mug/tau.rb +44 -44
- data/test/test-and-or.rb +41 -41
- data/test/test-fragile-method-chain.rb +65 -65
- data/test/test-iterator-for.rb +18 -18
- data/test/test-iterator-method.rb +18 -18
- data/test/test-maybe.rb +90 -90
- data/test/test-self.rb +10 -0
- data/test/test-tau.rb +22 -22
- data/test/test-to_h.rb +16 -16
- data/test/test_apply.rb +56 -56
- metadata +19 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7df248ff5d52dd3e3e1cf6013d34396e5b18b6e6
|
4
|
+
data.tar.gz: b3763159ff1dd730f3c430f6f530ce6b9b81e9a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1abdd9d4aed84c1e0fe9d0052e63bb928f55c5a39387c35f8e53cbe943c6385191283cc459e0e65fb5f49c338ef310c1a4117bfdb7a9c93e74c0598a336d1550
|
7
|
+
data.tar.gz: 17c75ae295933e5a21bc1a626bca13010213ab1d0a2c1c271ef7a80affb01a284ea8c5aebc29b5d20aa7d893359f769ed839701c1d37780a072e81d31d061e43
|
data/lib/mug/and-or.rb
CHANGED
@@ -1,48 +1,48 @@
|
|
1
|
-
|
2
|
-
class Object
|
3
|
-
|
4
|
-
#
|
5
|
-
# Returns either +obj+ or +default+, depending on the falsiness of +obj+.
|
6
|
-
#
|
7
|
-
# If a block is given, +obj+ is yielded to it; if it returns truthy,
|
8
|
-
# +default+ is returned, otherwise +obj+ is returned.
|
9
|
-
#
|
10
|
-
def and default
|
11
|
-
if block_given?
|
12
|
-
yield(self) ? default : self
|
13
|
-
else
|
14
|
-
self && default
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
#
|
19
|
-
# Returns either +obj+ or +default+, depending on the truthiness of +obj+.
|
20
|
-
#
|
21
|
-
# If a block is given, +obj+ is yielded to it; if it returns truthy,
|
22
|
-
# +obj+ is returned, otherwise +default+ is returned.
|
23
|
-
#
|
24
|
-
def or default
|
25
|
-
if block_given?
|
26
|
-
yield(self) ? self : default
|
27
|
-
else
|
28
|
-
self || default
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
=begin
|
34
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
35
|
-
|
36
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
37
|
-
purpose with or without fee is hereby granted, provided that the above
|
38
|
-
copyright notice and this permission notice appear in all copies.
|
39
|
-
|
40
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
41
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
42
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
43
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
44
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
45
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
46
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
47
|
-
=end
|
48
|
-
|
1
|
+
|
2
|
+
class Object
|
3
|
+
|
4
|
+
#
|
5
|
+
# Returns either +obj+ or +default+, depending on the falsiness of +obj+.
|
6
|
+
#
|
7
|
+
# If a block is given, +obj+ is yielded to it; if it returns truthy,
|
8
|
+
# +default+ is returned, otherwise +obj+ is returned.
|
9
|
+
#
|
10
|
+
def and default
|
11
|
+
if block_given?
|
12
|
+
yield(self) ? default : self
|
13
|
+
else
|
14
|
+
self && default
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Returns either +obj+ or +default+, depending on the truthiness of +obj+.
|
20
|
+
#
|
21
|
+
# If a block is given, +obj+ is yielded to it; if it returns truthy,
|
22
|
+
# +obj+ is returned, otherwise +default+ is returned.
|
23
|
+
#
|
24
|
+
def or default
|
25
|
+
if block_given?
|
26
|
+
yield(self) ? self : default
|
27
|
+
else
|
28
|
+
self || default
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
=begin
|
34
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
35
|
+
|
36
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
37
|
+
purpose with or without fee is hereby granted, provided that the above
|
38
|
+
copyright notice and this permission notice appear in all copies.
|
39
|
+
|
40
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
41
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
42
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
43
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
44
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
45
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
46
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
47
|
+
=end
|
48
|
+
|
data/lib/mug/apply.rb
CHANGED
@@ -1,58 +1,58 @@
|
|
1
|
-
|
2
|
-
class Proc
|
3
|
-
#
|
4
|
-
# Curries this Proc and partially applies parameters.
|
5
|
-
# If a sufficient number of arguments are supplied, it passes the
|
6
|
-
# supplied arguments to the original proc and returns the result.
|
7
|
-
# Otherwise, returns another curried proc that takes the rest of
|
8
|
-
# arguments.
|
9
|
-
#
|
10
|
-
def apply(*args)
|
11
|
-
curry.call(*args)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class Method
|
16
|
-
#
|
17
|
-
# Returns a curried proc. If the optional arity argument is given,
|
18
|
-
# it determines the number of arguments. A curried proc receives
|
19
|
-
# some arguments. If a sufficient number of arguments are supplied,
|
20
|
-
# it passes the supplied arguments to the original proc and returns
|
21
|
-
# the result. Otherwise, returns another curried proc that takes the
|
22
|
-
# rest of arguments.
|
23
|
-
#
|
24
|
-
def curry(n=nil)
|
25
|
-
if n
|
26
|
-
to_proc.curry n
|
27
|
-
else
|
28
|
-
to_proc.curry
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Curries this Method and partially applies parameters.
|
34
|
-
# If a sufficient number of arguments are supplied, it passes the
|
35
|
-
# supplied arguments to the original proc and returns the result.
|
36
|
-
# Otherwise, returns another curried proc that takes the rest of
|
37
|
-
# arguments.
|
38
|
-
#
|
39
|
-
def apply(*args)
|
40
|
-
curry.call(*args)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
=begin
|
45
|
-
Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
|
46
|
-
|
47
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
48
|
-
purpose with or without fee is hereby granted, provided that the above
|
49
|
-
copyright notice and this permission notice appear in all copies.
|
50
|
-
|
51
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
52
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
53
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
54
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
55
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
56
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
57
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
58
|
-
=end
|
1
|
+
|
2
|
+
class Proc
|
3
|
+
#
|
4
|
+
# Curries this Proc and partially applies parameters.
|
5
|
+
# If a sufficient number of arguments are supplied, it passes the
|
6
|
+
# supplied arguments to the original proc and returns the result.
|
7
|
+
# Otherwise, returns another curried proc that takes the rest of
|
8
|
+
# arguments.
|
9
|
+
#
|
10
|
+
def apply(*args)
|
11
|
+
curry.call(*args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Method
|
16
|
+
#
|
17
|
+
# Returns a curried proc. If the optional arity argument is given,
|
18
|
+
# it determines the number of arguments. A curried proc receives
|
19
|
+
# some arguments. If a sufficient number of arguments are supplied,
|
20
|
+
# it passes the supplied arguments to the original proc and returns
|
21
|
+
# the result. Otherwise, returns another curried proc that takes the
|
22
|
+
# rest of arguments.
|
23
|
+
#
|
24
|
+
def curry(n=nil)
|
25
|
+
if n
|
26
|
+
to_proc.curry n
|
27
|
+
else
|
28
|
+
to_proc.curry
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Curries this Method and partially applies parameters.
|
34
|
+
# If a sufficient number of arguments are supplied, it passes the
|
35
|
+
# supplied arguments to the original proc and returns the result.
|
36
|
+
# Otherwise, returns another curried proc that takes the rest of
|
37
|
+
# arguments.
|
38
|
+
#
|
39
|
+
def apply(*args)
|
40
|
+
curry.call(*args)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
=begin
|
45
|
+
Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
|
46
|
+
|
47
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
48
|
+
purpose with or without fee is hereby granted, provided that the above
|
49
|
+
copyright notice and this permission notice appear in all copies.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
52
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
53
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
54
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
55
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
56
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
57
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
58
|
+
=end
|
data/lib/mug/bool.rb
CHANGED
@@ -1,128 +1,128 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# Converts arg to a boolean (true or false).
|
4
|
-
#
|
5
|
-
def Bool(arg)
|
6
|
-
!!arg
|
7
|
-
end
|
8
|
-
|
9
|
-
class Object
|
10
|
-
#
|
11
|
-
# Converts obj to a boolean.
|
12
|
-
#
|
13
|
-
def to_bool
|
14
|
-
true
|
15
|
-
end
|
16
|
-
|
17
|
-
#
|
18
|
-
# Converts obj to a boolean.
|
19
|
-
#
|
20
|
-
def to_b
|
21
|
-
true
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def nil.to_bool; false; end
|
26
|
-
def nil.to_b; false; end
|
27
|
-
def false.to_bool; false; end
|
28
|
-
def false.to_b; false; end
|
29
|
-
|
30
|
-
class Numeric
|
31
|
-
#
|
32
|
-
# Converts num to a boolean.
|
33
|
-
# Returns true if not zero.
|
34
|
-
#
|
35
|
-
def to_b
|
36
|
-
self != 0
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
class Float
|
41
|
-
#
|
42
|
-
# Converts num to a boolean.
|
43
|
-
# Returns true if not zero or NaN.
|
44
|
-
# Note: -0.0 is false, and +/-infinity are true.
|
45
|
-
#
|
46
|
-
def to_b
|
47
|
-
!(self.zero? || self.nan?)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class String
|
52
|
-
#
|
53
|
-
# Converts str to a boolean.
|
54
|
-
# Returns true if not empty.
|
55
|
-
#
|
56
|
-
def to_b
|
57
|
-
!empty?
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
class Array
|
62
|
-
#
|
63
|
-
# Converts ary to a boolean.
|
64
|
-
# Returns true if not empty.
|
65
|
-
#
|
66
|
-
def to_b
|
67
|
-
!empty?
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Hash
|
72
|
-
#
|
73
|
-
# Converts hsh to a boolean.
|
74
|
-
# Returns true if not empty.
|
75
|
-
#
|
76
|
-
def to_b
|
77
|
-
!empty?
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
module Enumerable
|
82
|
-
#
|
83
|
-
# Converts enum to a boolean.
|
84
|
-
# Returns true if there are any elements.
|
85
|
-
#
|
86
|
-
def to_b
|
87
|
-
any?{ true }
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
if RUBY_VERSION.to_i >= 2
|
92
|
-
class Enumerator
|
93
|
-
#
|
94
|
-
# Converts enum to a boolean.
|
95
|
-
# Returns true if there are any elements.
|
96
|
-
#
|
97
|
-
def to_b
|
98
|
-
size.to_b
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
class Exception
|
104
|
-
#
|
105
|
-
# Converts ex to a boolean.
|
106
|
-
# All Exceptions are considered false.
|
107
|
-
#
|
108
|
-
def to_b
|
109
|
-
false
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
=begin
|
114
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
115
|
-
|
116
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
117
|
-
purpose with or without fee is hereby granted, provided that the above
|
118
|
-
copyright notice and this permission notice appear in all copies.
|
119
|
-
|
120
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
121
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
122
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
123
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
124
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
125
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
126
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
127
|
-
=end
|
128
|
-
|
1
|
+
|
2
|
+
#
|
3
|
+
# Converts arg to a boolean (true or false).
|
4
|
+
#
|
5
|
+
def Bool(arg)
|
6
|
+
!!arg
|
7
|
+
end
|
8
|
+
|
9
|
+
class Object
|
10
|
+
#
|
11
|
+
# Converts obj to a boolean.
|
12
|
+
#
|
13
|
+
def to_bool
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Converts obj to a boolean.
|
19
|
+
#
|
20
|
+
def to_b
|
21
|
+
true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def nil.to_bool; false; end
|
26
|
+
def nil.to_b; false; end
|
27
|
+
def false.to_bool; false; end
|
28
|
+
def false.to_b; false; end
|
29
|
+
|
30
|
+
class Numeric
|
31
|
+
#
|
32
|
+
# Converts num to a boolean.
|
33
|
+
# Returns true if not zero.
|
34
|
+
#
|
35
|
+
def to_b
|
36
|
+
self != 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Float
|
41
|
+
#
|
42
|
+
# Converts num to a boolean.
|
43
|
+
# Returns true if not zero or NaN.
|
44
|
+
# Note: -0.0 is false, and +/-infinity are true.
|
45
|
+
#
|
46
|
+
def to_b
|
47
|
+
!(self.zero? || self.nan?)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class String
|
52
|
+
#
|
53
|
+
# Converts str to a boolean.
|
54
|
+
# Returns true if not empty.
|
55
|
+
#
|
56
|
+
def to_b
|
57
|
+
!empty?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Array
|
62
|
+
#
|
63
|
+
# Converts ary to a boolean.
|
64
|
+
# Returns true if not empty.
|
65
|
+
#
|
66
|
+
def to_b
|
67
|
+
!empty?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Hash
|
72
|
+
#
|
73
|
+
# Converts hsh to a boolean.
|
74
|
+
# Returns true if not empty.
|
75
|
+
#
|
76
|
+
def to_b
|
77
|
+
!empty?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module Enumerable
|
82
|
+
#
|
83
|
+
# Converts enum to a boolean.
|
84
|
+
# Returns true if there are any elements.
|
85
|
+
#
|
86
|
+
def to_b
|
87
|
+
any?{ true }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
if RUBY_VERSION.to_i >= 2
|
92
|
+
class Enumerator
|
93
|
+
#
|
94
|
+
# Converts enum to a boolean.
|
95
|
+
# Returns true if there are any elements.
|
96
|
+
#
|
97
|
+
def to_b
|
98
|
+
size.to_b
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Exception
|
104
|
+
#
|
105
|
+
# Converts ex to a boolean.
|
106
|
+
# All Exceptions are considered false.
|
107
|
+
#
|
108
|
+
def to_b
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
=begin
|
114
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
115
|
+
|
116
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
117
|
+
purpose with or without fee is hereby granted, provided that the above
|
118
|
+
copyright notice and this permission notice appear in all copies.
|
119
|
+
|
120
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
121
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
122
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
123
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
124
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
125
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
126
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
127
|
+
=end
|
128
|
+
|