matilda-function 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +10 -0
- data/LICENSE.md +7 -0
- data/README.md +50 -0
- data/Rakefile +9 -0
- data/lib/function.rb +45 -0
- data/matilda-function.gemspec +15 -0
- data/spec/function_spec.rb +37 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a08ee980eee81813383e4bac42692571d0b6881d
|
4
|
+
data.tar.gz: 337597ef7d2048f3d285ca2a2804d91020eeb088
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5528a2b671640eea62b49e9a783fbcb261e01c0e44fde7d75f81799dd1febb97aa1b227525386b3f56022d67b46d7b0208b55c7280791cd3abe4236e387f78e2
|
7
|
+
data.tar.gz: c078d51680e8883407e4328960d115c477471580187a5d3350850a97add3b18268edc0c999e5d923e36a79060f3c7dd00f87f24ff78efa55fc425dd3895ec024
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
The MIT License (MIT) Copyright (c) Callum Stott, http://www.seadowg.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#matilda-function
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/seadowg/matilda-function.png?branch=master)](https://travis-ci.org/seadowg/matilda-function)
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
This library provides extensions to Ruby's Proc to allow for some functional goodness.
|
8
|
+
|
9
|
+
### Composition
|
10
|
+
|
11
|
+
So we can pretend we are coding in Haskell we use the `<<` operator for composition on Procs:
|
12
|
+
|
13
|
+
sort = proc { |array| array.sort }
|
14
|
+
reverse = proc { |array| array.reverse }
|
15
|
+
reverese_order = sort << reverse
|
16
|
+
reverese_order.call [3,1,4,8] #=> [8,4,3,1]
|
17
|
+
|
18
|
+
When we combine two Procs using `<<` we create a new Proc that will first execute the receiver Proc and
|
19
|
+
then pass the result to the argument Proc. This provides an expressive and modular approach to creating complex
|
20
|
+
high order functons in Ruby.
|
21
|
+
|
22
|
+
### Recursive
|
23
|
+
|
24
|
+
Recursive calls are pretty easy in Ruby: lambdas, Procs and methods can
|
25
|
+
all reference themselves. However, these recursive calls will deepen the
|
26
|
+
Ruby call stack. Enter the `recursive` method:
|
27
|
+
|
28
|
+
recursive { |this|
|
29
|
+
this.call
|
30
|
+
}
|
31
|
+
|
32
|
+
Blocks passed to `recursive` will unroll returning recursive calls so
|
33
|
+
that the recursion can execute without eating up the Ruby call stack.
|
34
|
+
This allows for infinitely recursive operations. Here's a quick example:
|
35
|
+
|
36
|
+
inf_marco = recursive { |this, shout|
|
37
|
+
puts shout
|
38
|
+
|
39
|
+
if shout == "Marco"
|
40
|
+
this.call("Polo")
|
41
|
+
else
|
42
|
+
this.call("Marco")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
recursive.call("Marco") # => "Marco"
|
47
|
+
# => "Polo"
|
48
|
+
# => "Marco"
|
49
|
+
# => "Polo"
|
50
|
+
# ...
|
data/Rakefile
ADDED
data/lib/function.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Proc
|
2
|
+
def self.recursive(&block)
|
3
|
+
Proc.new do |args|
|
4
|
+
step = RecursiveStep.new
|
5
|
+
returned = block.call(step, *args)
|
6
|
+
|
7
|
+
while returned.kind_of?(RecursiveStep)
|
8
|
+
returned = block.call(step, *returned.args)
|
9
|
+
end
|
10
|
+
|
11
|
+
returned
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def <<(func)
|
16
|
+
Proc.new do |*args|
|
17
|
+
func.call(self.call(*args))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
class RecursiveStep
|
24
|
+
attr_reader :args
|
25
|
+
|
26
|
+
def call(*args)
|
27
|
+
@args = args
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module RecursiveExtension
|
34
|
+
def self.extended(mod)
|
35
|
+
mod.module_eval do
|
36
|
+
def recursive(&block)
|
37
|
+
Proc.recursive(&block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module Kernel
|
44
|
+
extend RecursiveExtension
|
45
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
lib = File.expand_path('../lib/', __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "matilda-function"
|
6
|
+
s.version = "0.1.0"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Callum Stott"]
|
9
|
+
s.email = ["callum@seadowg.com"]
|
10
|
+
s.summary = "Enhancement to Ruby Procs to allow for some wholesome Functional goodness."
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.require_paths = ['lib']
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'function'
|
3
|
+
|
4
|
+
describe "recursive" do
|
5
|
+
it "creates a recursive function" do
|
6
|
+
func = recursive { |this, i|
|
7
|
+
if i < 1
|
8
|
+
i
|
9
|
+
else
|
10
|
+
this.call(i - 1)
|
11
|
+
end
|
12
|
+
}
|
13
|
+
|
14
|
+
func.call(10).must_equal(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns an instance of Proc" do
|
18
|
+
func = recursive { |this| this.call }
|
19
|
+
func.kind_of?(Proc).must_equal true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "Proc" do
|
24
|
+
describe "#<<" do
|
25
|
+
it "returns a new Proc" do
|
26
|
+
func = Proc.new {} << Proc.new {}
|
27
|
+
func.kind_of?(Proc).must_equal true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "executing the result chains argument and receiver" do
|
31
|
+
sort = Proc.new { |array| array.sort }
|
32
|
+
reverse = Proc.new { |array| array.reverse }
|
33
|
+
|
34
|
+
(sort << reverse).call([1,4,2,3]).must_equal [4,3,2,1]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matilda-function
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Callum Stott
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- callum@seadowg.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .travis.yml
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- LICENSE.md
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/function.rb
|
27
|
+
- matilda-function.gemspec
|
28
|
+
- spec/function_spec.rb
|
29
|
+
homepage:
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.0.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Enhancement to Ruby Procs to allow for some wholesome Functional goodness.
|
53
|
+
test_files: []
|