proc_compose 0.0.1
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/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/ext/proc_compose/extconf.rb +3 -0
- data/ext/proc_compose/proc_compose.c +43 -0
- data/lib/.gemkeep +0 -0
- data/spec/proc_compose_spec.rb +39 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f7a14c61633e1d2e244be98c8d367a7a03d04c4
|
4
|
+
data.tar.gz: 7e8c52794af59f83019d44e30a9484f63e22c72e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd7767ba32aac4533938fecb932c5e2e5c99b046e9fe5a2856cf77c53f58ed7dd456518f6e3bfaa9492b35f1996504e68b9162a73d7d894afdac3462a8f3c3e0
|
7
|
+
data.tar.gz: 7f351dc65d0283dd381eef189420eea202c6f3da5fc56f70024d36c5fef7552e8e0b832a1deb3057c6ebda546f0f69988641c98a60c150fcaf09b6af743a029d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alex Moore - Niemi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
```
|
2
|
+
|
3
|
+
___|)___________________________________________________________
|
4
|
+
|___/____________________________________________________________
|
5
|
+
|__/|____________________________________________________________
|
6
|
+
|_/(|,\__________________________________________________________
|
7
|
+
|_\_|_/__________________________________________________________
|
8
|
+
| |
|
9
|
+
| (_|
|
10
|
+
|
|
11
|
+
|________________________________________________________________
|
12
|
+
|__/___\_._______________________________________________________
|
13
|
+
|__\___|_._______________________________________________________
|
14
|
+
|_____/__________________________________________________________
|
15
|
+
|____/___________________________________________________________ ejm
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
# Proc#compose
|
20
|
+
|
21
|
+
This gem does just one thing: it adds `compose` to `Proc`, with the alias `*`. Based on [this experiment](https://github.com/mooreniemi/compose). It performs just a _little_ worse than chaining `map`'d blocks, and currently does **not** [preserve lambdas](http://culttt.com/2015/05/13/what-are-lambdas-in-ruby/) ([they are coerced into `Proc`](spec/proc_compose_spec.rb#L35)).
|
22
|
+
|
23
|
+

|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'proc_compose'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install proc_compose
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
First things first:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'proc_compose'
|
47
|
+
```
|
48
|
+
|
49
|
+
Now, given two [Proc](https://ruby-doc.org/core-2.2.0/Proc.html) objects, say:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
double = proc {|a| a * 2 }
|
53
|
+
triple = proc {|a| a * 3 }
|
54
|
+
```
|
55
|
+
|
56
|
+
We can compose them:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
(double * triple).(2)
|
60
|
+
=> 12
|
61
|
+
```
|
62
|
+
|
63
|
+
So you can do things like this:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
[1, 2, 3, 4, 5].map(&(double * triple))
|
67
|
+
=> [6, 12, 18, 24, 30]
|
68
|
+
```
|
69
|
+
|
70
|
+
Neat!
|
71
|
+
|
72
|
+
## Alternatives
|
73
|
+
|
74
|
+
You can write your [own version in Ruby](http://mooreniemi.github.io/2016/09/18/combinators.html), or use [funkify](https://github.com/banister/funkify). Though its [performance is worse](https://github.com/mooreniemi/experiments/blob/master/lib/funkify.rb) it is pure Ruby and you get some really nice other stuff like `>=` and `|`!
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mooreniemi/proc_compose. 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.
|
79
|
+
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
84
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
* =====================================================================================
|
3
|
+
*
|
4
|
+
* Filename: Compose.c
|
5
|
+
*
|
6
|
+
* Description: Proc composition
|
7
|
+
*
|
8
|
+
* Version: 1.0
|
9
|
+
* Created: 10/01/2016 16:14:22
|
10
|
+
* Revision: none
|
11
|
+
* Compiler: gcc
|
12
|
+
*
|
13
|
+
* Author: Alex Moore-Niemi
|
14
|
+
*
|
15
|
+
* =====================================================================================
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <ruby.h>
|
19
|
+
|
20
|
+
void Init_proc_compose();
|
21
|
+
|
22
|
+
VALUE compose(VALUE arg, VALUE procs) {
|
23
|
+
VALUE first = rb_ary_entry(procs, 0);
|
24
|
+
VALUE second = rb_ary_entry(procs, 1);
|
25
|
+
|
26
|
+
VALUE first_result = rb_funcall(first, rb_intern("call"), 1, arg);
|
27
|
+
VALUE final_result = rb_funcall(second, rb_intern("call"), 1, first_result);
|
28
|
+
|
29
|
+
return final_result;
|
30
|
+
}
|
31
|
+
|
32
|
+
VALUE method_compose(VALUE self, VALUE other_proc) {
|
33
|
+
VALUE procs = other_proc;
|
34
|
+
procs = rb_ary_push(procs, self);
|
35
|
+
VALUE result = rb_proc_new(compose, procs);
|
36
|
+
|
37
|
+
return result;
|
38
|
+
}
|
39
|
+
|
40
|
+
void Init_proc_compose() {
|
41
|
+
rb_define_method(rb_cProc, "compose", method_compose, -2);
|
42
|
+
rb_define_method(rb_cProc, "*", method_compose, -2);
|
43
|
+
}
|
data/lib/.gemkeep
ADDED
File without changes
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Proc#compose' do
|
4
|
+
let(:double) do
|
5
|
+
proc {|a| a * 2 }
|
6
|
+
end
|
7
|
+
let(:triple) do
|
8
|
+
proc {|a| a * 3 }
|
9
|
+
end
|
10
|
+
let(:negate) do
|
11
|
+
proc {|a| -a }
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'lives on Proc' do
|
15
|
+
expect(double.respond_to?(:compose)).to eq(true)
|
16
|
+
expect(double.respond_to?(:*)).to eq(true)
|
17
|
+
end
|
18
|
+
it 'creates new Proc from 2 Procs' do
|
19
|
+
expect(double * triple).to be_a Proc
|
20
|
+
end
|
21
|
+
it 'compose Proc can be evaluated' do
|
22
|
+
expect((double * triple).(2)).to eq(triple.(double.(2)))
|
23
|
+
end
|
24
|
+
it 'compose on & on' do
|
25
|
+
expect((double * triple * negate).(2)).to eq(-12)
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'lambdas' do
|
29
|
+
it 'removes lambda information' do
|
30
|
+
a, b = [lambda {|x| x + 2 }, lambda {|y| y + 3 }]
|
31
|
+
# works by coercing to Proc
|
32
|
+
expect((a * b).(2)).to eq(7)
|
33
|
+
|
34
|
+
# if lambdas were preserved, this would be:
|
35
|
+
#expect{ (a * b).() }.to raise_error ArgumentError
|
36
|
+
expect{ (a * b).() }.to raise_error NoMethodError
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proc_compose
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Moore-Niemi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- moore.niemi@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/proc_compose/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- ext/proc_compose/extconf.rb
|
94
|
+
- ext/proc_compose/proc_compose.c
|
95
|
+
- lib/.gemkeep
|
96
|
+
- spec/proc_compose_spec.rb
|
97
|
+
homepage: https://github.com/mooreniemi/proc_compose
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.6.10
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Proc#compose (*) C extension
|
121
|
+
test_files:
|
122
|
+
- spec/proc_compose_spec.rb
|