Hi all - I've recently encouraged a friend to start learning Clojure,
and he has written some basic Markov chaining code as a learning
exercise. His code works fine with small sets of input data, but
larger inputs have been causing a StackOverflowError.

I've taken a look at the code and suspect that the error may be caused
by recurrent calls to filter occupying increasing amounts of stack
space due to filter leaving closed-over locals hanging around. That
being said, this is only a suspicion, I'm still learning the language
myself, so I could be totally wrong. Wrapping the filter calls in
doall seems to prevent the problem from happening, but performance is
abysmal in that case.

I'm using Clojure 1.1.0-alpha-SNAPSHOT for testing.

(defn generate-chain [source]
  (loop [the-list (map #(list (first (split-at 2 %)) (last %))
                      (partition 3 1 (.split (.replace source "\n" "
") " ")))
         res (hash-map)]
    (if (empty? the-list)
      res
      (recur (filter #(not= (first (first the-list))
                            (first %)) the-list)
             (assoc res (first (first the-list))
                    (map #(last %)
                         (filter #(= (first (first the-list))
                                     (first %)) the-list)))))))

(println (generate-chain (slurp "big-file.txt")))

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to