问题标签 [lazy-sequences]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
311 浏览

clojure - Find the elements of a LazySeq that have been realized

I have a LazySeq of connections that are created when realized. If an exception occurs while attempting to create a connection, I'd like to iterate through all of the connections that have already been realized in the LazySeq and close them. Something like:

This doesn't quite work though since close-connections will attempt to realize the connections again. I only want to close connections that have been realized, not realize additional connections. Any ideas for doing this?

0 投票
3 回答
219 浏览

recursion - “分区”函数实现中的递归

我正在随机阅读 Clojure 源代码,我看到分区函数是根据递归定义的,而不使用 "recur"

这样做有什么理由吗?

0 投票
1 回答
254 浏览

clojure - Clojure 修改 LazySeq

我继承了一些执行以下操作的 Java 代码:

1) 它从 Clojure 接收一个 LazySeq 对象(由许多 PersistentHashMap 对象组成)

2) 然后它将相同的 LazySeq 对象(未更改)传递回 Clojure 脚本,在该脚本中将其转换为字符串并传递回 Java

问题是在步骤 (1) 之后和步骤 (2) 之前的 Java 代码中,我需要修改 LazySeq 中的一些 PersistentHashMap 对象,然后继续执行步骤 (2)。就像是:

LazySeq seq = clojureFunctionReturningLazySeq();

//update the elements of the sequence

String result = clojureFunctionReceivingLazySeq(seq);

我无法修改 Clojure 脚本本身,并且 LazySeq 的更新必须在 Java 代码中进行。我检查了 LazySeq API,但找不到修改(或添加)元素的方法。

谢谢,

克里斯

0 投票
5 回答
239 浏览

clojure - Partitioning across partitions in Clojure?

Here are some values. Each is a sequence of ascending (or otherwise grouped) values.

I can partition them each by value.

But that gets me 3 sequences of partitions. I want one single sequence of partitioned groups.

What I want to do is return a single lazy sequence of (potentially) lazy sequences that are the respective partitions joined. e.g. I want to produce this:

Note that not all values appear in all sequences (there is no 2 in the third vector).

This is of course a simplification of my problem. The real data is a set of lazy streams coming from very large files, so nothing can be realised. But I think the solution for the above question is the solution for my problem.

Feel free to edit the title, I wasn't quite sure how to express it.

0 投票
2 回答
472 浏览

clojure - line-seq 的行为会是什么?

doseq如果我迭代但保留第一个元素的一部分,我想了解惰性序列的行为。

我不想保留任何列表,我只想对每个元素执行副作用。我相信没有这个first-item就不会有问题。

我在我的程序中遇到了内存问题,我认为也许在序列的开头保留对某些内容的引用parsed-line意味着整个序列都被存储了。

这里定义的行为是什么?如果正在存储序列,是否有一种通用的方法来获取对象的副本并使序列的已实现部分能够被垃圾收集?

0 投票
2 回答
340 浏览

clojure - 将 Clojure 向量的内容提取为 LazySeq

我使用 Clojure 的 Java API,我有一个通过代码创建的持久向量:

后来填充了值。

我需要将此向量的内容提取为LazySeq. 我知道向量的方法seq()返回一个ISeq. 有没有办法把它转换ISeqLazySeq?

谢谢,

克里斯

0 投票
2 回答
154 浏览

clojure - clojure 惰性函数 - Clojure Koan

“迭代提供了无限的惰性序列”

所以我的问题是为什么它从 0 而不是 1 开始?如何理解这里的懒惰?

0 投票
1 回答
340 浏览

clojure - 执行惰性函数序列

我想知道如何强制评估一个惰性函数序列。

例如,如果我有一个返回整数 1 的函数:

我构建了这些函数的惰性序列:

我如何实际执行序列中的功能?

我已经阅读了如何在 Clojure 中将惰性序列转换为非惰性序列,这表明了 doall...但我不确定结果的去向?我期待 [1 1 1 1 1] 或类似的东西。

0 投票
2 回答
1615 浏览

clojure - mapcat breaking the lazyness

I have a function that produces lazy-sequences called a-function.

If I run the code:

it returns a lazy sequence as expected.

But when I run the code:

it breaks the lazyness of my function. In fact it turns that code into

So it needs to realize all the values from the map before concatenating those values.

What I need is a function that concatenates the result of a map function on demand without realizing all the map beforehand.

I can hack a function for this:

But I can't believe that clojure doesn't have something already done. Do you know if clojure has such feature? Only a few people and I have the same problem?

I also found a blog that deals with the same issue: http://clojurian.blogspot.com.br/2012/11/beware-of-mapcat.html

0 投票
4 回答
1497 浏览

f# - Infinite fibonacci sequence

I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated?