Clojure : missing namespace errors when running "use" at the REPL - namespaces

I have a .clj file that starts like this :
(ns clojure_crawl.core)
(require '[clj-http.client :as client])
(use 'clojure.contrib.json)
Followed by several function definitions :
(defn f1 [] "" (+ 1 1))
(defn f2 [] "" (+ 2 2))
etc...
However, when I run the command "(use 'myfile.core :reload)"
Some of my functions , although visible at the REPL, cannot run do to "missing namespace" errors.
How do I add the dependencies so that the REPL can run any of the functions defined in my file ?

If your code is in "clojure_crawl/core.clj", its namespace should be clojure-crawl.core (note the hyphen). See http://clojure.org/libs

As Joost already said, you have to be careful with hyphens and underscores: wherever you use a hyphen in your namespace names, replace it with an underscore in the corresponding file/directory names (and vice versa).
Also, the use of the require and use functions in clj source files is discouraged. Instead, declare the libraries you need directly in the ns macro:
(ns clojure-crawl.core
(:require [clj-http.client :as client])
(:use clojure.contrib.json))
This also takes the burden of properly quoting the required namespaces from you.

Related

Function argument as part of path to a file in Clojure

Suppose I have the same file name, placed in five different sub-folders. The file in question has the same name in all folders but otherwise has different values.
Suppose my file is called something like test.csv. My folder are A1,A2,A3,A4 and A5.
My data-read structure looks as so:
(defn my-data [folder]
(loop [SUB-FOLDER (str 'folder)
X []
data (with-open [file (clojure.java.io/reader "./resources/SUB-FOLDER/test.csv")]
(doall (csv/read-csv file)))]
(def row (first data))
(if (nil? row)
[(lazy-seq X)]
(recur (conj X (map #(Float/parseFloat %) row))
(rest data)))))
I would call this function as so
(def Y (map vec (get (my-data A1) 0)))
Where I am trying to access the file test.csv in the sub-folder A1 which I am passing as an argument to my csv-read function.
Now obviously the above code does not work, but it provides an overview of what I am trying to achieve.
How could one make the minimal example work?
Thanks.
Something like this should put your data from one folder into a sequence of sequences:
(defn read-data [folder]
(let [data (csv/read-csv (slurp (str "./resources/" folder "/test.csv")))]
(for [line data]
(map #(Float/parseFloat %) line))))
Change map to mapv if you want to have vectors, and wrap the for with into [] if you want a vector of vectors. This assumes folder is a string. Because your files are very small you don't need to open them with a reader, it's easier to slurp them and then parse them.
A fe issues with your original code:
You're always trying to read a file called "./resources/SUB-FOLDER/test.csv" instead of replacing the name of your folder.
You don't want to use def inside a function. See let.
Calling lazy-seq on an already realized sequence doesn't do anything.
By calling doall on your reader you're forcing all your data into memory instead of reading it and processing it as you go (that's why I replaced it with slurp). If you're going to process it in a loop as you read it, you want the with-open outside the loop (and no doall).

how to create macros for clojurescript

I am stuck with creating a macro for clojurescript. Can you give a step-by-step instruction on how to include a macro in clojurescript? The folder structure I am looking for is like this
+ src/
| clj/
| test_app/
| macros.clj
| cljs/
| test_app/
| example.cljs
| project.clj
Can you give an example for each of the 3 files? I am looking for a solution that is compatible with lein cljsbuild auto .
This is a macro that allows you to include files in cljs at compile time. For example, when doing i18n, I save all the language strings on a translations.edn file. I then include it at compile time using a macro in /clj/test_app/macros.clj:
(ns test-app.macros
(:import java.io.File))
(defmacro load-edn
"Reads a file and returns it as a string"
[relative-path]
(slurp relative-path))
In cljs/test_app/example.cljs
(ns test-app.example
(:require [cljs.reader :as reader])
(:require-macros [test-app.macros :as m])
(:use [net.unit8.tower :only [t]]))
(def i18n (reader/read-string (m/load-edn "resources/lang/translations.edn")))
(defn lang-map
"Wrapper around tower's t adding the configuration map"
[language & args]
(apply t language i18n args))
This should work if you start from cljs-kickoff [1] and add [net.unit8/tower-cljs "0.1.0"] to your dependencies.
[1] https://github.com/konrad-garus/cljs-kickoff

How to download and parse a csv file in Racket?

How do I download and parse a csv file in Racket?
Use get-pure-port to download the file, and use the Planet library (require (planet neil/csv)) to parse it.
The following example downloads and parses a csv file containing
data on the size of the various Galapagos islands and how many species were found on each island.
#lang racket
(require (planet neil/csv:1:=7) net/url)
(define galapagos-url
(string->url
"http://www.stat.washington.edu/~handcock/536/Data/galapagos.csv"))
(define make-galapagos-csv-reader
(make-csv-reader-maker
'((separator-chars #\,)
(strip-leading-whitespace? . #t)
(strip-trailing-whitespace? . #t))))
(define (all-rows url make-reader)
(define next-row (make-reader (get-pure-port url)))
(define (loop)
(define row (next-row))
(if (empty? row)
'()
(cons row (loop))))
(loop))
(all-rows galapagos-url make-galapagos-csv-reader)
The first rows returned are:
'(("Island"
"Observed.species"
"Native.species"
"Area(km^2)"
"Elevation(m)"
"Distance.nearest.island(km)"
"Distance.Santa.Cruz(km)"
"Area.adj.island(km^2)")
("Baltra" "58" "23" "25.09" "" "0.6" "0.6" "1.84")
("Bartolome" "31" "21" "1.24" "109" "0.6" "26.3" "572.33")
("Caldwell" "3" "3" "0.21" "114" "2.8" "58.7" "0.78")
Neil has a new library, csv-reading, so use that instead.
First, install the package using raco:
raco pkg install csv-reading
To convert a CSV file to a list, here is a helper function:
(require csv-reading)
(define (csvfile->list filename)
(call-with-input-file filename
csv->list))
To download a CSV file and convert to a list, do this:
(require net/url)
((compose csv->list get-pure-port string->url) "http://example.com")
See here for the csv-reading library: csv-reading library, it is the latest version (and the other answers are using deprecated ones).
This answer got me where I was looking to go, but I thought I'd chime in for future intrepid explorers. There is an easier invocation (perhaps with newer versions of the csv library?) for doing what you ask. This of course assumes you want the comma delimiter, and to strip leading / trailing whitespace as in the above example.
#lang racket
(require (planet neil/csv:2:0) net/url)
;; Continuing with the UW data sources examples
(define iver-url
(string->url
"http://faculty.washington.edu/cadolph/vis/iver.csv"))
(csv->list (get-pure-port iver-url))
In this version, the csv->list function automatically creates a csv-reader with the aforementioned defaults. You can override the defaults (e.g. if you had a different delimiter or didn't want to strip trailing and leading whitespace) by instead passing a your custom csv reader constructed with make-csv-reader to csv->list.
Use get-pure-port to download the file, then use read-csv-file (from the 2htdp/batch-io library included in Racket) to parse the data.
Download the file
#lang racket
(require net/url)
;; Get the data.
(define the-url (string->url "http://www.example.com/data.csv"))
(define the-data (port->bytes (get-pure-port the-url)))
;; Write the data to a file.
(define outfile (open-output-file "data.csv"))
(write-bytes the-data outfile)
(close-output-port outfile)
Reference: https://www.monolune.com/how-to-download-files-using-racket/
WARNING: By default, when using HTTPS, Racket will not verify the server's certificate. Read the documentation (get-pure-port) on how to enable server certificates verification when using HTTPS.
Parse the data
#lang racket
(require 2htdp/batch-io)
(read-csv-file "data.csv") ; Returns a list.

Can't get pprint to work in clojure

Noob question, using Win7 64-bit, Clojure 1.2.0, Java 1.6.0_22
When I start clojure from command line, pprint function is easily available.
user=> pprint
#<pprint$pprint clojure.pprint$pprint#16dfa45>
user=> (pprint "hi")
"hi"
nil
user=>
But when I try to use pprint from a file, I get an error. This happens with and without namespace (ns... :require...) as shown in pprint documentation
clj file as follows:
(ns whatevah
(:require clojure.pprint))
(pprint "hi")
Error as follows:
C:\Users\mischw\code\Clojure>java -cp ";c:\users\mischw\code\clojure\classes\*;c:\Program Files (x86)\Java\SWT;c:\users\mischw\code\clojure\classes\bookcode\*" clojure.main swinglearn.clj
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: pprint in this context (swinglearn.clj:14)
... 21 more
Output completed (0 sec consumed) - Normal Termination
I don't understand the general idea of what's going on here. Why does one work but not the other? Does that have to do with namespaces? Classpaths? Some other simple fix? Clearly noob questions, but I find this happens with a bunch of examples... I'm unable to run them even though it seems straightforward to import/use/require/include them.
You're getting require mixed up with use and/or import. require causes the library to get loaded, and every public symbol it exports will be accessible as eg clojure.pprint/pprint. If you want to use a more convenient name like just pprint, you need to refer to the namespace. use is a convenient shorthand for "require, then refer", to load the library without the namespace prefix.
user> (ns user (:require clojure.pprint))
nil
user> (pprint 1)
; Evaluation aborted.
user> (clojure.pprint/pprint 1)
1
nil
user> (ns user (:use clojure.pprint))
nil
user> (pprint 1)
1
Edit: Not sure why it's working for you from the REPL. As you can see, it doesn't work for me. I imagine you did some setup earlier that makes it work and then forgot about it, or possibly you have some init script that does this stuff for you at the REPL but not when loading from a file.
Here are a couple of examples:
;;how to use it with :require and :use
;; :require
(ns example.pprinter
(:require [clojure.pprint :as pp]))
(def myname "John Smith")
(pp/pprint myname)
;; :use
(ns example.pprinter
(:use clojure.pprint))
(def myname "John Smith")
(pprint myname)

Compojure HTML Formatting

I'm relatively new to Clojure and a complete HTML/Compojure virgin. I'm trying to use Compojure to create static pages of HTML using a function similar to this:
(defn fake-write-html
[dir args]
(let [file (str dir *file-separator* *index-file*)
my-html (html
(doctype :html4)
[:html
[:head
[:title "Docs and Dirs:"]]
[:body
[:div
[:h2 "A nice title"]]
[:div
[:ul
[:li "One"]
[:li "Two"]]]]])]
(clojure.contrib.duck-streams/spit file my-html)))
The function just writes HTML to a file. (The args argument is irrelevant here. Just there to assure the example compiles and runs in my program.)
"Programming Clojure" indicated that the call to the html function would produce formatted HTML -- multiple lines with indentation. All I get is the doc type as expected followed by all of the HTML on a single line. HTML Tidy doesn't find any issues with the content of the output file. It comes out as a single line if I println it at the REPL too.
Is there something else needed to get formatted output?
The formatting of HTML output in Compojure was removed for performance and complexity reasons. To get formatted output you will probably have to write your own printer function.
I usually output HTML as Compojure sees fit and use Firebug to view it live in my browser. Firebug will display it nicely formatted no matter if it's really all on one line or not. This works well enough most of the time. If you need to serialize this HTML in a readable form, you could keep it as Clojure vectors and sexps and serialize it that way.
Although Brian's answer pointed me to Firebug, enabling the debugging I wanted, I was just to obsessive-compulsive to leave it alone. Following up on kwertii's pointer to JTidy, I included the following code in my program.
Edit: Simplified the code somewhat
(ns net.dneclark.someprogram
(:gen-class)
...
(:import (org.w3c.tidy Tidy))
)
...
(defn configure-pretty-printer
"Configure the pretty-printer (an instance of a JTidy Tidy class) to
generate output the way we want -- formatted and without sending warnings.
Return the configured pretty-printer."
[]
(doto (new Tidy)
(.setSmartIndent true)
(.setTrimEmptyElements true)
(.setShowWarnings false)
(.setQuiet true)))
(defn pretty-print-html
"Pretty-print the html and return it as a string."
[html]
(let [swrtr (new StringWriter)]
(.parse (configure-pretty-printer) (new StringReader (str html)) swrtr)
(str swrtr)))
I added the jtidy-r938.jar to my project (NetBeans using the enclojure plugin) and imported it. The configuration function tells the parser to output formatted, indented HTML and skip the warnings. The return value from the pretty-printer function is now nicely formatted whether I open it with Firebug or a simple text editor.
There are tons of HTML pretty printers available for Java, notably JTidy, a Java port of HTML Tidy. You can easily feed Clojure's output through this library programatically and get neatly indented and formatted HTML back.
HTML Tidy is also available as a command-line program for Unix if you'd care to go that route -- you can just pipe your HTML through it like any other shell program.
The above did not work for me.
I changed this a bit.
add this [jtidy "4aug2000r7-dev"] to project.clj
(:use clojure.core)
(:import (org.w3c.tidy Tidy))
(:import (java.io ByteArrayInputStream ByteArrayOutputStream)))
(defn configure-pretty-printer
"Configure the pretty-printer (an instance of a JTidy Tidy class) to
generate output the way we want -- formatted and without sending warnings.
Return the configured pretty-printer."
[]
(doto (new Tidy)
(.setSmartIndent true)
;(.setTrimEmptyElements true)
(.setShowWarnings false)
(.setQuiet true)))
(defn pretty-print-html
"Pretty-print the html and return it as a string."
[html]
(let [swrtr ( ByteArrayOutputStream.)]
(.parse (configure-pretty-printer) (ByteArrayInputStream. (.getBytes (str html))) swrtr)
(str swrtr)))
If anyone is still looking at this query, you need the hiccup library. If formats HTML from exactly the Clojure data structure shown.
So
(require '[hiccup.core :refer [html]])
(defn fake-write-html
[dir args]
(let [file (str dir *file-separator* *index-file*)
my-html (html
[:html
[:head
[:title "Docs and Dirs:"]]
[:body
[:div
[:h2 "A nice title"]]
[:div
[:ul
[:li "One"]
[:li "Two"]]]]])]
(clojure.contrib.duck-streams/spit file my-html)))
will work exactly as the original poster intended. Strongly recommended.