imnisen
imnisen

Reputation: 3

clojure cannot define same function in different namespace? not possible

i have one file "map_reduce2.clj" and another "map_reduce3.clj", the both defin function "map-reduce" themself.

now i want to use namespace of "map_reduce2.clj" in "map_reduce3.clj", but when i press "C-c C-k" in emacs to compile the "map_reduce3.clj",

error happens: "parse-line already refers to: #'chapter12.map-reduce2/parse-line in namespace: chapter12.map-reduce3" , but this doesn't make any sense.

; map_reduce3.cli
(ns chapter12.map-reduce3
  (:use clojure.java.io)
  (:require [chapter12.map-reduce2 :as c12]))

(def IGNORE "_")

(defn parse-line [line]
  (let [tokens (.split (.toLowerCase line) " ")]
    [[IGNORE (count tokens)]]))

(defn average [numbers]
  (/ (apply + numbers)
     (count numbers)))

(defn reducer [combined]
  (average (val (first combined))))

(defn average-line-length [filename]
  (c12/map-reduce parse-line reducer (line-seq (reader filename))))


; map_reduce2.clj
(ns chapter12.map-reduce2
  (:use clojure.java.io))

(defn combine [mapped]
  (->> (apply concat mapped)
       (group-by first)
       (map (fn [[k v]]
              {k (map second v)}))
       (apply merge-with conj)))

(defn map-reduce [mapper reducer args-seq]
  (->> (map mapper args-seq)
       (combine)
       (reducer)))

(defn parse-line [line]
  (let [tokens (.split (.toLowerCase line) " ")]
    (map #(vector % 1) tokens)))

(defn sum [[k v]]
  {k (apply + v)})

(defn reduce-parsed-lines [collected-values]
  (apply merge (map sum collected-values)))


(defn word-frequency [filename]
  (map-reduce parse-line reduce-parsed-lines (line-seq (reader filename))))

images of the the error

Upvotes: 0

Views: 96

Answers (1)

Michiel Borkent
Michiel Borkent

Reputation: 34790

This probably means you have dirty REPL state. Maybe you moved the function parse-line from one namespace to the other. I suggest restarting the REPL, or unload parse-line from map-reduce3: How to unload a function from another namespace?.

Upvotes: 2

Related Questions