arrows 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +2 -0
- data/arrows.gemspec +25 -0
- data/lib/arrows.rb +45 -0
- data/lib/arrows/version.rb +3 -0
- data/spec/arrows/proc_spec.rb +30 -0
- data/spec/spec_helper.rb +2 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3959c84c04b961af219c9ebace94e96b8dee661e
|
4
|
+
data.tar.gz: 1f8938d020276560369e30f791a8ec7607c9e267
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1c511982b5353cce121f0d99783d04a93a7760dfcad30b2e50308c4dba5edf94b52d27928a5a9d0b2c96363c246cce7e552a13ec4ca3b5e7189d940576e6589
|
7
|
+
data.tar.gz: 6d56bc8cbce8fa1d48ff1ab14a0cdcdf3195f952b28f6147941e9b0cb520c34dd2611362a18f04bd0a9d2438037231724d3d5dab8f12f4ad5fe1e235284e6bd1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Thomas Chen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Arrows
|
2
|
+
|
3
|
+
A library to bring some of the best elements of Haskell functional programming into Ruby.
|
4
|
+
|
5
|
+
Except, where Haskell is overly general and nigh unapproachable, here I try to build an
|
6
|
+
actually useful set of functional tools for day-to-day Ruby programming
|
7
|
+
|
8
|
+
Features:
|
9
|
+
|
10
|
+
### Function composition
|
11
|
+
|
12
|
+
If given
|
13
|
+
x -> F -> y and y -> G -> z
|
14
|
+
|
15
|
+
Returns
|
16
|
+
x -> H -> z
|
17
|
+
|
18
|
+
As in we pipe what F poops out into the mouth of G a la Human Centipede
|
19
|
+
|
20
|
+
### Applicative composition
|
21
|
+
Calls map (in Haskell, they generalize it to fmap) on the data passed in
|
22
|
+
|
23
|
+
So... If given
|
24
|
+
x -> F -> y and [x]
|
25
|
+
|
26
|
+
Returns
|
27
|
+
[x] -> F -> [z]
|
28
|
+
|
29
|
+
### Arrow fan out
|
30
|
+
x -> y
|
31
|
+
x -> z
|
32
|
+
becomes
|
33
|
+
x -> [y,z]
|
34
|
+
|
35
|
+
### Arrow split
|
36
|
+
Not implemented
|
37
|
+
|
38
|
+
## Use Case
|
39
|
+
Suppose you're running rails (lol what else is there in ruby?) for some sort of ecommerce app and you have an OfferController that handles inputs from an user who is trying to make an offer on some listing you have. Your controller might look like this:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class OfferController < ApplicationController
|
43
|
+
rescue_from ActiveRecord::HasCancer
|
44
|
+
def create
|
45
|
+
if user_signed_in?
|
46
|
+
offer = current_user.offers.new offer_params
|
47
|
+
else
|
48
|
+
user = User.create! offer_params[:user]
|
49
|
+
login_in(user)
|
50
|
+
offer = user.offers.new offer_params
|
51
|
+
end
|
52
|
+
if offer.valid?
|
53
|
+
if offer.save!
|
54
|
+
mail = OfferNotificationMailer.new_offer offer
|
55
|
+
mail.deliver!
|
56
|
+
render offer
|
57
|
+
end
|
58
|
+
else
|
59
|
+
if ...cancer
|
60
|
+
end
|
61
|
+
end
|
62
|
+
private
|
63
|
+
def stuff
|
64
|
+
...
|
65
|
+
end
|
66
|
+
|
67
|
+
class Offer < ActiveRecord::Base
|
68
|
+
...
|
69
|
+
after_create :queue_some_job, :dispatch_invoice
|
70
|
+
...
|
71
|
+
end
|
72
|
+
```
|
73
|
+
If it does, I'm sorry to inform you, but your codebase has cancer. Functional Arrows can help resolve this by linearization your currently highly nonlinear Object-Oriented business logic
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class OfferController < ApplicationController
|
77
|
+
def create
|
78
|
+
offer_creation_process.call
|
79
|
+
end
|
80
|
+
private
|
81
|
+
def offer_creation_process
|
82
|
+
offer_params >> initialize_user >> initialize_new_offer >> persist_offer >> deliver_mail / render_view
|
83
|
+
end
|
84
|
+
def offer_params
|
85
|
+
...
|
86
|
+
end
|
87
|
+
def initialize_user
|
88
|
+
...
|
89
|
+
end
|
90
|
+
...
|
91
|
+
end
|
92
|
+
```
|
93
|
+
That is, offer creation has been reduced back down to a process with distinct steps.
|
94
|
+
## Installation
|
95
|
+
|
96
|
+
Add this line to your application's Gemfile:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
gem 'arrows'
|
100
|
+
```
|
101
|
+
|
102
|
+
And then execute:
|
103
|
+
|
104
|
+
$ bundle
|
105
|
+
|
106
|
+
Or install it yourself as:
|
107
|
+
|
108
|
+
$ gem install arrows
|
109
|
+
|
110
|
+
## Usage
|
111
|
+
|
112
|
+
TODO: Write usage instructions here
|
113
|
+
|
114
|
+
## Contributing
|
115
|
+
|
116
|
+
1. Fork it ( https://github.com/[my-github-username]/arrows/fork )
|
117
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
118
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
120
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/arrows.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'arrows/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "arrows"
|
8
|
+
spec.version = Arrows::VERSION
|
9
|
+
spec.authors = ["Thomas Chen"]
|
10
|
+
spec.email = ["foxnewsnetwork@gmail.com"]
|
11
|
+
spec.summary = %q{Functional programming with composable, applicable, and arrowable functions.}
|
12
|
+
spec.description = %q{Introduces a Arrows::Proc class which allows for piping processes.}
|
13
|
+
spec.homepage = "http://github.com/foxnewsnetwork/arrows"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", ">=3.1"
|
24
|
+
spec.add_dependency "activesupport", ">=2.3"
|
25
|
+
end
|
data/lib/arrows.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "arrows/version"
|
2
|
+
|
3
|
+
module Arrows
|
4
|
+
class << self
|
5
|
+
def fanout(f,g)
|
6
|
+
Arrows::Proc.new { |*args| [f[*args], g[*args]] }
|
7
|
+
end
|
8
|
+
def compose(f,g)
|
9
|
+
Arrows::Proc.new { |*args| g[*f[*args]] }
|
10
|
+
end
|
11
|
+
def fmap(xs, f)
|
12
|
+
Arrows::Proc.new { |*args| xs[*args].map { |*x| f[*x] } }
|
13
|
+
end
|
14
|
+
def lift(x)
|
15
|
+
return x if arrow_like? x
|
16
|
+
return wrap_proc x if proc_like? x
|
17
|
+
Arrows::Proc.new { |*args| x }
|
18
|
+
end
|
19
|
+
def arrow_like?(x)
|
20
|
+
proc_like?(x) && x.arity == -1
|
21
|
+
end
|
22
|
+
def proc_like?(x)
|
23
|
+
x.respond_to?(:call) && x.respond_to?(:arity)
|
24
|
+
end
|
25
|
+
def wrap_proc(f)
|
26
|
+
Arrows::Proc.new { |*args| f[*args] }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
class Proc < ::Proc
|
30
|
+
# applicative fmap
|
31
|
+
def >=(f)
|
32
|
+
Arrows.fmap self, Arrows.lift(f)
|
33
|
+
end
|
34
|
+
|
35
|
+
# standard composition
|
36
|
+
def >>(f)
|
37
|
+
Arrows.compose self, Arrows.lift(f)
|
38
|
+
end
|
39
|
+
|
40
|
+
# fanout composition
|
41
|
+
def /(f)
|
42
|
+
Arrows.fanout self, Arrows.lift(f)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Arrows::Proc do
|
4
|
+
context '>> composition' do
|
5
|
+
let(:times2) { -> (x) { x * 2 } }
|
6
|
+
let(:plus3) { -> (x) { x + 3 } }
|
7
|
+
let(:four) { Arrows.lift 4 }
|
8
|
+
let(:fourteen) { four >> plus3 >> times2 }
|
9
|
+
subject { fourteen.call }
|
10
|
+
specify { should eq 14 }
|
11
|
+
end
|
12
|
+
|
13
|
+
context '>= application' do
|
14
|
+
let(:times2) { -> (x) { x * 2 } }
|
15
|
+
let(:plus3) { -> (x) { x + 3 } }
|
16
|
+
let(:twos) { Arrows.lift [2,3,4] }
|
17
|
+
let(:tens) { twos >= plus3 >= times2 }
|
18
|
+
subject { tens.call }
|
19
|
+
specify { should eq [10, 12, 14] }
|
20
|
+
end
|
21
|
+
|
22
|
+
context '+ fanout' do
|
23
|
+
let(:times2) { Arrows.lift -> (x) { x * 2 } }
|
24
|
+
let(:plus3) { Arrows.lift -> (x) { x + 3 } }
|
25
|
+
let(:four) { Arrows.lift 4 }
|
26
|
+
let(:six_eight) { four >> times2 / plus3 }
|
27
|
+
subject { six_eight.call }
|
28
|
+
specify { should eq [8, 7] }
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arrows
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-04 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.3'
|
69
|
+
description: Introduces a Arrows::Proc class which allows for piping processes.
|
70
|
+
email:
|
71
|
+
- foxnewsnetwork@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- arrows.gemspec
|
83
|
+
- lib/arrows.rb
|
84
|
+
- lib/arrows/version.rb
|
85
|
+
- spec/arrows/proc_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: http://github.com/foxnewsnetwork/arrows
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Functional programming with composable, applicable, and arrowable functions.
|
111
|
+
test_files:
|
112
|
+
- spec/arrows/proc_spec.rb
|
113
|
+
- spec/spec_helper.rb
|