ruby-lambdas 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c4fedf3c4a2cc3931f7515e7a18c81a0599f395544b6ea939173f82a60a6708
4
- data.tar.gz: b6b4b6bdaab9acf068688e7d1cdab5e003900ff31c087f146b07be156cfc92c2
3
+ metadata.gz: 771d6089b5f69e89e34b7596edbdf925fa5e2b4648d81da1e3b4bf72ff28f477
4
+ data.tar.gz: dfdf5062b4bdbdab2dccfa0004b503917891cb07440a6e0c8a51320d4e58749a
5
5
  SHA512:
6
- metadata.gz: bf7026c160d65d99a1feb56cde57bbd3c2d1f1f56eec40547ad49322993a296c76468bcc0dd87f9e66b401c9fcf7dec81f1fc96b6bed93429ad39c02c92c0a1b
7
- data.tar.gz: 1e6f0935e01951da557b2ef8a488547982a925f7f495151d86164c7aeb710d95675e56c3c7225d5b8cde1e3666e3a0caf1525aa73bf88e1466b3d21ddb229530
6
+ metadata.gz: b97b8b83d86b626418e87b3aaa6b8a696c2e3b3e2ddc6436ef80b9c1e927475ca2383e00097d874cb51dc2616341cbdb98da36c3073ac7ade707ab041ddd2e25
7
+ data.tar.gz: beb380a94fe2a5a937227b8f7231735000b95f44f4353512cba4246e4df8cd5adf7faf371910bc01ab3acb727e9e026d910385891d81ac374f7d20474eb85681
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-lambdas (0.4.0)
4
+ ruby-lambdas (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -122,6 +122,21 @@ Slugify.(1.0) # => "1.0"
122
122
  Slugify.(' I WILL be a url slug ') # => "i-will-be-a-url-slug"
123
123
  ```
124
124
 
125
+ ### Strict mode
126
+
127
+ By default, all functions believe in their given inputs data types. To change this behavior, you can enable the strict mode (data type checking) via `require "ruby/lambdas/strict-mode"`. e.g:
128
+
129
+ ```ruby
130
+ p ["2", "4"].map(&Numerics + "1") # => ["12", "14"]
131
+
132
+ require "ruby/lambdas/strict-mode"
133
+
134
+ p ["2", "4"].map(&Numerics + "1") # => TypeError ("1" must be Numeric)
135
+ p ["2", "4"].map(&Numerics + 1) # => TypeError ("2" must be Numeric)
136
+ p [2, "4"].map(&Numerics + 1) # => TypeError ("4" must be Numeric)
137
+ p [2, 4].map(&Numerics + 1) # => [3, 5]
138
+ ```
139
+
125
140
  ### Do you like it?
126
141
 
127
142
  So, look in [examples folder](https://github.com/serradura/ruby-lambdas/tree/master/examples) to see more ideas and benchmarks.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'ruby-lambdas', require: 'ruby/lambdas'
9
+ end
10
+
11
+ ############
12
+ # Examples #
13
+ ############
14
+
15
+ p ["2", "4", "8"].map(&Numerics + "2") # => ["22", "24", "28"]
16
+
17
+ # ---
18
+
19
+ require "ruby/lambdas/strict-mode"
20
+
21
+ p [2, 4, 8].map(&Numerics + 2) # => [4, 6, 10]
22
+
23
+ begin
24
+ p ["2", "4", "8"].map(&Numerics + "2")
25
+ rescue TypeError => e
26
+ p e # => #<TypeError: "2" must be Numeric>
27
+ end
28
+
29
+ p ["2 ", " 4", "8 "].map(&Strings::Strip) # => ["2", "4", "8"]
30
+
31
+ begin
32
+ p [2, 4, 8].map(&Strings::Strip)
33
+ rescue TypeError => e
34
+ p e # => #<TypeError: 2 must be a String>
35
+ end
File without changes
@@ -0,0 +1,19 @@
1
+ #
2
+ # These Kernel methods can be overridden by
3
+ # new ones if the strict mode was enabled. e.g:
4
+ #
5
+ # require "ruby/lambdas/strict-mode"
6
+ #
7
+ [:String!, :Numeric!].each do |name|
8
+ Kernel.remove_method(name) if Kernel.respond_to?(name)
9
+ end
10
+
11
+ module Kernel
12
+ def String!(data)
13
+ data
14
+ end
15
+
16
+ def Numeric!(data)
17
+ data
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ [:String!, :Numeric!].each do |name|
2
+ Kernel.remove_method(name) if Kernel.respond_to?(name)
3
+ end
4
+
5
+ module Kernel
6
+ def String!(data)
7
+ return data if data.is_a?(::String)
8
+
9
+ raise TypeError, "#{data.inspect} must be a String"
10
+ end
11
+
12
+ def Numeric!(data)
13
+ return data if data.is_a?(::Numeric)
14
+
15
+ raise TypeError, "#{data.inspect} must be Numeric"
16
+ end
17
+ end
@@ -1,5 +1,6 @@
1
1
  module Numerics
2
- require_relative "import"
2
+ require_relative "core/strict_mode/disable"
3
+ require_relative "core/import"
3
4
  require_relative "numerics/to_export"
4
5
 
5
6
  RubyLambdas::Import.(from: RubyLambdas::Numerics, to: self)
@@ -4,24 +4,24 @@ module RubyLambdas
4
4
  #
5
5
  # Operators
6
6
  #
7
- Add = -> (a, b) { a + b }
7
+ Add = -> (a, b) { Numeric!(a) + Numeric!(b) }
8
8
 
9
- Divide = -> (b, a) { a / b }
9
+ Divide = -> (b, a) { Numeric!(a) / Numeric!(b) }
10
10
 
11
- Subtract = -> (b, a) { a - b }
11
+ Subtract = -> (b, a) { Numeric!(a) - Numeric!(b) }
12
12
 
13
- Multiply = -> (a, b) { a * b }
13
+ Multiply = -> (a, b) { Numeric!(a) * Numeric!(b) }
14
14
 
15
15
  #
16
16
  # Comparables
17
17
  #
18
- GreaterThan = -> (b, a) { a > b }
18
+ GreaterThan = -> (b, a) { Numeric!(a) > Numeric!(b) }
19
19
 
20
- GreaterThanOrEqual = -> (b, a) { a >= b }
20
+ GreaterThanOrEqual = -> (b, a) { Numeric!(a) >= Numeric!(b) }
21
21
 
22
- LessThan = -> (b, a) { a < b }
22
+ LessThan = -> (b, a) { Numeric!(a) < Numeric!(b) }
23
23
 
24
- LessThanOrEqual = -> (b, a) { a <= b }
24
+ LessThanOrEqual = -> (b, a) { Numeric!(a) <= Numeric!(b) }
25
25
  end
26
26
 
27
27
  ALIASES = {
@@ -1,5 +1,5 @@
1
1
  module Objects
2
- require_relative "import"
2
+ require_relative "core/import"
3
3
  require_relative "objects/to_export"
4
4
 
5
5
  RubyLambdas::Import.(from: RubyLambdas::Objects, to: self)
@@ -0,0 +1 @@
1
+ require_relative "core/strict_mode/enable"
@@ -1,5 +1,6 @@
1
1
  module Strings
2
- require_relative "import"
2
+ require_relative "core/strict_mode/disable"
3
+ require_relative "core/import"
3
4
  require_relative "strings/to_export"
4
5
 
5
6
  RubyLambdas::Import.(from: RubyLambdas::Strings, to: self)
@@ -1,23 +1,25 @@
1
1
  module RubyLambdas
2
2
  module Strings
3
3
  module ToExport
4
- Capitalize = -> data { data.capitalize }
4
+ Capitalize = -> data { String!(data).capitalize }
5
5
 
6
- Center = -> (width, data) { data.center(width) }
6
+ Center = -> (width, data) { String!(data).center(width) }
7
7
 
8
- CenterWith = -> (width, padstr, data) { data.center(width, padstr) }
8
+ CenterWith = -> (width, padstr, data) do
9
+ String!(data).center(width, padstr)
10
+ end
9
11
 
10
- Downcase = -> data { data.downcase }
12
+ Downcase = -> data { String!(data).downcase }
11
13
 
12
14
  FromObject = -> data { String(data) }
13
15
 
14
- GSub = -> (pattern, replacement, data) do
15
- return data.gsub(pattern, &replacement) if replacement.is_a?(::Proc)
16
+ GSub = -> (pattern, repl, data) do
17
+ return String!(data).gsub(pattern, &repl) if repl.is_a?(::Proc)
16
18
 
17
- data.gsub(pattern, replacement)
19
+ String!(data).gsub(pattern, repl)
18
20
  end
19
21
 
20
- Strip = -> data { data.strip }
22
+ Strip = -> data { String!(data).strip }
21
23
  end
22
24
 
23
25
  ALIASES = {
@@ -1,3 +1,3 @@
1
1
  module RubyLambdas
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lambdas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -73,12 +73,16 @@ files:
73
73
  - examples/02.rb
74
74
  - examples/03.rb
75
75
  - examples/04.rb
76
+ - examples/strict_mode/01.rb
76
77
  - lib/ruby/lambdas.rb
77
- - lib/ruby/lambdas/import.rb
78
+ - lib/ruby/lambdas/core/import.rb
79
+ - lib/ruby/lambdas/core/strict_mode/disable.rb
80
+ - lib/ruby/lambdas/core/strict_mode/enable.rb
78
81
  - lib/ruby/lambdas/numerics.rb
79
82
  - lib/ruby/lambdas/numerics/to_export.rb
80
83
  - lib/ruby/lambdas/objects.rb
81
84
  - lib/ruby/lambdas/objects/to_export.rb
85
+ - lib/ruby/lambdas/strict-mode.rb
82
86
  - lib/ruby/lambdas/strings.rb
83
87
  - lib/ruby/lambdas/strings/to_export.rb
84
88
  - lib/ruby/lambdas/version.rb