lazy-evaluation

Lazy vs eager evaluation and double linked list building

狂风中的少年 提交于 2019-12-06 22:38:47
问题 I can't sleep! :) I've written small program building double linked list in Haskell. The basic language's property to make it was lazy evaluation (see the bunch of code below). And my question is can I do the same in a pure functional language with eager evaluation or not? In any case, what properties eager functional language must have to be able to build such structure (impurity?)? import Data.List data DLList a = DLNull | DLNode { prev :: DLList a , x :: a , next :: DLList a } deriving

In Haskell, will calling length on a Lazy ByteString force the entire string into memory?

青春壹個敷衍的年華 提交于 2019-12-06 21:17:06
问题 I am reading a large data stream using lazy bytestrings, and want to know if at least X more bytes is available while parsing it. That is, I want to know if the bytestring is at least X bytes long. Will calling length on it result in the entire stream getting loaded, hence defeating the purpose of using the lazy bytestring? If yes, then the followup would be: How to tell if it has at least X bytes without loading the entire stream? EDIT: Originally I asked in the context of reading files but

Irrefutable pattern does not leak memory in recursion, but why?

女生的网名这么多〃 提交于 2019-12-06 19:22:55
问题 The mapAndSum function in the code block all the way below combines map and sum (never mind that another sum is applied in the main function, it just serves to make the output compact). The map is computed lazily, while the sum is computed using an accumulating parameter. The idea is that the result of map can be consumed without ever having the complete list in memory, and (only) afterwards the sum is available "for free". The main function indicates that we had a problem with irrefutable

F# PSeq.iter does not seem to be using all cores

时光毁灭记忆、已成空白 提交于 2019-12-06 19:16:26
问题 I've been doing some computationally intensive work in F#. Functions like Array.Parallel.map which use the .Net Task Parallel Library have sped up my code exponentially for a really quite minimal effort. However, due to memory concerns, I remade a section of my code so that it can be lazily evaluated inside a sequence expression (this means I have to store and pass less information). When it came time to evaluate I used: // processor and memory intensive task, results are not stored let

Idris eager evaluation

假装没事ソ 提交于 2019-12-06 16:36:31
问题 In Haskell , I might implement if like this: if' True x y = x if' False x y = y spin 0 = () spin n = spin (n - 1) This behaves how I expect : haskell> if' True (spin 1000000) () -- takes a moment haskell> if' False (spin 1000000) () -- immediate In Racket , I could implement a flawed if like this: (define (if2 cond x y) (if cond x y)) (define (spin n) (if (= n 0) (void) (spin (- n 1)))) This behaves how I expect : racket> (if2 #t (spin 100000000) (void)) -- takes a moment racket> (if2 #f

prefix(_ maxLength:) is type-erased when used with a struct that conforms to LazySequenceProtocol

亡梦爱人 提交于 2019-12-06 05:07:45
prefix(_ maxLength:) returns a type-erased Sequence in the following code EXAMPLE : public struct CycleIterator <Base: Sequence>: IteratorProtocol { public typealias Element = Base.Element private var sequence: Base private var iterator: Base.Iterator internal init (_ sequence: Base) { self.sequence = sequence self.iterator = self.sequence.makeIterator() } public mutating func next () -> Element? { var next = self.iterator.next() if next == nil { self.iterator = self.sequence.makeIterator() next = self.iterator.next() } return next } } public struct LazyCycleSequence <Base:

If a python iterator returns iterable objects, how can I chain those objects into one big iterator?

廉价感情. 提交于 2019-12-06 04:24:23
问题 I'll give a simplified example here. Suppose I have an iterator in python, and each object that this iterator returns is itself iterable. I want to take all the objects returned by this iterator and chain them together into one long iterator. Is there a standard utility to make this possible? Here is a contrived example. x = iter([ xrange(0,5), xrange(5,10)]) x is an iterator that returns iterators, and I want to chain all the iterators returned by x into one big iterator. The result of such

Is there a lazy mapM?

牧云@^-^@ 提交于 2019-12-06 04:04:25
问题 At first glance I thought these two functions would work the same: firstM _ [] = return Nothing firstM p (x:xs) = p x >>= \r -> if r then return (Just x) else firstM p xs firstM' p xs = fmap listToMaybe (mapM p xs) But they don't. In particular, firstM stops as soon as the first p x is true. But firstM' , because of mapM , needs the evaluate the whole list. Is there a "lazy mapM " that enables the second definition, or at least one that doesn't require explicit recursion? 回答1: There isn't

lazy attribute in Swift equivalent to lazy Init getter in Objective C

只谈情不闲聊 提交于 2019-12-06 02:54:51
问题 Is the lazy attribute in Swift equivalent to overriding the getter with a lazy loading pattern in Objective C? 回答1: From the docs: A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy attribute before its declaration. So, mostly, yes. You must always declare a lazy property as a variable (with the var keyword), because its initial value may not be retrieved until after instance

Lazy Quicksort in Scala

一笑奈何 提交于 2019-12-06 02:39:57
问题 Is it possible to do this sort of thing in Scala? 回答1: def quicksort[A](xs: Stream[A])(implicit o: Ordering[A]): Stream[A] = { import o._ if (xs.isEmpty) xs else { val (smaller, bigger) = xs.tail.partition(_ < xs.head) quicksort(smaller) #::: xs.head #:: quicksort(bigger) } } It can be done with views as well, though it's bound to be much slower: def quicksort[A](xs: List[A])(implicit o: Ordering[A]) = { import o._ def qs(xs: SeqView[A, List[A]]): SeqView[A, Seq[_]] = if (xs.isEmpty) xs else