Skip to content

Commit

Permalink
Simplifications, cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
kongra committed Jan 6, 2019
1 parent 6623408 commit f98ab43
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 75 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;; Copyright (c) Konrad Grzanek
;; Created 2019-01-05
(defproject kongra/prelude "0.1.11"
(defproject kongra/prelude "0.1.12"
:description "Predule codebase for Clojure"
:url "https://github.com/kongra/prelude"
:license {:name "Eclipse Public License"
Expand Down
19 changes: 12 additions & 7 deletions src/main/clojure/clojure/kongra/prelude/doclean.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
;; CLEANUP CONTEXT
(defchP chDoclean (instance? jkongra.prelude.Doclean x))

(defn ^jkongra.prelude.Doclean create [ ] (jkongra.prelude.Doclean.))
(defn ^jkongra.prelude.Doclean create
[]
(jkongra.prelude.Doclean.))

(defn close! [^jkongra.prelude.Doclean d] (chUnit (.close d)))
(defn close!
[^jkongra.prelude.Doclean d]
(chUnit (.close d)))

(def ^:dynamic *doclean* nil)

Expand All @@ -34,9 +38,10 @@

(defn register!
([^jkongra.prelude.Doclean d f]
(chIfn f)
(chUnit (.register d (reify java.io.Closeable (close [this] (f))))))
(chUnit
(do (chDoclean d)
(chIfn f)
(.register d (reify java.io.Closeable (close [this] (f))))
nil)))

([f]
(chIfn f)
(chUnit (register! (ensure) f))))
([f] (register! (ensure) f)))
84 changes: 38 additions & 46 deletions src/main/clojure/clojure/kongra/prelude/print.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,62 @@

(defn ^:private indentSymbol
[isEmpty?]
(chString (if (chBool isEmpty?) PRINT-TREE-EMPTYINDENT PRINT-TREE-INDENT)))
(chString
(if (chBool isEmpty?)
PRINT-TREE-EMPTYINDENT
PRINT-TREE-INDENT)))

(defn ^:private genindent
[[isLast? & lastChildInfos]]
(chBool isLast?)
(chMaybe chSequential lastChildInfos)
(chString
(let [suffix (if isLast? PRINT-TREE-FORLASTCHILD PRINT-TREE-FORCHILD)
prefix (->> lastChildInfos
butlast
reverse
(map indentSymbol)
(apply str))]
(str prefix suffix))))
(do (chBool isLast?)
(chMaybe chSequential lastChildInfos)
(let [suffix (if isLast?
PRINT-TREE-FORLASTCHILD
PRINT-TREE-FORCHILD)
prefix (->> lastChildInfos
butlast
reverse
(map indentSymbol)
(apply str))]

(str prefix suffix)))))

(defn ^:private printTreeImpl
[node adjs show ^Long depth ^Long level lastChildInfos isFirst?]
(chIfn adjs)
(chIfn show)
(chNatLong depth)
(chNatLong level)
(chSequential lastChildInfos)
(chBool isFirst?)
(chUnit
(let [s (chString (show node))
pfx (if isFirst? PRINT-TREE-EMPTY PRINT-TREE-EOL)
repr (if (p/zero? (.longValue level))
(str pfx s)
(str pfx (genindent lastChildInfos) s))]

(print repr)

(when-not (p/== (.longValue level) (.longValue depth))
(let [nextLevel (p/inc level)
children (chSequential (adjs node))]
(doseq [[child isLast?] (map vector children (markLast children))]
(printTreeImpl child adjs show depth nextLevel
(cons isLast? lastChildInfos) false)))))))
(do (chIfn adjs)
(chIfn show)
(chNatLong depth)
(chNatLong level)
(chSequential lastChildInfos)
(chBool isFirst?)
(let [s (chString (show node))
pfx (if isFirst? PRINT-TREE-EMPTY PRINT-TREE-EOL)
repr (if (p/zero? (.longValue level))
(str pfx s)
(str pfx (genindent lastChildInfos) s))]

(print repr)

(when-not (p/== (.longValue level) (.longValue depth))
(let [nextLevel (p/inc level)
children (chSequential (adjs node))]
(doseq [[child isLast?] (map vector children (markLast children))]
(printTreeImpl child adjs show depth nextLevel
(cons isLast? lastChildInfos) false))))))))

(defn printTree
"Prints a tree using a textual representation like in UNIX tree command.
adjs : node -> [node]
show : node -> String"
([node adjs]
(chIfn adjs)
(chUnit (printTree node adjs str)))

([node adjs show]
(chIfn adjs)
(chIfn show)
(chUnit (printTree node adjs show Long/MAX_VALUE)))

([node adjs show depth]
(chIfn adjs)
(chIfn show)
(chNatLong depth)
([node adjs show ^Long depth]
(chUnit (printTreeImpl node adjs show depth 0 '(true) true))))

;; GRAPH TREE-PRINTING
Expand All @@ -89,7 +89,6 @@

(defn ^:private printGraphShow
[show v]
(chIfn show)
(chString (if (instance? PrintGraphEllipsis v)
(str (chString (show (.v ^PrintGraphEllipsis v))) " ...")

Expand All @@ -99,7 +98,6 @@

(defn ^:private printGraphAdjs
[adjs v]
(chIfn adjs)
(chSequential
(if-not (instance? PrintGraphEllipsis v)
(map
Expand All @@ -110,22 +108,16 @@
'())))

(defn printGraph
([v adjs show depth]
(chIfn adjs)
(chIfn show)
(chNatLong depth)
([v adjs show ^Long depth]
(chUnit (binding [*printGraphVisited* (chAtom (atom #{}))]
(printTree v
(partial printGraphAdjs adjs)
(partial printGraphShow show)
depth))))
([v adjs show]
(chIfn adjs)
(chIfn show)
(chUnit (printGraph v adjs show Long/MAX_VALUE)))

([v adjs]
(chIfn adjs)
(chUnit (printGraph v adjs str))))

;; SOME TESTS
Expand Down
33 changes: 12 additions & 21 deletions src/main/clojure/clojure/kongra/prelude/search.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
(ns clojure.kongra.prelude.search
(:require
[clojure.kongra.ch
:refer [chIfn chMaybe chSome chBool
chSeq chPosLong]]
:refer [chIfn chBool chSeq chPosLong]]

[clojure.kongra.prelude
:refer [lazyCat]]))
Expand All @@ -24,28 +23,21 @@
(chIfn goal?)
(chIfn adjs)
(chIfn comb)
(chMaybe chSome
(loop [nodes (list start)]
(when (seq nodes)
(let [obj (first nodes)]
(if (chBool (goal? obj))
obj
(recur (chSeq (comb (chSeq (rest nodes))
(chSeq (adjs obj)))))))))))
(loop [nodes (list start)]
(when (seq nodes)
(let [obj (first nodes)]
(if (chBool (goal? obj))
obj
(recur (chSeq (comb (chSeq (rest nodes))
(chSeq (adjs obj))))))))))

(defn breadthFirstSearch
[start goal? adjs]
(chIfn goal?)
(chIfn adjs)
(chMaybe chSome
(treeSearch start goal? adjs breadthFirstCombiner)))
(treeSearch start goal? adjs breadthFirstCombiner))

(defn depthFirstSearch
[start goal? adjs]
(chIfn goal?)
(chIfn adjs)
(chMaybe chSome
(treeSearch start goal? adjs depthFirstCombiner)))
(treeSearch start goal? adjs depthFirstCombiner))

;; TREE-SEARCH SEQ
(defn breadthFirstTreeLevels
Expand All @@ -61,9 +53,8 @@
(chIfn adjs)
(chSeq (apply concat (breadthFirstTreeLevels start adjs))))

([start adjs depth]
(chIfn adjs)
([start adjs ^long depth]
(chPosLong depth)
(chSeq (->> (breadthFirstTreeLevels start adjs)
(take depth)
(take depth)
(apply concat)))))

0 comments on commit f98ab43

Please sign in to comment.