在clojure中使用一个惰性的字符串集合分区 [英] Partitioning in clojure with a lazy collection of strings

查看:90
本文介绍了在clojure中使用一个惰性的字符串集合分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字符串集合开始:

(def str-coll ["abcd" "efgh" "jklm"])

目标是从字符串集合的头部提取特定数量的字符,生成字符串的分区分组。这是所需的行为:

The goal is to extract off a specific number of characters from the head of the string collection, generating a partitioned grouping of strings. This is the desired behavior:

(use '[clojure.contrib.str-utils2 :only (join)])
(partition-all 3 (join "" str-coll))

((\a \b \c) (\d \e \f) (\g \h \j) (\k \l \m))

,使用连接力评估整个集合,这在处理非常大的字符串集合时导致内存问题。我的具体使用情况是从解析分隔记录的大文件生成的惰性集合生成字符串子集:

However, using join forces evaluation of the entire collection, which causes memory issues when dealing with very large collections of strings. My specific use case is generating subsets of strings from a lazy collection generated by parsing a large file of delimited records:

(defn file-coll [in-file]
  (->> (line-seq (reader in-file))
    (partition-by #(.startsWith ^String % ">"))
    (partition 2))))

http://stackoverflow.com/questions/3303848/improving-clojure-lazy-seq-usage-for-iterative-text-parsing\">此问题。我尝试了reduce,分区和连接的组合,但不能想出正确的方法从第一个字符串的头部拉扯字符,并懒惰地评估后续字符串的需要。非常感谢任何想法或指针。

and is building on work from this previous question. I've tried combinations of reduce, partition and join but can't come up with the right incantation to pull characters from the head of the first string and lazily evaluate subsequent strings as needed. Thanks much for any ideas or pointers.

推荐答案

不太确定你打算做什么,

Not quite sure what you're going for, but the following does what your first example does, and does so lazily.

为了清楚起见,一步一步:

Step-by-step for clarity:


user=> (def str-coll ["abcd" "efgh" "jklm"])
#'user/str-coll
user=> (map seq str-coll)
((\a \b \c \d) (\e \f \g \h) (\j \k \l \m))
user=> (flatten *1)
(\a \b \c \d \e \f \g \h \j \k \l \m)
user=> (partition 3 *1)
((\a \b \c) (\d \e \f) (\g \h \j) (\k \l \m))

现在:


(->> str-coll 
  (map seq)
  flatten
  (partition 3))

这篇关于在clojure中使用一个惰性的字符串集合分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆