mug 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mug.rb CHANGED
@@ -1,13 +1,14 @@
1
-
2
- require_relative 'mug/apply'
3
- require_relative 'mug/bool'
4
- require_relative 'mug/fragile-method-chain'
5
- require_relative 'mug/hashmap'
6
- require_relative 'mug/hashop'
7
- require_relative 'mug/iterator/for'
8
- require_relative 'mug/iterator/method'
9
- require_relative 'mug/maybe'
10
- require_relative 'mug/self'
11
- require_relative 'mug/tau'
12
- require_relative 'mug/to_h'
13
-
1
+
2
+ require_relative 'mug/apply'
3
+ require_relative 'mug/array/extend'
4
+ require_relative 'mug/bool'
5
+ require_relative 'mug/fragile-method-chain'
6
+ require_relative 'mug/hash/map'
7
+ require_relative 'mug/hash/operations'
8
+ require_relative 'mug/iterator/for'
9
+ require_relative 'mug/iterator/method'
10
+ require_relative 'mug/maybe'
11
+ require_relative 'mug/self'
12
+ require_relative 'mug/tau'
13
+ require_relative 'mug/to_h'
14
+
@@ -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
+
@@ -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
@@ -0,0 +1 @@
1
+ require_relative 'array/extend'
@@ -0,0 +1,69 @@
1
+
2
+ class Array
3
+
4
+ ##
5
+ # Extend this Array.
6
+ #
7
+ # In the first form, when a +size+ and an optional +obj+ are sent,
8
+ # the array is extended with +size+ copies of +obj+. Take notice that
9
+ # all elements will reference the same object +obj+.
10
+ #
11
+ # The second form appends a copy of the array passed as a parameter
12
+ # (the array is generated by calling #to_ary on the parameter).
13
+ # @see #concat
14
+ # @see #+
15
+ #
16
+ # In the last form, the array is extended by the given size. Each new
17
+ # element in the array is created by passing the element's index to the
18
+ # given block and storing the return value.
19
+ #
20
+ # @call-seq extend!(size=0, obj=nil)
21
+ # @call-seq extend!(array)
22
+ # @call-seq extend!(size) {|index| block }
23
+ #
24
+ def extend! size, *rest
25
+ raise ArgumentError, "wrong number of arguments (#{rest.length+1} for 1..2)" if rest.length > 1
26
+
27
+ # Same logic as array.c/rb_ary_initialize
28
+ if rest.empty? && !size.is_a?(Fixnum)
29
+ warn 'warning: given block not used' if block_given?
30
+ concat size.to_ary
31
+ return self
32
+ end
33
+
34
+ raise ArgumentError, 'negative size' if size < 0
35
+
36
+ a = length
37
+ b = a+size
38
+ if block_given?
39
+ warn 'warning: block supersedes default value argument' if !rest.empty?
40
+ fill(a...b) {|i| yield i }
41
+ else
42
+ obj = rest[0]
43
+ fill(a...b) { obj }
44
+ end
45
+ end
46
+
47
+ # @see #extend!
48
+ def extend *args, &block
49
+ dup.extend! *args, &block
50
+ end
51
+
52
+ end
53
+
54
+ =begin
55
+ Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
56
+
57
+ Permission to use, copy, modify, and/or distribute this software for any
58
+ purpose with or without fee is hereby granted, provided that the above
59
+ copyright notice and this permission notice appear in all copies.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
62
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
63
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
64
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
65
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
66
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
67
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68
+ =end
69
+
@@ -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
+