Permalink
Browse files

Merge branch 'cljs-jar'

  • Loading branch information...
2 parents c899f47 + 68e18db commit 84d5235eaa699e60759ea3433a1b99fd74d7ed16 @lynaghk committed Feb 9, 2012
View
@@ -1,3 +1,7 @@
out/
vendor/closure-compiler.jar
manifest.MF
+.cake/
+classes/
+lib/
+pom.xml
View
31 LICENSE
@@ -0,0 +1,31 @@
+Original Cassowary under LGPL.
+
+
+CoffeeScript port + Clojure bindings:
+
+Copyright (c) 2012, Kevin Lynagh
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+* The name Kevin Lynagh may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL KEVIN LYNAGH BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -0,0 +1,11 @@
+java -jar vendor/closure-compiler.jar \
+ --js_output_file runme.js \
+ --compilation_level ADVANCED_OPTIMIZATIONS \
+ --manage_closure_dependencies true \
+ --js $(find out/js/ -name '*.js') \
+ --js vendor/base.js \
+ --js vendor/underscore.js \
+ --js vendor/jshashtable.js \
+ --js vendor/jshashset.js \
+ --js grr.js
+
View
@@ -28,6 +28,7 @@ rm -rf $COMPILED_FILE $JS_OUT
vendor/coffee-script/bin/coffee \
--google \
--compile \
+ --bare \
--output $JS_OUT $COFFEE_IN
#Run Closure Compiler
View
@@ -0,0 +1,4 @@
+(defproject com.keminglabs/cassowary "0.0.1-SNAPSHOT"
+ :description "Cassowary linear constraint solver with ClojureScript bindings"
+ :source-path "src/cljs"
+ :compile-path "out/js")
@@ -0,0 +1,29 @@
+(ns cassowary.core
+ (:refer-clojure :exclude [+ - =]))
+
+(defn cvar
+ ([] (cvar 0))
+ ([val] (js/Cl.Variable. (js/parseFloat val))))
+
+(defn simplex-solver [] (js/Cl.SimplexSolver.))
+
+(defn constrain! [solver constraint] (.addConstraint solver constraint))
+(defn stay! [solver cvar] (.addStay solver cvar))
+
+(defn contains-cassowary? [& args]
+ (if (some #(or (instance? js/Cl.Variable %)
+ (instance? js/Cl.LinearExpression %)) args)
+ :cassowary-var
+ :number))
+
+(defmulti + contains-cassowary?)
+(defmulti - contains-cassowary?)
+(defmulti = contains-cassowary?)
+
+(defmethod + :number [& args] (apply clojure.core/+ args))
+(defmethod = :number [& args] (apply clojure.core/= args))
+(defmethod - :number [& args] (apply clojure.core/- args))
+
+(defmethod + :cassowary-var [& args] (apply js/Cl.CL.Plus args))
+(defmethod = :cassowary-var [a b] (js/Cl.LinearEquation. a b))
+(defmethod - :cassowary-var [& args] (apply js/Cl.CL.Minus args))
View
@@ -1,14 +1,13 @@
#Global Cassowary functions
include Cl.Variable as Variable
-goog.provide "Cl.CL"
#Times, Plus, and Minus functions from the original Cassowary JavaScript port are mixed into the Cl.CL object after LinearExpression is defined.
-Cl.CL =
- GEQ: 1
- LEQ: 2
- Assert: (bool) -> throw "Nope." unless bool
- approx: (a, b) ->
+class Cl.CL
+ @GEQ: 1
+ @LEQ: 2
+ @Assert: (bool) -> throw "Nope." unless bool
+ @approx: (a, b) ->
a = a.value() if a instanceof Variable
b = b.value() if b instanceof Variable
epsilon = 1e-8
@@ -19,7 +18,7 @@ Cl.CL =
else
Math.abs(a - b) < Math.abs(a)*epsilon
- hashToString: (h) ->
+ @hashToString: (h) ->
answer = ""
h.each (k,v) ->
answer += k + " => "
@@ -31,7 +30,7 @@ Cl.CL =
v + "\n"
return answer
- setToString: (s) ->
+ @setToString: (s) ->
answer = s.size() + " {"
first = true
s.each (e) ->
@@ -42,8 +41,3 @@ Cl.CL =
answer += e
answer += "}\n"
return answer
-
-goog.exportSymbol "Cl", Cl
-goog.exportSymbol "Cl.CL", Cl.CL
-goog.exportSymbol "Cl.CL.Plus", Cl.CL.Plus
-
@@ -0,0 +1,52 @@
+#Mixin CL singleton functions that rely on LinearExpression
+include Cl.LinearExpression as LinearExpression
+include Cl.Variable as Variable
+include Cl.CL as CL
+
+CL["Times"] = (e1, e2) ->
+ if e1 instanceof LinearExpression and
+ e2 instanceof LinearExpression
+ e1.times e2
+ else if e1 instanceof LinearExpression and
+ e2 instanceof Variable
+ e1.times new LinearExpression(e2)
+ else if e1 instanceof Variable and
+ e2 instanceof LinearExpression
+ (new LinearExpression(e1)).times e2
+ else if e1 instanceof LinearExpression and
+ typeof (e2) is "number"
+ e1.times new LinearExpression(e2)
+ else if typeof (e1) is "number" and
+ e2 instanceof LinearExpression
+ (new LinearExpression(e1)).times e2
+ else if typeof (e1) is "number" and
+ e2 instanceof Variable
+ new LinearExpression(e2, e1)
+ else if e1 instanceof Variable and
+ typeof (e2) is "number"
+ new LinearExpression(e1, e2)
+ else if e1 instanceof Variable and
+ e2 instanceof LinearExpression
+ new LinearExpression(e2, n)
+
+CL["Linify"] = (x) ->
+ if x instanceof LinearExpression
+ x
+ else
+ new LinearExpression(x)
+
+CL["Plus"] = ->
+ if arguments.length == 0
+ new LinearExpression(0)
+ else
+ _(arguments).chain()
+ .map(CL["Linify"])
+ .reduce((sum, v) -> sum.plus v)
+ .value()
+
+CL["Minus"] = ->
+ switch(arguments.length)
+ when 0 then new LinearExpression 0
+ when 1 then CL["Linify"](arguments[0]).times -1
+ else
+ CL["Linify"](arguments[0]).minus CL["Plus"].apply null, _.rest arguments
@@ -1,3 +1,4 @@
#Setup base namespace.
-#Not sure how else to do this.
-class Cl
+goog.provide("Cl")
+
+Cl.version = "0.0.1"
View
@@ -1,5 +1,4 @@
-goog.provide "Cl.errors"
-Cl.errors = {}
+class Cl.errors
class Cl.errors.Error
this.prototype = new Error()
@@ -1,4 +1,3 @@
-include Cl
include Cl.CL as CL
include Cl.Constraint
include Cl.Strength
@@ -1,9 +1,7 @@
-include Cl
include Cl.CL as CL
include Cl.AbstractVariable as AbstractVariable
include Hashtable
-include underscore._ as _
class Cl.LinearExpression
@@ -144,64 +142,3 @@ class Cl.LinearExpression
bstr += coeff + "*" + clv
needsplus = true
return bstr
-
-
-#Mixin CL singleton functions that rely on LinearExpression
-LinearExpression = Cl.LinearExpression
-include Cl.Variable as Variable
-
-CL["Times"] = (e1, e2) ->
- if e1 instanceof LinearExpression and
- e2 instanceof LinearExpression
- e1.times e2
- else if e1 instanceof LinearExpression and
- e2 instanceof Variable
- e1.times new LinearExpression(e2)
- else if e1 instanceof Variable and
- e2 instanceof LinearExpression
- (new LinearExpression(e1)).times e2
- else if e1 instanceof LinearExpression and
- typeof (e2) is "number"
- e1.times new LinearExpression(e2)
- else if typeof (e1) is "number" and
- e2 instanceof LinearExpression
- (new LinearExpression(e1)).times e2
- else if typeof (e1) is "number" and
- e2 instanceof Variable
- new LinearExpression(e2, e1)
- else if e1 instanceof Variable and
- typeof (e2) is "number"
- new LinearExpression(e1, e2)
- else if e1 instanceof Variable and
- e2 instanceof LinearExpression
- new LinearExpression(e2, n)
-
-lin = (x) ->
- if x instanceof LinearExpression
- x
- else
- new LinearExpression(x)
-
-CL["Plus"] = ->
- if arguments.length == 0
- new LinearExpression(0)
- else
- _(arguments).chain()
- .map(lin)
- .reduce((sum, v) -> sum.plus v)
- .value()
-
-CL["Minus"] = ->
- switch(arguments.length)
- when 0 then new LinearExpression 0
- when 1 then lin(arguments[0]).times -1
- else
- lin(arguments[0]).minus CL["Plus"].apply null, _.rest arguments
-
-goog.exportSymbol "Cl.LinearExpression", Cl.LinearExpression
-# goog.exportSymbol "LinearExpression.prototype.times", Cl.LinearExpression.prototype.times
-# goog.exportSymbol "LinearExpression.prototype.plus", Cl.LinearExpression.prototype.plus
-# goog.exportSymbol "LinearExpression.prototype.minus", Cl.LinearExpression.prototype.minus
-# goog.exportSymbol "LinearExpression.prototype.divide", Cl.LinearExpression.prototype.divide
-
-# goog.exportSymbol "LinearExpression.prototype.toString", Cl.LinearExpression.prototype.toString
View
@@ -1,8 +0,0 @@
-#Ensure that these modules are picked up by Closure during compilation
-
-include Cl.CL as CL
-include Cl.LinearExpression
-include Cl.LinearEquation
-include Cl.LinearInequality
-include Cl.Variable
-include Cl.SimplexSolver
@@ -8,7 +8,7 @@ include Cl.errors
include Cl.Tableau
include Cl.SlackVariable
include Cl.DummyVariable
-include underscore._ as _
+include _
class Cl.SimplexSolver extends Cl.Tableau
constructor: ->
@@ -1,4 +1,3 @@
-include Cl
include Cl.SymbolicWeight
class Cl.Strength
@@ -1,4 +1,3 @@
-include Cl
class Cl.SymbolicWeight
constructor: (w1, w2, w3) ->
@_values = new Array(w1, w2, w3)
@@ -1,4 +1,3 @@
-include Cl
include Cl.CL as CL
include Hashtable
include HashSet
@@ -1,4 +1,3 @@
-include Cl
class Cl.AbstractVariable
constructor: (a1, a2) ->
@hash_code = Cl.AbstractVariable.iVariableNumber++
@@ -46,11 +45,6 @@ class Cl.Variable extends Cl.AbstractVariable
setAttachedObject: (@_attachedObject) ->
getAttachedObject: -> @_attachedObject
-#Static
-Cl.Variable.setVarMap = (@_ourVarMap) ->
-Cl.Variable.getVarMap = (map) -> @_ourVarMap
-
-
class Cl.DummyVariable extends Cl.AbstractVariable
constructor: (name_or_val, prefix) -> super name_or_val, prefix
isDummy: -> true
Oops, something went wrong.

0 comments on commit 84d5235

Please sign in to comment.