Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore examples lost during modules split, a6a92b9
- Loading branch information
Showing
8 changed files
with
1,194 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
modules/accumulators/src/examples/clojure/examples/accumulators.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; | ||
;; Accumulator application examples | ||
;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(ns | ||
#^{:author "Konrad Hinsen" | ||
:skip-wiki true | ||
:doc "Examples for using accumulators"} | ||
examples.accumulators | ||
(:use [clojure.contrib.accumulators | ||
:only (combine add add-items | ||
empty-vector empty-list empty-queue empty-set empty-map | ||
empty-counter empty-counter-with-total | ||
empty-sum empty-product empty-maximum empty-minimum | ||
empty-min-max empty-mean-variance empty-string empty-tuple)])) | ||
|
||
; Vector accumulator: combine is concat, add is conj | ||
(combine [:a :b] [:c :d] [:x :y]) | ||
(add [:a :b] :c) | ||
(add-items empty-vector [:a :b :a]) | ||
|
||
; List accumulator: combine is concat, add is conj | ||
(combine '(:a :b) '(:c :d) '(:x :y)) | ||
(add '(:a :b) :c) | ||
(add-items empty-list [:a :b :a]) | ||
|
||
; Queue accumulator | ||
(let [q1 (add-items empty-queue [:a :b :a]) | ||
q2 (add-items empty-queue [:x :y])] | ||
(combine q1 q2)) | ||
|
||
; Set accumulator: combine is union, add is conj | ||
(combine #{:a :b} #{:c :d} #{:a :d}) | ||
(add #{:a :b} :c) | ||
(add-items empty-set [:a :b :a]) | ||
|
||
; Map accumulator: combine is merge, add is conj | ||
(combine {:a 1} {:b 2 :c 3} {}) | ||
(add {:a 1} [:b 2]) | ||
(add-items empty-map [[:a 1] [:b 2] [:a 0]]) | ||
|
||
; Counter accumulator | ||
(let [c1 (add-items empty-counter [:a :b :a]) | ||
c2 (add-items empty-counter [:x :y])] | ||
(combine c1 c2)) | ||
|
||
; Counter-with-total accumulator | ||
(let [c1 (add-items empty-counter-with-total [:a :b :a]) | ||
c2 (add-items empty-counter-with-total [:x :y])] | ||
(combine c1 c2)) | ||
|
||
; Sum accumulator: combine is addition | ||
(let [s1 (add-items empty-sum [1 2 3]) | ||
s2 (add-items empty-sum [-1 -2 -3])] | ||
(combine s1 s2)) | ||
|
||
; Product accumulator: combine is multiplication | ||
(let [p1 (add-items empty-product [2 3]) | ||
p2 (add-items empty-product [(/ 1 2)])] | ||
(combine p1 p2)) | ||
|
||
; Maximum accumulator: combine is max | ||
(let [m1 (add-items empty-maximum [2 3]) | ||
m2 (add-items empty-maximum [(/ 1 2)])] | ||
(combine m1 m2)) | ||
|
||
; Minimum accumulator: combine is min | ||
(let [m1 (add-items empty-minimum [2 3]) | ||
m2 (add-items empty-minimum [(/ 1 2)])] | ||
(combine m1 m2)) | ||
|
||
; Min-max accumulator: combination of minimum and maximum | ||
(let [m1 (add-items empty-min-max [2 3]) | ||
m2 (add-items empty-min-max [(/ 1 2)])] | ||
(combine m1 m2)) | ||
|
||
; Mean-variance accumulator: sample mean and sample variance | ||
(let [m1 (add-items empty-mean-variance [2 4]) | ||
m2 (add-items empty-mean-variance [6])] | ||
(combine m1 m2)) | ||
|
||
; String accumulator: combine is concatenation | ||
(combine "a" "b" "c" "def") | ||
(add "a" (char 44)) | ||
(add-items empty-string [(char 55) (char 56) (char 57)]) | ||
|
||
; Accumulator tuples permit to update several accumulators in parallel | ||
(let [pair (empty-tuple [empty-vector empty-string])] | ||
(add-items pair [[1 "a"] [2 "b"]])) |
66 changes: 66 additions & 0 deletions
66
modules/condition/src/examples/clojure/examples/condition.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and | ||
;; distribution terms for this software are covered by the Eclipse Public | ||
;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can | ||
;; be found in the file epl-v10.html at the root of this distribution. By | ||
;; using this software in any fashion, you are agreeing to be bound by the | ||
;; terms of this license. You must not remove this notice, or any other, | ||
;; from this software. | ||
;; | ||
;; clojure.contrib.condition.example.clj | ||
;; | ||
;; scgilardi (gmail) | ||
;; Created 09 June 2009 | ||
|
||
(ns example.condition | ||
(:use (clojure.contrib | ||
[condition | ||
:only (handler-case print-stack-trace raise *condition*)]))) | ||
|
||
(defn func [x y] | ||
"Raises an exception if x is negative" | ||
(when (neg? x) | ||
(raise :type :illegal-argument :arg 'x :value x)) | ||
(+ x y)) | ||
|
||
(defn main | ||
[] | ||
|
||
;; simple handler | ||
|
||
(handler-case :type | ||
(println (func 3 4)) | ||
(println (func -5 10)) | ||
(handle :illegal-argument | ||
(print-stack-trace *condition*)) | ||
(println 3)) | ||
|
||
;; multiple handlers | ||
|
||
(handler-case :type | ||
(println (func 4 1)) | ||
(println (func -3 22)) | ||
(handle :overflow | ||
(print-stack-trace *condition*)) | ||
(handle :illegal-argument | ||
(print-stack-trace *condition*))) | ||
|
||
;; nested handlers | ||
|
||
(handler-case :type | ||
(handler-case :type | ||
nil | ||
nil | ||
(println 1) | ||
(println 2) | ||
(println 3) | ||
(println (func 8 2)) | ||
(println (func -6 17)) | ||
;; no handler for :illegal-argument | ||
(handle :overflow | ||
(println "nested") | ||
(print-stack-trace *condition*))) | ||
(println (func 3 4)) | ||
(println (func -5 10)) | ||
(handle :illegal-argument | ||
(println "outer") | ||
(print-stack-trace *condition*)))) |
116 changes: 116 additions & 0 deletions
116
modules/datalog/src/examples/clojure/examples/datalog.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and | ||
;; distribution terms for this software are covered by the Eclipse Public | ||
;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can | ||
;; be found in the file epl-v10.html at the root of this distribution. By | ||
;; using this software in any fashion, you are agreeing to be bound by the | ||
;; terms of this license. You must not remove this notice, or any other, | ||
;; from this software. | ||
;; | ||
;; example.clj | ||
;; | ||
;; A Clojure implementation of Datalog - Example | ||
;; | ||
;; straszheimjeffrey (gmail) | ||
;; Created 2 March 2009 | ||
|
||
|
||
(ns examples.datalog | ||
(:use [clojure.contrib.datalog :only (build-work-plan run-work-plan)] | ||
[clojure.contrib.datalog.rules :only (<- ?- rules-set)] | ||
[clojure.contrib.datalog.database :only (make-database add-tuples)] | ||
[clojure.contrib.datalog.util :only (*trace-datalog*)])) | ||
|
||
|
||
|
||
|
||
(def db-base | ||
(make-database | ||
(relation :employee [:id :name :position]) | ||
(index :employee :name) | ||
|
||
(relation :boss [:employee-id :boss-id]) | ||
(index :boss :employee-id) | ||
|
||
(relation :can-do-job [:position :job]) | ||
(index :can-do-job :position) | ||
|
||
(relation :job-replacement [:job :can-be-done-by]) | ||
;(index :job-replacement :can-be-done-by) | ||
|
||
(relation :job-exceptions [:id :job]))) | ||
|
||
(def db | ||
(add-tuples db-base | ||
[:employee :id 1 :name "Bob" :position :boss] | ||
[:employee :id 2 :name "Mary" :position :chief-accountant] | ||
[:employee :id 3 :name "John" :position :accountant] | ||
[:employee :id 4 :name "Sameer" :position :chief-programmer] | ||
[:employee :id 5 :name "Lilian" :position :programmer] | ||
[:employee :id 6 :name "Li" :position :technician] | ||
[:employee :id 7 :name "Fred" :position :sales] | ||
[:employee :id 8 :name "Brenda" :position :sales] | ||
[:employee :id 9 :name "Miki" :position :project-management] | ||
[:employee :id 10 :name "Albert" :position :technician] | ||
|
||
[:boss :employee-id 2 :boss-id 1] | ||
[:boss :employee-id 3 :boss-id 2] | ||
[:boss :employee-id 4 :boss-id 1] | ||
[:boss :employee-id 5 :boss-id 4] | ||
[:boss :employee-id 6 :boss-id 4] | ||
[:boss :employee-id 7 :boss-id 1] | ||
[:boss :employee-id 8 :boss-id 7] | ||
[:boss :employee-id 9 :boss-id 1] | ||
[:boss :employee-id 10 :boss-id 6] | ||
|
||
[:can-do-job :position :boss :job :management] | ||
[:can-do-job :position :accountant :job :accounting] | ||
[:can-do-job :position :chief-accountant :job :accounting] | ||
[:can-do-job :position :programmer :job :programming] | ||
[:can-do-job :position :chief-programmer :job :programming] | ||
[:can-do-job :position :technician :job :server-support] | ||
[:can-do-job :position :sales :job :sales] | ||
[:can-do-job :position :project-management :job :project-management] | ||
|
||
[:job-replacement :job :pc-support :can-be-done-by :server-support] | ||
[:job-replacement :job :pc-support :can-be-done-by :programming] | ||
[:job-replacement :job :payroll :can-be-done-by :accounting] | ||
|
||
[:job-exceptions :id 4 :job :pc-support])) | ||
|
||
(def rules | ||
(rules-set | ||
(<- (:works-for :employee ?x :boss ?y) (:boss :employee-id ?e-id :boss-id ?b-id) | ||
(:employee :id ?e-id :name ?x) | ||
(:employee :id ?b-id :name ?y)) | ||
(<- (:works-for :employee ?x :boss ?y) (:works-for :employee ?x :boss ?z) | ||
(:works-for :employee ?z :boss ?y)) | ||
(<- (:employee-job* :employee ?x :job ?y) (:employee :name ?x :position ?pos) | ||
(:can-do-job :position ?pos :job ?y)) | ||
(<- (:employee-job* :employee ?x :job ?y) (:job-replacement :job ?y :can-be-done-by ?z) | ||
(:employee-job* :employee ?x :job ?z)) | ||
(<- (:employee-job* :employee ?x :job ?y) (:can-do-job :job ?y) | ||
(:employee :name ?x :position ?z) | ||
(if = ?z :boss)) | ||
(<- (:employee-job :employee ?x :job ?y) (:employee-job* :employee ?x :job ?y) | ||
(:employee :id ?id :name ?x) | ||
(not! :job-exceptions :id ?id :job ?y)) | ||
(<- (:bj :name ?x :boss ?y) (:works-for :employee ?x :boss ?y) | ||
(not! :employee-job :employee ?y :job :pc-support)))) | ||
|
||
|
||
|
||
(def wp-1 (build-work-plan rules (?- :works-for :employee '??name :boss ?x))) | ||
(run-work-plan wp-1 db {'??name "Albert"}) | ||
|
||
(def wp-2 (build-work-plan rules (?- :employee-job :employee '??name :job ?x))) | ||
(binding [*trace-datalog* true] | ||
(run-work-plan wp-2 db {'??name "Li"})) | ||
|
||
(def wp-3 (build-work-plan rules (?- :bj :name '??name :boss ?x))) | ||
(run-work-plan wp-3 db {'??name "Albert"}) | ||
|
||
(def wp-4 (build-work-plan rules (?- :works-for :employee ?x :boss ?y))) | ||
(run-work-plan wp-4 db {}) | ||
|
||
|
||
;; End of file |
60 changes: 60 additions & 0 deletions
60
modules/miglayout/src/examples/clojure/examples/miglayout.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and | ||
;; distribution terms for this software are covered by the Eclipse Public | ||
;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can | ||
;; be found in the file epl-v10.html at the root of this distribution. By | ||
;; using this software in any fashion, you are agreeing to be bound by the | ||
;; terms of this license. You must not remove this notice, or any other, | ||
;; from this software. | ||
;; | ||
;; clojure.contrib.miglayout.example | ||
;; | ||
;; A temperature converter using miglayout. Demonstrates accessing | ||
;; components by their id constraint. | ||
;; | ||
;; scgilardi (gmail) | ||
;; Created 31 May 2009 | ||
|
||
(ns examples.miglayout | ||
(:import (javax.swing JButton JFrame JLabel JPanel JTextField | ||
SwingUtilities)) | ||
(:use (clojure.contrib | ||
[miglayout :only (miglayout components)] | ||
[swing-utils :only (add-key-typed-listener)]))) | ||
|
||
(defn fahrenheit | ||
"Converts a Celsius temperature to Fahrenheit. Input and output are | ||
strings. Returns \"input?\" if the input can't be parsed as a Double." | ||
[celsius] | ||
(try | ||
(format "%.2f" (+ 32 (* 1.8 (Double/parseDouble celsius)))) | ||
(catch NumberFormatException _ "input?"))) | ||
|
||
(defn- handle-key | ||
"Clears output on most keys, shows conversion on \"Enter\"" | ||
[event out] | ||
(.setText out | ||
(if (= (.getKeyChar event) \newline) | ||
(fahrenheit (-> event .getComponent .getText)) | ||
""))) | ||
|
||
(defn converter-ui | ||
"Lays out and shows a Temperature Converter UI" | ||
[] | ||
(let [panel | ||
(miglayout (JPanel.) | ||
(JTextField. 6) {:id :input} | ||
(JLabel. "\u00b0Celsius") :wrap | ||
(JLabel.) {:id :output} | ||
(JLabel. "\u00b0Fahrenheit")) | ||
{:keys [input output]} (components panel)] | ||
(add-key-typed-listener input handle-key output) | ||
(doto (JFrame. "Temperature Converter") | ||
(.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE) | ||
(.add panel) | ||
(.pack) | ||
(.setVisible true)))) | ||
|
||
(defn main | ||
"Invokes converter-ui in the AWT Event thread" | ||
[] | ||
(SwingUtilities/invokeLater converter-ui)) |
Oops, something went wrong.