Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fns is now a required positional argument #10

Merged
merged 6 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ Or install it yourself as:
## Usage

```ruby
graph = Funk::Graph.new({
a: -> (b, d) { "#{b}, #{d}" },
b: -> (c, e) { "#{c}, #{e}" },
c: -> (d, e, h) { "#{d}, #{e}, #{h}" }
})

compiled_graph = graph.compile
compiled_graph.call({
d: "d",
e: "e",
h: "h",
})

# evaluates to:
#
# {
# :d=>"d",
# :e=>"e",
# :h=>"h",
# :c=>"d, e, h",
# :b=>"d, e, h, e",
# :a=>"d, e, h, e, d”,
# }
# compile a graph of functions which can be inter-dependent.
stats = Funk.compile(
count: -> (items) { items.length },
mean: -> (items, count) { (items.reduce(:+) / count).round(1) },
mean_sq: -> (items, count) { (items.reduce(1) { |m, i| m + i**2 } / count).round(1) },
variance: -> (mean, mean_sq) { (mean_sq - mean**2).round(1) }
)

# funk-rb figures out what order to call them in so dependencies are satisfied
stats.call(items: (1..10).to_a.map(&:to_f))
#=> {:items=>[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
:count=>10,
:mean=>5.5,
:mean_sq=>38.6,
:variance=>8.4}

# Can call the same function more than once with different input.
stats.call(items: [1,1,2,3,5,8,13].map(&:to_f))
#=> {:items=>[1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0],
:count=>7,
:mean=>4.7,
:mean_sq=>39.1,
:variance=>17.0}
```

## Development
Expand All @@ -62,4 +62,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/tonywo
## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

4 changes: 2 additions & 2 deletions lib/funk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Funk

def self.compile(fns: {}, strategy: Funk::Evaluators::Eager, instruments: [])
def self.compile(fns, strategy: Funk::Evaluators::Eager, instruments: [])
graph = Graph.new(fns)
strategy.new(graph, instruments: instruments)
end
Expand All @@ -15,7 +15,7 @@ def self.compile_module(mod, **args)
m = mod.instance_method(meth)
hash[meth] = m.bind(dummy_receiver)
end
compile(fns: fn_hash, **args)
compile(fn_hash, **args)
end

end
81 changes: 75 additions & 6 deletions test/funk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def sub_holdback(holdback, holdback_max)
end
end

describe "compile_function" do
describe "compile_module" do

it "works" do
policy = Funk.compile_module(Example)
Expand All @@ -35,10 +35,10 @@ def sub_holdback(holdback, holdback_max)
describe "compile" do

it "works" do
result = Funk.compile(fns: {
result = Funk.compile(
a: -> (b, c) { b + c },
b: -> (c) { c + 10 },
}).call(c: 2)
b: -> (c) { c + 10 }
).call(c: 2)

result[:a].must_equal(14)
result[:b].must_equal(12)
Expand All @@ -48,7 +48,7 @@ def sub_holdback(holdback, holdback_max)

it "self reference" do
identity = { a: -> (a) { a } }
policy = Funk.compile(fns: identity)
policy = Funk.compile(identity)
err = lambda { policy.call({}) }.must_raise(Funk::MissingDepenciesException)
err.message.must_match "Fn a is missing dependencies [:a]"
end
Expand All @@ -61,9 +61,78 @@ def sub_holdback(holdback, holdback_max)
a: -> (b) { b },
b: -> (a) { a },
}
policy = Funk.compile(fns: cycle)
policy = Funk.compile(cycle)
lambda { policy.call({}) }.must_raise(TSort::Cyclic)
end
end
end

describe "various graph scenarios" do

it "calculates statistics" do
stats = Funk.compile(
count: -> (items) { items.length },
mean: -> (items, count) { (items.reduce(:+) / count).round(1) },
mean_sq: -> (items, count) { (items.reduce(1) { |m, i| m + i**2 } / count).round(1) },
variance: -> (mean, mean_sq) { (mean_sq - mean**2).round(1) }
)
stats.call(items: [1.0,1,2,3,5,8,13]).must_equal(
items: [1,1,2,3,5,8,13],
count: 7,
mean: 4.7,
mean_sq: 39.1,
variance: 17.0
)
end

it "calculates a big, rather unordered tree" do
sums = Funk.compile(
a: -> (z, y) { z + y },
b: -> (a, y) { a + y },
c: -> (x, w) { x + w },
d: -> (a, c) { a + c },
e: -> (f, g) { f + g },
f: -> (w, x) { w + x },
g: -> (c, d) { c + d }
)

sums.call(w:1, x:2, y:3, z:4).must_equal(
a: 7,
b: 10,
c: 3,
d: 10,
e: 16,
f: 3,
g: 13,
w: 1,
x: 2,
y: 3,
z: 4
)
end

it "calculates a tree with multiple roots" do
sums = Funk.compile(
c: -> (a, b) { a + b },
d: -> (a, c) { a + c },
e: -> (b, c) { b + c },
f: -> (g, h) { g + h },
g: -> (h) { h**2 },
z: -> (x) { x + 1 }
)

sums.call(a:1, b:2, h:3, x:99).must_equal(
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 12,
g: 9,
h: 3,
x: 99,
z: 100
)
end
end
end
19 changes: 19 additions & 0 deletions test/graph_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

describe Funk::Graph do
it "orders functions in dependency order" do
graph = Funk::Graph.new(
a: -> (b, d) { "#{b}, #{d}" },
b: -> (c, e) { "#{c}, #{e}" },
c: -> (d, e, f) { "#{d}, #{e}, #{f}" },
g: -> (f) { "separate graph" },
)

seen = []

graph.tsort.each do |fn|
assert !seen.include?(fn.name)
seen += fn.dependencies
end
end
end
11 changes: 7 additions & 4 deletions test/instruments/timings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

describe Funk::Instruments::Timings do
it "profiles the execution of each computation" do
result = Funk.compile(fns: {
a: -> (b, c) { b + c },
b: -> (c) { c + 10 },
}, instruments: [Funk::Instruments::Timings]).call(c: 2)
graph = Funk.compile({
a: -> (b, c) { b + c },
b: -> (c) { c + 10 },
}, instruments: [Funk::Instruments::Timings])
result = graph.call(c: 2)

timings = result.instruments.first
timings.each do |name, timing|
assert [:a, :b, :c].include?(name)
assert_kind_of Time, timing[:start]
assert_kind_of Time, timing[:end]
end
Expand Down