ruby-lambdas 0.2.0 → 0.3.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: 19c424e1936c0b793d84a5401fe16e61ef506c0ce6b8973095bfe69877078748
4
- data.tar.gz: 238ecf3f90787dd2094ed1c07018755f3f7b5c0f6ead52eac76d7d344a790441
3
+ metadata.gz: fdd09393827cb1cfa7166f4c9390c500b45cb6e374ea1e7fff85d6e8d1023444
4
+ data.tar.gz: ea9faa37417a2abf246d9049e40f09247faacf1908550e84b7bb98d3b342f404
5
5
  SHA512:
6
- metadata.gz: 62eb3e3f36731b625d4629b24b6d6bed9b76643f8c6ae5e3ea8f3eb194a7b51e6170c062430bd82c4dda21fdcd404a49b0008238371a5239bdd0002c90c001f5
7
- data.tar.gz: baf613b0646136602049d21c32de3bac40eb69893d015f9609c17f26b053fc6af67266eb65f8aede2565366fd88b7cd147a54885eb2c6ef7180987b6c5f0cc1d
6
+ metadata.gz: 72b50b42fcebeb42306809114066e946783bc6bfd33e23eea1acd256adc70634de56de2d9fe83bbd0a63c1116733c2db13a3887fa9163352176d8643c05ef62f
7
+ data.tar.gz: b06bea15c208280081a98fa62bb3d2d9521e41e94e359ba4d05bdad09ac0ff922fc5179a3215265e26c043d34df6964dabe53686d7e457f32838d7a1f2bbe4e4
data/README.md CHANGED
@@ -9,7 +9,7 @@ TODO: Delete this and the text above, and describe your gem
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'ruby-lambdas', '~> 0.1.0', require: 'ruby/lambdas'
12
+ gem 'ruby-lambdas', require: 'ruby/lambdas'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -30,6 +30,73 @@ Strings::Strip.call(" hello ") # => "hello"
30
30
 
31
31
  # Strings::Trim is an alias to the Strings::Strip function
32
32
  Strings::Trim.call(" hello ") # => "hello"
33
+
34
+ # Other ways to call a lambda (function):
35
+ Strings::Trim[" hello "]
36
+ Strings::Trim.(" hello ") # Syntactic sugar to hide “call”.
37
+ Strings::Trim.===(" hello ") # This allows a proc object to be the target of a when clause in a case statement.
38
+ Strings::Trim.yield(" hello ")
39
+ ```
40
+
41
+ **Partial application (curried functions):**
42
+
43
+ ```ruby
44
+ require "ruby/lambdas"
45
+
46
+ # We call a function with fewer arguments than it expects and It returns a
47
+ # function that takes the remaining arguments.
48
+ # And this is called Partial Application of Functions. e.g:
49
+
50
+ Strings::GSub.call(/[aeiou]/, "hello", "*") # => "h*ll*"
51
+ Strings::GSub.call(/[aeiou]/).call("hello", "*") # => "h*ll*"
52
+ Strings::GSub.call(/[aeiou]/).call("hello").call("*") # => "h*ll*"
53
+
54
+ # Last example with an alternative syntax:
55
+ Strings::GSub.(/[aeiou]/).("hello").("*") # => "h*ll*"
56
+
57
+ # ---
58
+
59
+ replace_vowels = Strings::GSub.call(/[aeiou]/)
60
+
61
+ replace_vowels.call("_", "banana") # => "b_n_n_"
62
+
63
+ # ---
64
+
65
+ hide_vowels = replace_vowels.call("*")
66
+
67
+ hide_vowels.call("hello") # => "h*ll*"
68
+ ```
69
+
70
+ **Pipelines:**
71
+
72
+ ```ruby
73
+ require "ruby/lambdas"
74
+
75
+ ###########################
76
+ # RUBY_VERSION >= '2.6.0' #
77
+ ###########################
78
+
79
+ value = ' I WILL be a url slug '
80
+
81
+ value
82
+ .then(&Strings::Strip)
83
+ .then(&Strings::Downcase)
84
+ .then(&Strings::GSub[' ', '-'])
85
+
86
+ # => "i-will-be-a-url-slug"
87
+
88
+ ###########################
89
+ # RUBY_VERSION >= '2.5.0' #
90
+ ###########################
91
+
92
+ value = ' I WILL be a url slug '
93
+
94
+ value
95
+ .yield_self(&Strings::Strip)
96
+ .yield_self(&Strings::Downcase)
97
+ .yield_self(&Strings::GSub[' ', '-'])
98
+
99
+ # => "i-will-be-a-url-slug"
33
100
  ```
34
101
 
35
102
  **Composing functions (`RUBY_VERSION >= '2.6.0'`):**
@@ -39,7 +106,7 @@ require "ruby/lambdas"
39
106
  Slugify = # Slugify =
40
107
  Strings::FromObject # Strings::String
41
108
  .>> Strings::Strip # >> Strings::Trim \
42
- .>> Strings::LowerCase # >> Strings::LowerCase \
109
+ .>> Strings::Downcase # >> Strings::LowerCase \
43
110
  .>> Strings::GSub[' ', '-'] # >> Strings::ReplaceAll[' ', '-']
44
111
 
45
112
  # Reading the previous composition (Step by step)
@@ -64,7 +131,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
64
131
 
65
132
  ## Contributing
66
133
 
67
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby-lambdas. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
134
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/ruby-lambdas. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
135
 
69
136
  ## License
70
137
 
@@ -72,4 +139,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
72
139
 
73
140
  ## Code of Conduct
74
141
 
75
- Everyone interacting in the RubyLambdas project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby-lambdas/blob/master/CODE_OF_CONDUCT.md).
142
+ Everyone interacting in the RubyLambdas project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/ruby-lambdas/blob/master/CODE_OF_CONDUCT.md).
data/lib/ruby/lambdas.rb CHANGED
@@ -2,4 +2,5 @@ require "ruby/lambdas/version"
2
2
 
3
3
  module RubyLambdas
4
4
  require_relative "lambdas/strings"
5
+ require_relative "lambdas/objects"
5
6
  end
@@ -0,0 +1,23 @@
1
+ module RubyLambdas
2
+ class Import
3
+ def self.call(from:, to:)
4
+ to_export = from::ToExport
5
+
6
+ to_export.constants.each do |function_name|
7
+ function = to_export.const_get(function_name)
8
+
9
+ to.const_set(
10
+ function_name,
11
+ function.arity == 1 ? function : function.curry
12
+ )
13
+ end
14
+
15
+ return unless from.const_defined?(:ALIASES)
16
+
17
+ from::ALIASES.each do |name, new_names|
18
+ Array(new_names)
19
+ .each { |new_name| to.const_set(new_name, to.const_get(name)) }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ module Objects
2
+ require_relative "import"
3
+ require_relative "objects/to_export"
4
+
5
+ RubyLambdas::Import.(from: RubyLambdas::Objects, to: self)
6
+ end
@@ -0,0 +1,11 @@
1
+ module RubyLambdas
2
+ module Objects
3
+ module ToExport
4
+ ToS = -> data { data.to_s }
5
+ end
6
+
7
+ ALIASES = {
8
+ ToS: :ToString
9
+ }.freeze
10
+ end
11
+ end
@@ -1,21 +1,6 @@
1
1
  module Strings
2
+ require_relative "import"
2
3
  require_relative "strings/to_export"
3
4
 
4
- RubyLambdas::Strings::ToExport.tap do |to_export|
5
- to_export
6
- .constants
7
- .each do |function_name|
8
- function = to_export.const_get(function_name)
9
-
10
- const_set(
11
- function_name,
12
- function.arity == 1 ? function : function.curry
13
- )
14
- end
15
- end
16
-
17
- RubyLambdas::Strings::ALIASES
18
- .each do |name, new_names|
19
- Array(new_names).each { |new_name| const_set(new_name, const_get(name)) }
20
- end
5
+ RubyLambdas::Import.(from: RubyLambdas::Strings, to: self)
21
6
  end
@@ -1,3 +1,3 @@
1
1
  module RubyLambdas
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -69,6 +69,9 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/ruby/lambdas.rb
72
+ - lib/ruby/lambdas/import.rb
73
+ - lib/ruby/lambdas/objects.rb
74
+ - lib/ruby/lambdas/objects/to_export.rb
72
75
  - lib/ruby/lambdas/strings.rb
73
76
  - lib/ruby/lambdas/strings/to_export.rb
74
77
  - lib/ruby/lambdas/version.rb