nelson 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f04c915e0ae0738b930c7fd8086dd7ae42c50f0
4
- data.tar.gz: 4563a129f03d9a6ff265892da1547ffce1c3402e
3
+ metadata.gz: fe237670123049b19a549ca9fbb13e9e3a8ad7dd
4
+ data.tar.gz: f306e36af7f6d2c5cebabc1726fa92e53eae712e
5
5
  SHA512:
6
- metadata.gz: f1b262c5eac526586baef0301e6a405841262766e238d25ec5731766d8f34a244780f5a5519acf115d8b40abe47618865c8d115caf882852ba024fcf413edc1f
7
- data.tar.gz: 2b6b24a23e9b89973836d97face4dcf68f7567b55b2a255130e61e151ddf4445a8143c80524ad327d039f2691a704cbe531342959cd66968309de07ab0d7e96d
6
+ metadata.gz: 95d5a5da40ff327ff1ee006b764e0375de747e947bdeddee81b10ccd358e90566478eab1ad71125a00d1b4dab2a31598babf87b3a48d57258aa72b827087185f
7
+ data.tar.gz: 1b9e0f1ecf82f66514d483dbe89a1d3a89f1ab38db584d39efcdf92cbbbbef8a46a0bf4ed3c6b896f1682da011edeea545f1c9730d172e1323e8c8fb3c4c1962
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Nelson
2
+ [![Gem Version](https://badge.fury.io/rb/nelson.svg)](http://badge.fury.io/rb/nelson)
2
3
 
3
- A library for building, manipulating, displaying, and soling math expressions using a fluent API.
4
+ A library for building, manipulating, displaying, and solving math expressions using a fluent API.
4
5
 
5
6
  ## Installation
6
7
 
@@ -1,3 +1,3 @@
1
1
  module Nelson
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/nelson.rb CHANGED
@@ -1,5 +1,97 @@
1
1
  require "nelson/version"
2
2
 
3
3
  module Nelson
4
- # Your code goes here...
4
+ def self.product_of(*terms)
5
+ MultipicationExpressionBuilder.new(*terms)
6
+ end
7
+
8
+ def self.sum_of(*terms)
9
+ AdditionExpressionBuilder.new(*terms)
10
+ end
11
+
12
+
13
+ class Expression
14
+ attr_accessor :terms
15
+
16
+ def initialize(*terms)
17
+ @terms = terms
18
+ end
19
+
20
+ def *(term)
21
+ rhs_value = self.call
22
+ lhs_value = term.respond_to?(:call) ? term.call : term
23
+ MultipicationExpression.new(rhs_value, lhs_value).call
24
+ end
25
+
26
+ def +(term)
27
+ rhs_value = self.call
28
+ lhs_value = term.respond_to?(:call) ? term.call : term
29
+ AdditionExpression.new(rhs_value, lhs_value).call
30
+ end
31
+ end
32
+
33
+ class ExpressionBuilder
34
+ attr_accessor :terms
35
+
36
+ def initialize(*terms)
37
+ @terms = terms
38
+ end
39
+
40
+ def and(term)
41
+ terms << term
42
+ return self
43
+ end
44
+
45
+ def times(term)
46
+ MultipicationExpressionBuilder.new(self, term)
47
+ end
48
+
49
+ def plus(term)
50
+ AdditionExpressionBuilder.new(self, term)
51
+ end
52
+ end
53
+
54
+ class MultipicationExpression < Expression
55
+ def call
56
+ terms.reduce(:*)
57
+ end
58
+
59
+ def to_s
60
+ terms.sort_by { |t| t.is_a?(Expression) ? 1 : 0 }.reduce("") do |c, t|
61
+ case t
62
+ when Expression
63
+ c << "(#{t})"
64
+ else
65
+ c = [c, t].map(&:to_s).reject(&:empty?).join("*")
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ class MultipicationExpressionBuilder < ExpressionBuilder
72
+ def build
73
+ terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
74
+ MultipicationExpression.new(*terms)
75
+ end
76
+ end
77
+
78
+ class AdditionExpression < Expression
79
+ def call
80
+ terms.reduce(:+)
81
+ end
82
+
83
+
84
+ def to_s
85
+ terms.map do |t|
86
+ t.is_a?(Expression) ? "(#{t})" : t
87
+ end.join("+")
88
+ end
89
+ end
90
+
91
+ class AdditionExpressionBuilder < ExpressionBuilder
92
+ def build
93
+ terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
94
+ AdditionExpression.new(*terms)
95
+ end
96
+ end
5
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nelson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Metcalfe