funk-rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 272a8ff8b3472a24d3e3230745e4aa4b02ad61b9
4
+ data.tar.gz: a414ef71aa521bf2e22655e72d137228e4535f2a
5
+ SHA512:
6
+ metadata.gz: 27146b1bd7b71c2aa1e94fec682dd1b759238ce13c7805528b8fc8fbd90d75d6696771098d90d6e4119b8eb76addd6d52c1698cac291f2a0bd2e761b9296c1e4
7
+ data.tar.gz: 5dd9ff379721a8007ec06f604a97cfd84afd5c986a14affab03a2f86244c7573284043688ced7751ecb2a219cf11d4303ae588705a6505433c5a492f2f56d718
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tony Schneider
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,63 @@
1
+ # Funk
2
+
3
+ Funk is a structured computation implementation inspired by Prismatic's [Graph](https://github.com/Prismatic/plumbing#graph-the-functional-swiss-army-knife) library.
4
+
5
+ ## Installation
6
+
7
+ _Note:_ This has not yet been released on rubygems, but will be shortly.
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'funk-rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install funk-rb
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ # compile a graph of functions which can be inter-dependent.
27
+ stats = Funk.compile(
28
+ count: -> (items) { items.length },
29
+ mean: -> (items, count) { (items.reduce(:+) / count).round(1) },
30
+ mean_sq: -> (items, count) { (items.reduce(1) { |m, i| m + i**2 } / count).round(1) },
31
+ variance: -> (mean, mean_sq) { (mean_sq - mean**2).round(1) })
32
+
33
+ # funk-rb figures out what order to call them in so dependencies are satisfied
34
+ stats.call(items: (1..10).to_a.map(&:to_f))
35
+ #=> {:items=>[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
36
+ # :count=>10,
37
+ # :mean=>5.5,
38
+ # :mean_sq=>38.6,
39
+ # :variance=>8.4}
40
+
41
+ # Can call the same function more than once with different input.
42
+ stats.call(items: [1,1,2,3,5,8,13].map(&:to_f))
43
+ #=> {:items=>[1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0],
44
+ # :count=>7,
45
+ # :mean=>4.7,
46
+ # :mean_sq=>39.1,
47
+ # :variance=>17.0}
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tonywok/funk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "funk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/funk.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'funk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "funk-rb"
8
+ spec.version = Funk::VERSION
9
+ spec.authors = ["Tony Schneider", "John Andrews"]
10
+ spec.email = ["tonywok@gmail.com", "john.m.andrews@gmail.com"]
11
+
12
+ spec.summary = %q{Structured computation graphs for ruby!}
13
+ spec.homepage = "https://github.com/tonywok/funk"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-nav"
26
+ end
data/lib/funk.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "funk/version"
2
+ require "funk/graph"
3
+ require "funk/evaluators/eager"
4
+
5
+ module Funk
6
+
7
+ def self.compile(fns, strategy: Funk::Evaluators::Eager, instruments: [])
8
+ graph = Graph.new(fns)
9
+ strategy.new(graph, instruments: instruments)
10
+ end
11
+
12
+ def self.compile_module(mod, **args)
13
+ dummy_receiver = Object.new
14
+ fn_hash = mod.instance_methods(false).each_with_object({}) do |meth, hash|
15
+ m = mod.instance_method(meth)
16
+ hash[meth] = m.bind(dummy_receiver)
17
+ end
18
+ compile(fn_hash, **args)
19
+ end
20
+
21
+ end
@@ -0,0 +1,42 @@
1
+ require "funk/result"
2
+
3
+ module Funk
4
+ module Evaluators
5
+ class Eager
6
+ attr_reader :graph
7
+
8
+ def initialize(graph, instruments: [])
9
+ @graph, @instruments = graph, instruments
10
+ end
11
+
12
+ def call(input)
13
+ instruments = @instruments.map{ |i| i.new }
14
+ @graph.tsort.each_with_object(Result.new(instruments)) do |fn, result|
15
+ result[fn.name] = evaluate(fn, result.merge(input), instruments)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def evaluate(fn, input, instruments)
22
+ before_call(fn, input, instruments)
23
+ value = fn.call(input)
24
+ after_call(fn, input, value, instruments)
25
+ value
26
+ end
27
+
28
+ def before_call(fn, input, instruments)
29
+ instruments.each do |inst|
30
+ inst.before_call(fn, input) if inst.respond_to?(:before_call)
31
+ end
32
+ end
33
+
34
+ def after_call(fn, input, value, instruments)
35
+ instruments.reverse.each do |inst|
36
+ inst.after_call(fn, input, value) if inst.respond_to?(:after_call)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
data/lib/funk/fn.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "set"
2
+ require "funk/missing_dependencies_exception"
3
+
4
+ module Funk
5
+ class Fn
6
+
7
+ REQUIRED_PARAM_TYPES = [:req, :keyreq].freeze
8
+
9
+ attr_reader :name
10
+ attr_reader :dependencies
11
+
12
+ def initialize(name, callable)
13
+ @name = name
14
+ @parameters = callable.parameters
15
+ @dependencies = @parameters.map(&:last)
16
+ @fn = callable
17
+ end
18
+
19
+ def call(hash)
20
+ assert_dependencies_present_in(hash)
21
+ args = extract_argument_values(hash)
22
+
23
+ @fn.call(*args)
24
+ end
25
+
26
+ private
27
+
28
+ def assert_dependencies_present_in(hash)
29
+ present_keys = Set.new(hash.keys)
30
+ deps = Set.new(dependencies)
31
+ unless deps.subset?(present_keys)
32
+ raise MissingDependenciesException, "Function #{name.inspect} is missing dependencies #{deps.difference(present_keys).to_a.inspect}"
33
+ end
34
+ end
35
+
36
+ # Check for required arguments which are missing from input values.
37
+ # If an argument is optional we don't raise, and we also don't use `nil`
38
+ # so that the proper default value will be used by the callable.
39
+ #
40
+ # One might be tempted to raise this dependency logic up a level
41
+ # but be warned: one Fn instance could require an argument which is
42
+ # optional in another Fn.
43
+ #
44
+ # NOTE: we don't currently support keyword args
45
+ def extract_argument_values(hash)
46
+ missing = []
47
+ args = []
48
+ @parameters.each do |(type, name)|
49
+ value = hash[name]
50
+ if value != Funk::NO_INPUT_PROVIDED
51
+ args << value
52
+ elsif REQUIRED_PARAM_TYPES.include?(type)
53
+ missing << name
54
+ end
55
+ end
56
+
57
+ if missing.empty?
58
+ args
59
+ else
60
+ raise MissingDependenciesException, "Function #{name.inspect} is missing dependencies #{missing.inspect}"
61
+ end
62
+ end
63
+ end
64
+ end
data/lib/funk/graph.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "tsort"
2
+ require "funk/fn"
3
+ require "funk/input_fn"
4
+
5
+ module Funk
6
+ class Graph
7
+
8
+ def initialize(map)
9
+ fn_map = map.each_with_object({}) do |(name, impl), fn_map|
10
+ fn = fn_map[name] = Fn.new(name, impl)
11
+ fn.dependencies.each do |dep_name|
12
+ fn_map[dep_name] ||= InputFn.new(dep_name)
13
+ end
14
+ end
15
+ accum = Hash.new { |h,k| h[k] = [] }
16
+ @nodes = fn_map.each_with_object(accum) do |(name, fn), graph|
17
+ graph[fn] = []
18
+ fn.dependencies.each do |dep_name|
19
+ graph[fn] << fn_map[dep_name]
20
+ end
21
+ end
22
+ end
23
+
24
+ # Topological Sort - exposes tsort enum
25
+ #
26
+ include TSort
27
+
28
+ def tsort_each_child(n, &b)
29
+ @nodes[n].each(&b)
30
+ end
31
+
32
+ def tsort_each_node(&b)
33
+ @nodes.each_key(&b)
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require "funk/fn"
2
+ require "funk/missing_dependencies_exception"
3
+
4
+ module Funk
5
+ NO_INPUT_PROVIDED = Object.new
6
+ def NO_INPUT_PROVIDED.inspect
7
+ "[not provided]"
8
+ end
9
+
10
+ class InputFn < Fn
11
+
12
+ def initialize(name, _=nil)
13
+ @name = name
14
+ @dependencies = []
15
+ end
16
+
17
+ def call(input)
18
+ if input.has_key?(name)
19
+ input[name]
20
+ else
21
+ NO_INPUT_PROVIDED
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ module Funk
2
+ module Instruments
3
+ class GraphViz
4
+ def initialize
5
+ @dots = []
6
+ end
7
+
8
+ def after_call(fn, input, value)
9
+ name = "node #{fn.name}"
10
+ style = {
11
+ "label" => "< #{fn.name} <br/> #{value.inspect} >",
12
+ "shape" => fn.is_a?(InputFn) ? "circle" : "box",
13
+ }
14
+ node = name + "[" + style.map { |attr,val| "#{attr}=#{val}" }.join(" ") + "]"
15
+
16
+ @dots << node
17
+ fn.dependencies.each do |dep|
18
+ edge = "#{dep} -> #{fn.name}"
19
+ @dots << edge
20
+ end
21
+ end
22
+
23
+ def digraph
24
+ digraph = []
25
+ digraph << "digraph {"
26
+ digraph.concat(@dots)
27
+ digraph << "}"
28
+ digraph.join("\n")
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ module Funk
2
+ module Instruments
3
+ class Timings
4
+ def initialize()
5
+ @timings = {}
6
+ end
7
+
8
+ def before_call(fn, input)
9
+ @timings[fn.name] = {:start => Time.now}
10
+ end
11
+
12
+ def after_call(fn, input, value)
13
+ @timings[fn.name][:end] = Time.now
14
+ end
15
+
16
+ def each
17
+ @timings.sort {|a,b|
18
+ a[1][:start] <=> b[1][:start]
19
+ }.each do |t|
20
+ yield t[0], t[1]
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module Funk
2
+ class MissingDependenciesException < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "delegate"
2
+
3
+ module Funk
4
+ class Result < Delegator
5
+ attr_reader :instruments
6
+
7
+ def initialize(instruments)
8
+ @instruments = instruments.each_with_object({}) do |inst, obj|
9
+ obj[inst.class.name.split("::").last] = inst
10
+ end
11
+ @result = {}
12
+ end
13
+
14
+ # delegate all other methods to @result hash
15
+ def __getobj__
16
+ @result
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Funk
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: funk-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Schneider
8
+ - John Andrews
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-09-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.10'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.10'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pry
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-nav
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description:
85
+ email:
86
+ - tonywok@gmail.com
87
+ - john.m.andrews@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - funk.gemspec
101
+ - lib/funk.rb
102
+ - lib/funk/evaluators/eager.rb
103
+ - lib/funk/fn.rb
104
+ - lib/funk/graph.rb
105
+ - lib/funk/input_fn.rb
106
+ - lib/funk/instruments/graph_viz.rb
107
+ - lib/funk/instruments/timings.rb
108
+ - lib/funk/missing_dependencies_exception.rb
109
+ - lib/funk/result.rb
110
+ - lib/funk/version.rb
111
+ homepage: https://github.com/tonywok/funk
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.8
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Structured computation graphs for ruby!
135
+ test_files: []