clojure

Isn't Swing's JPanel pretty much like AWT's Panel? The former's widget is not showing up

I'm changing a program from AWT to Swing, as proposed on the second chapter of Java Swing's book, and the panel just disappears when I do the change from Panel to JPanel. The same doesn't happen when I change from Button to JButton. It seems to be a bug, since it appears to be trivially simple to do so - just adding an extra J to the na...

Writing a time function in Haskell

I'm new to Haskell and I'd like to be able to time the runtime of a given function call or snippet of code. In Clojure I can use 'time': user=> (time (apply * (range 2 10000))) "Elapsed time: 289.795 msecs" 2846259680917054518906413212119868890148051... In Scala, I can define the function myself: scala> def time[T](code : => T) = {...

Clojure allows encapsulation and inheritance, but can I combine them?

Here is an overly simplistic example for illustration: I can encapsulate an implementation detail such as using an atom for a counter: (defn make-counter ([] (make-counter 0)) ([init-val] (let [c (atom init-val)] {:get (fn [] @c) :++ (fn [] (swap! c inc))}))) But that means I need to redefine everything to add a fea...

Image Processing, extending JPanel and Simulating Classes in Clojure

Hello, there! I'm building an image-processing application in swing/clojure, and right now I need to develop an image panel in which I can click and compute data. Thanks to coobird, I now have a good idea on how to do it in Java, but I still don't get many issues on its integration with Clojure. Let's take a look at how coobird suggeste...

Help! I get OutofMemory while Retrieving web pages.

I am retrieving the HTML from the web. I get "java.lang.OutOfMemoryError: Java heap space (repl-1:3)" ;; fetch: URL -> String ;; fetch returns the string of the HTML url (defn fetch [url] (with-open [stream (. url openStream)] (let [buffer (BufferedReader. (InputStreamReader. stream))] (apply str (line-seq buffer))))) ...

Let vs. Binding in Clojure

I understand that they're different since one works for setting *compile-path* and one doesn't. However, I need help with why they're different. let creates a new scope with the given bindings, but binding...? Thanks! ...

How do I typehint away this reflection warning?

(set! *warn-on-reflection* true) (proxy [javax.swing.JPanel] [] (paintComponent [#^java.awt.Graphics g] (proxy-super paintComponent g) (.fillRect g 100 100 10 10))) "Reflection warning, call to paintComponent can't be resolved" ...

How to write this better in Clojure

The structure of my hash is (def *document-hash* {"documentid" {:term-detail {"term1" tf1 ,"term2" tf2}}) I wan to find the tf. Right now I have this. Is there a better way to do this in clojure? ;; Find tf (defn find-tf [documentid term-number] (if (nil? *document-hash* documentid) 0 (if (nil? (((*document-hash* do...

Why does Clojure have "keywords" in addition to "symbols"?

I have a passing knowledge of other Lisps (particularly Scheme) from way back when. My knowledge is pretty rusty (and was pretty basic to begin with). Recently I've been reading about Clojure. I see that it has both "symbols" and "keywords". Symbols I'm familiar with, but not keywords. Do other Lisps have keywords? How are keywords diff...

Why is this function returnin nil? Is it because the D.S is lazy?

(defn get-doc-list [a-term] (map #(Integer/parseInt %)(take-nth 3 (take (* 3 3)(rest (rest a-term)))))) This function works on small lists but returns and empty sequence on larger ones. What is the problem? ...

How do you add in this hash table in Clojure?

I have a document hash which is a reference like this: (def *document-hash* (ref (hash-map))) It looks like this {"documentid" {:term-detail {"term1" count1 ,"term2" count2}, "doclen" 33}}} How do I add to this hash table?Right now I have (defn add-doc-hash [docid term-number count] (dosync (alter *document-hash* (fn [a-h...

How do you use sorted-map-by in Clojure?

I cant understand the documentation at all. I want a sorted map "xxx", which sorts the map according to the value. How do I do that? Thanks. ...

How do you convert a list of numbers into a map?

I have a list of keys: (1 2 3 4) I want a map with the values set to 0 like this: {1 0, 2 0, 3 0, 4 0}. How do I do that? ...

Nullpointer in clojure when running doseq with multiple expressions in the body

The following expression in clojure works great: (doseq [x '(1 2 3 4)] (println x)) This one gives me a nullpointer: (doseq [x '(1 2 3 4)] ((println x)(println "x"))) It produces the following output: user=> (doseq [x '(1 2 3 4)] ((println x)(println "x"))) 1 x java.lang.NullPointerException (NO_SOURCE_FILE:0) user=> (.printStackT...

How do I make the alter function/ assoc function return nil?

I have a really really huge hash table and whenever I try to alter the hash, the entire hash is returned, which crashes my REPL. Is there a way I can ask Clojure to just set the value and return nil? Thank You. ...

Is there an alternative to map to apply a function to all in the sequence in Clojure?

I have a function (defn change-score [docid termid] (do (dosync (alter *documents-scores* assoc docid (+ 1 (*documents-scores* docid)))) nil) ) (defn vector-space[] (loop [terms (count (deref *term-hash*))] (if (zero? terms) nil (do (dorun (map (fn[docid](change-score docid terms)) (doc-...

Is there an easier way to change BufferedReader to string?

Right Now I have ;; buffer->string: BufferedReader -> String (defn buffer->string [buffer] (loop [line (.readLine buffer) sb (StringBuilder.)] (if(nil? line) (.toString sb) (recur (.readLine buffer) (.append sb line))))) This is too slow. Edit: I have a BufferedReader when i try to do (str BufferedReader) it ...

Operator Overloading in Clojure

Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus. I am very new to C...

Is Clojure object-oriented at its heart? (Polymorphism in seqs)

Clojure is a functional lisp, reportedly not at all object-oriented, even though it runs on the JVM, a VM designed for an object oriented language. Clojure provides identical interfaces for iterating over lists and vectors by abstracting them to an interface called seq. This is even implemented internally using a Java interface called ...

CMS in functional programming language

Are there any CMS'es, written in functonal programming languages (lisp, haskell, f#/nemerle, scala, erlang, clojure, smalltalk) already? ...