Getting Vars to bind properly across multiple files
Posted
by Alex Baranosky
on Stack Overflow
See other posts from Stack Overflow
or by Alex Baranosky
Published on 2010-03-20T03:48:25Z
Indexed on
2010/03/20
3:51 UTC
Read the original article
Hit count: 471
clojure
I am just learning Clojure and am having trouble moving my code into different files.
I keep detting this error from the appnrunner.clj -
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: -run-application in this context
It seems to be finding the namespaces fine, but then not seeing the Vars as being bound... Any idea how to fix this?
Here's my code:
APPLICATION RUNNER -
(ns src/apprunner
(:use src/functions))
(def input-files [(resource-path "a.txt") (resource-path "b.txt") (resource-path "c.txt")])
(def output-file (resource-path "output.txt"))
(defn run-application []
(sort-files input-files output-file))
(-run-application)
APPLICATION FUNCTIONS -
(ns src/functions
(:use clojure.contrib.duck-streams))
(defn flatten [x]
(let [s? #(instance? clojure.lang.Sequential %)]
(filter
(complement s?)
(tree-seq s? seq x))))
(defn resource-path [file]
(str "C:/Users/Alex and Paula/Documents/SoftwareProjects/MyClojureApp/resources/" file))
(defn split2 [str delim]
(seq (.split str delim)))
(defstruct person :first-name :last-name)
(defn read-file-content [file]
(apply str
(interpose "\n" (read-lines file))))
(defn person-from-line [line]
(let [sections (split2 line " ")]
(struct person (first sections) (second sections))))
(defn formatted-for-display [person]
(str (:first-name person) (.toUpperCase " ") (:last-name person)))
(defn sort-by-keys [struct-map keys]
(sort-by #(vec (map % [keys])) struct-map))
(defn formatted-output [persons output-number]
(let [heading (str "Output #" output-number "\n")
sorted-persons-for-output (apply str (interpose "\n" (map formatted-for-display (sort-by-keys persons (:first-name :last-name)))))]
(str heading sorted-persons-for-output)))
(defn read-persons-from [file]
(let [lines (read-lines file)]
(map person-from-line lines)))
(defn write-persons-to [file persons]
(dotimes [i 3]
(append-spit file (formatted-output persons (+ 1 i)))))
(defn sort-files [input-files output-file]
(let [persons (flatten (map read-persons-from input-files))]
(write-persons-to output-file persons)))
© Stack Overflow or respective owner