本文介绍了如何将Reduce实现的序列变回Lazy向量序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行一行221行.csv文件 - 用clojure-csv解析成这个函数

When I run a 221 line .csv file -- parsed with clojure-csv -- into this function

(defn test-key-inclusion
    "Accepts csv-data param and an index, a second csv-data param and an index,
     and searches the second csv-data instances' rows (at index) to see if
     the first file's data is located in the second csv-data instance."

    [csv-data1 pkey-idx1 csv-data2 pkey-idx2 lnam-idx fnam-idx]

    (reduce
        (fn [out-log csv-row1]
            (let [cmp-val (nth csv-row1 pkey-idx1 nil)
                  lnam (nth csv-row1 lnam-idx nil)
                  fnam (nth csv-row1 fnam-idx)
                  temp-rc (first (key-pres? cmp-val pkey-idx2 csv-data2))]

            (concat out-log (sorted-map cmp-val (vector lnam fnam)))))
         {}
         csv-data1))

然后打印结果,一切正常。

and then print the result, everything's fine.

如果我运行2672行。 csv文件 - 也通过clojure-csv解析 - 然后尝试打印它,我得到一个堆栈溢出错误 - 线程主java.lang.StackOverflowError

If I run a 2672 line .csv file -- also parsed with clojure-csv -- through the function above and then try to print it, I get a stack overflow error -- Exception in thread "main" java.lang.StackOverflowError

所以我的问题是:

1)应该在lazy-seq中调用这个函数来解决我的问题吗?

1) Should wrapping the call to this function inside lazy-seq cure my problem?

2)我不想要一个列表,所以将lazy-seq调用包装在一个vec中
我的序列回到一个向量而不实现内存中的整个序列,

2) I don't want a list, so will wrapping the lazy-seq call inside a vec turn my sequence back into a vector without realizing the whole sequence in memory, that is make the lazy-seq un-lazy again?

推荐答案

1)我期望使序列惰性不帮助,因为打印将评估实现它在打印之前。而是尝试 doseq (map print my-seq)以较小的块打印。

1) i expect that making the sequence lazy not to help because print will evaluate realize it before printing it. instead try doseq or (map print my-seq) to print it in smaller chunks.

2)yes wrap在vec中会给你你想要的:)虽然用包装你的reduce到全程。即:(reduce into [] [[1] [2] [3]]) - > [1 2 3]

2) yes wrapping it in vec will give you what you want :) though wrapping your reduce with an into would keep it a vector the whole time. ie: (reduce into [] [[1] [2] [3]] ) --> [1 2 3]

  (into out-log (sorted-map cmp-val (vector lnam fnam)))))

这篇关于如何将Reduce实现的序列变回Lazy向量序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:06