lazy-sequences

Clojure printing lazy sequence

ε祈祈猫儿з 提交于 2019-11-26 18:33:54
问题 I'm trying to print out my binary tree but Clojure is giving me a hard time printing out the sequences properly. So, I have a list of nodes '(1 2 3) for example. In each iteration I want to print out the node with a number of spaces before and after each element. (defn spaces [n] (apply str (repeat n " "))) Great, this seems to work. So, suppose I have a list of nodes '(:a :b :c) I want to print out on one line, with as said, the spaces. (println (map #(str (spaces before) % (spaces (dec

How do I avoid Clojure's chunking behavior for lazy seqs that I want to short circuit?

对着背影说爱祢 提交于 2019-11-26 12:19:14
问题 I have a long, lazy sequence that I want to reduce and test lazily. As soon as two sequential elements are not = (or some other predicate) to each other, I want to stop consuming the list, which is expensive to produce. Yes, this sounds like take-while , but read further. I wanted to write something simple and elegant like this (pretending for a minute that every? works like reduce ): (every? = (range 100000000)) But that does not work lazily and so it hangs on infinite seqs. I discovered

How Are Lazy Sequences Implemented in Clojure?

我的未来我决定 提交于 2019-11-26 12:07:24
问题 I like Clojure. One thing that bothers me about the language is that I don\'t know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for. How does it do this? What makes lazy sequences so efficient that they don\'t consume much stack? How come you can wrap recursive calls in a lazy sequence and no longer get a stack over flow for large computations? What resources do lazy sequences consume to do what it does

Understanding a recursively defined list (fibs in terms of zipWith)

痞子三分冷 提交于 2019-11-26 10:19:37
问题 I\'m learning Haskell, and came across the following code: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) which I\'m having a bit of trouble parsing, in terms of how it works. It\'s very neat, I understand that nothing more is needed, but I\'d like to understand how Haskell manages to \"fill in\" fibs when I write: take 50 fibs Any help? Thanks! 回答1: I'll give a bit of an explanation of how it works internally. First, you must realise that Haskell uses a thing called a thunk for its values. A

Lazy lists in Prolog?

你说的曾经没有我的故事 提交于 2019-11-26 08:25:30
问题 Is it possible to have lazy lists in Prolog? Something like the following: ones([1 | Y]) :- ones(Y). Although this obviously doesn\'t work as it\'s written. 回答1: Markus Triska placed here in public domain some code worth to study: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Prolog stream/lazy list demonstration Written 2005 by Markus Triska (triska@gmx.at) Public domain code. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ The

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

核能气质少年 提交于 2019-11-26 06:28:58
问题 Both of these interfaces define only one method public operator fun iterator(): Iterator<T> Documentation says Sequence is meant to be lazy. But isn\'t Iterable lazy too (unless backed by a Collection )? 回答1: The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T> . For Sequence<T> , the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { .

Recursive function causing a stack overflow

夙愿已清 提交于 2019-11-26 02:58:18
问题 I am trying to write a simple sieve function to calculate prime numbers in clojure. I\'ve seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am just trying to write a very simple (and slow) sieve. Here is what I have come up with: (defn sieve [potentials primes] (if-let [p (first potentials)] (recur (filter #(not= (mod % p) 0) potentials) (conj primes p)) primes)) For small ranges it works fine, but causes a stack overflow for large