为什么定义在外区的变量在内区不可用?
Why the variable defined in outer section is not available in inner sector?
我有以下代码,在第二行我定义了file-fields-in-keyword,但它只在第3行可用,在第10行和第24行,它打印nil。但是为什么第 10,24 行看到 nil 而第 3 行可以看到非 nil 值?我希望他们是一样的
(defn load-file-to-database [file-name file-id constraints-map auto-creation-config delimiter]
(let [file-fields-in-keyword (get-file-header-as-key-word file-name)]
(println file-fields-in-keyword)
(with-open [rdr (io/reader file-name)]
(doseq [chunk (partition-all 10000 (map-indexed
(fn [index line] (str file-id delimiter (inc index) delimiter line))
(line-seq rdr)
))]
(println (count chunk))
(println file-fields-in-keyword) ;; it prints nil
(println (first chunk))
(let [rows (map #(clojure.string/split % #",") chunk)
rows-map (map #(zipmap file-fields-in-keyword %) rows)
rows-after-validation (map #(process-row % constraints-map auto-creation-config) rows-map)
valid-records (valid-lines rows-after-validation)
invalid-records (invalid-lines rows-after-validation)
]
(println (count valid-records))
(println (count invalid-records))
(println (first valid-records))
(println (first invalid-records))
(println "xxx")
(println file-fields-in-keyword) ;; it prints nil
(apply (partial j/insert! postgres-db 'test_clojure file-fields-in-keyword) valid-records)
)))
)
)
(defn get-file-header-as-key-word [file-name]
(with-open [rdr (io/reader file-name)]
(println (map keyword (str/split (str row_number "," "file_id" "," (first (line-seq rdr))) #",")))
))
(load-file-to-database file-name 100 constraints-map auto-creation-config ",")
你是对的,所有三个值应该相同。
从函数 get-file-header-as-key-word
返回的值总是 nil
因为 println
总是 returns 这个值,所以对 println
的所有三个调用都应该打印nil
作为 file-fields-in-keyword
的值。
要从 get-file-header-as-key-word
函数中获取非 nil
值,您需要删除对 println
的调用。这应该导致 load-file-to-database
中 println
的所有输出都是相同的非 nil
值。
我有以下代码,在第二行我定义了file-fields-in-keyword,但它只在第3行可用,在第10行和第24行,它打印nil。但是为什么第 10,24 行看到 nil 而第 3 行可以看到非 nil 值?我希望他们是一样的
(defn load-file-to-database [file-name file-id constraints-map auto-creation-config delimiter]
(let [file-fields-in-keyword (get-file-header-as-key-word file-name)]
(println file-fields-in-keyword)
(with-open [rdr (io/reader file-name)]
(doseq [chunk (partition-all 10000 (map-indexed
(fn [index line] (str file-id delimiter (inc index) delimiter line))
(line-seq rdr)
))]
(println (count chunk))
(println file-fields-in-keyword) ;; it prints nil
(println (first chunk))
(let [rows (map #(clojure.string/split % #",") chunk)
rows-map (map #(zipmap file-fields-in-keyword %) rows)
rows-after-validation (map #(process-row % constraints-map auto-creation-config) rows-map)
valid-records (valid-lines rows-after-validation)
invalid-records (invalid-lines rows-after-validation)
]
(println (count valid-records))
(println (count invalid-records))
(println (first valid-records))
(println (first invalid-records))
(println "xxx")
(println file-fields-in-keyword) ;; it prints nil
(apply (partial j/insert! postgres-db 'test_clojure file-fields-in-keyword) valid-records)
)))
)
)
(defn get-file-header-as-key-word [file-name]
(with-open [rdr (io/reader file-name)]
(println (map keyword (str/split (str row_number "," "file_id" "," (first (line-seq rdr))) #",")))
))
(load-file-to-database file-name 100 constraints-map auto-creation-config ",")
你是对的,所有三个值应该相同。
从函数 get-file-header-as-key-word
返回的值总是 nil
因为 println
总是 returns 这个值,所以对 println
的所有三个调用都应该打印nil
作为 file-fields-in-keyword
的值。
要从 get-file-header-as-key-word
函数中获取非 nil
值,您需要删除对 println
的调用。这应该导致 load-file-to-database
中 println
的所有输出都是相同的非 nil
值。