lambdada 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile.lock +52 -0
- data/README.md +74 -0
- data/lambdada.gemspec +26 -0
- data/lib/lambdada/proc.rb +23 -0
- data/lib/lambdada/version.rb +14 -0
- data/lib/lambdada.rb +4 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f228d041aaef2bdf90f0aaeaaec194f73ee061b
|
4
|
+
data.tar.gz: ba1ed741814779fac770e91aa7a9750862454400
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7885341c8a8e2357f7f4cb4f5fa42b996a2f74fd8a7ceaffcba9ca28efaa0bf79f3bbe5d205e65b563e5f4d07c0c7ada587396d4b42701f2ebbb636b27f1c87
|
7
|
+
data.tar.gz: aa42e14d12da5a83a0bcd4f92ed32fc0a20f4e15684abb19e405c4ce626130aad2b0c4191166b11792732b0c4be13aa479d90a87b8046a0805cc702066ad935d
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ast (2.0.0)
|
5
|
+
astrolabe (1.3.0)
|
6
|
+
parser (>= 2.2.0.pre.3, < 3.0)
|
7
|
+
byebug (4.0.5)
|
8
|
+
columnize (= 0.9.0)
|
9
|
+
coderay (1.1.0)
|
10
|
+
columnize (0.9.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
parser (2.2.2.5)
|
14
|
+
ast (>= 1.1, < 3.0)
|
15
|
+
powerpack (0.1.1)
|
16
|
+
pry (0.10.1)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.8.1)
|
19
|
+
slop (~> 3.4)
|
20
|
+
pry-byebug (3.1.0)
|
21
|
+
byebug (~> 4.0)
|
22
|
+
pry (~> 0.10)
|
23
|
+
rainbow (2.0.0)
|
24
|
+
rspec (3.3.0)
|
25
|
+
rspec-core (~> 3.3.0)
|
26
|
+
rspec-expectations (~> 3.3.0)
|
27
|
+
rspec-mocks (~> 3.3.0)
|
28
|
+
rspec-core (3.3.2)
|
29
|
+
rspec-support (~> 3.3.0)
|
30
|
+
rspec-expectations (3.3.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.3.0)
|
33
|
+
rspec-mocks (3.3.2)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.3.0)
|
36
|
+
rspec-support (3.3.0)
|
37
|
+
rubocop (0.32.1)
|
38
|
+
astrolabe (~> 1.3)
|
39
|
+
parser (>= 2.2.2.5, < 3.0)
|
40
|
+
powerpack (~> 0.1)
|
41
|
+
rainbow (>= 1.99.1, < 3.0)
|
42
|
+
ruby-progressbar (~> 1.4)
|
43
|
+
ruby-progressbar (1.7.5)
|
44
|
+
slop (3.6.0)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
pry-byebug
|
51
|
+
rspec (~> 3.3)
|
52
|
+
rubocop
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Let's dance the Lambdada !
|
2
|
+
|
3
|
+
You'd like a easier function composition in ruby ? Lambdada is for you !
|
4
|
+
|
5
|
+
Now, composition is simple as adding functions !
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install lambdada
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
**Composition:**
|
16
|
+
|
17
|
+
Now, you are able to compose lambdas by just adding them:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'lambdada'
|
21
|
+
|
22
|
+
ADD2 = ->(a) { a + 2 }
|
23
|
+
DIV2 = ->(a) { a / 2 }
|
24
|
+
|
25
|
+
ADD_2_AND_DIV_BY_2 = DIV2 + ADD2
|
26
|
+
|
27
|
+
puts ADD_2_AND_DIV_BY_2.call(6)
|
28
|
+
# => 4
|
29
|
+
```
|
30
|
+
|
31
|
+
Any lambda returning nil in the chain of composition is considered as
|
32
|
+
a side effect less monitoring function:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'lambdada'
|
36
|
+
|
37
|
+
PRINT = ->(*args) { puts(*args) }
|
38
|
+
SUM = ->(a, b) { a + b }
|
39
|
+
NEG = ->(a) { -a }
|
40
|
+
|
41
|
+
# That code set result to -5 and print 5
|
42
|
+
result = (NEG + PRINT + SUM)[2, 3]
|
43
|
+
|
44
|
+
```
|
45
|
+
**Currying:**
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
require 'lambdada'
|
49
|
+
|
50
|
+
DIV = -> (divisor, dividend) { dividend / divisor }
|
51
|
+
|
52
|
+
DIV_BY_2 = DIV[2]
|
53
|
+
|
54
|
+
puts DIV_BY_2.call(8)
|
55
|
+
# => 4
|
56
|
+
```
|
57
|
+
|
58
|
+
**Both at the same time, baby !**
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'lambdada'
|
62
|
+
|
63
|
+
SUM = ->(a, b) { a + b }
|
64
|
+
MAP = ->(func) { ->(*args) { args.map { |arg| func.call(arg) } } }
|
65
|
+
TO_I = ->(a) { a.to_i }
|
66
|
+
TO_S = ->(a) { a.to_s }
|
67
|
+
|
68
|
+
(SUM + MAP[TO_I])['1', 2.0, '3']
|
69
|
+
# => 6 (Integer)
|
70
|
+
|
71
|
+
(SUM + MAP[TO_S])[1, 2, '3']
|
72
|
+
# => '123' (String)
|
73
|
+
|
74
|
+
```
|
data/lambdada.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
4
|
+
require 'lambdada/version'
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'lambdada'
|
7
|
+
s.version = Lambdada::Version::STRING
|
8
|
+
s.summary = 'Ruby lambda composition (and currying) helpers'
|
9
|
+
s.description = 'Add less verbose function composition and currying ability to ruby.'
|
10
|
+
s.authors = ['Alexandre Ignjatovic']
|
11
|
+
s.email = 'alexandre.ignjatovic@gmail.com'
|
12
|
+
s.files = `git ls-files`.split($RS).reject do |file|
|
13
|
+
file =~ %r{^(?:
|
14
|
+
spec/.*
|
15
|
+
|.*\.swp
|
16
|
+
|Gemfile
|
17
|
+
|Rakefile
|
18
|
+
|\.rspec
|
19
|
+
|\.gitignore
|
20
|
+
|\.rubocop.yml
|
21
|
+
)$}x
|
22
|
+
end
|
23
|
+
s.require_paths = ['lib']
|
24
|
+
s.homepage = 'https://github.com/bankair/lambdada'
|
25
|
+
s.license = 'MIT'
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# Extend the Proc class to ease composition and curry usage
|
5
|
+
class Proc
|
6
|
+
##
|
7
|
+
# Compose two lambdas or proc into a third one.
|
8
|
+
def +(other)
|
9
|
+
proc { |*args| massala(*other.massala(*args) { args }) }
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Curry the current proc with one or several arguments.
|
14
|
+
# If the result of the operation is nil, return the alternate
|
15
|
+
# result provided in a block (if provided).
|
16
|
+
def massala(*args)
|
17
|
+
result = arity == -1 ? call(*args) : curry[*args]
|
18
|
+
result = block_given? ? yield : args if result.nil?
|
19
|
+
result
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :[], :massala
|
23
|
+
end
|
data/lib/lambdada.rb
ADDED
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lambdada
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandre Ignjatovic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Add less verbose function composition and currying ability to ruby.
|
14
|
+
email: alexandre.ignjatovic@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- Gemfile.lock
|
20
|
+
- README.md
|
21
|
+
- lambdada.gemspec
|
22
|
+
- lib/lambdada.rb
|
23
|
+
- lib/lambdada/proc.rb
|
24
|
+
- lib/lambdada/version.rb
|
25
|
+
homepage: https://github.com/bankair/lambdada
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.0.14
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Ruby lambda composition (and currying) helpers
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|