lazy-evaluation

How do experienced Haskell developers approach laziness at *design* time?

断了今生、忘了曾经 提交于 2020-01-10 06:43:07
问题 I'm an intermediate Haskell programmer with tons of experience in strict FP and non-FP languages. Most of my Haskell code analyzes moderately large datasets (10^6..10^9 things), so laziness is always lurking. I have a reasonably good understanding of thunks, WHNF, pattern matching, and sharing, and I've been able to fix leaks with bang patterns and seq, but this profile-and-pray approach feels sordid and wrong. I want to know how experienced Haskell programmers approach laziness at design

How do experienced Haskell developers approach laziness at *design* time?

孤街醉人 提交于 2020-01-10 06:41:25
问题 I'm an intermediate Haskell programmer with tons of experience in strict FP and non-FP languages. Most of my Haskell code analyzes moderately large datasets (10^6..10^9 things), so laziness is always lurking. I have a reasonably good understanding of thunks, WHNF, pattern matching, and sharing, and I've been able to fix leaks with bang patterns and seq, but this profile-and-pray approach feels sordid and wrong. I want to know how experienced Haskell programmers approach laziness at design

Is Haskell's mapM not lazy?

偶尔善良 提交于 2020-01-09 03:49:30
问题 UPDATE: Okay this question becomes potentially very straightforward. q <- mapM return [1..] Why does this never return? Does mapM not lazily deal with infinite lists? The code below hangs. However, if I replace line A by line B, it doesn't hang anymore. Alternatively, if I preceed line A by a "splitRandom $", it also doesn't hang. Q1 is: Is mapM not lazy? Otherwise, why does replacing line A with line B "fix this" code? Q2 is: Why does preceeding line A with splitRandom "solve" the problem?

How to process lazily two strings at once?

三世轮回 提交于 2020-01-07 03:56:07
问题 Suppose I need a function to filter out characters of chars from a string str and then take only k first characters from the result: def cleanTrim(str: String, chars: Set[Char], k: Int): String = str.filterNot(chars).take(k) This implementation is suboptimal since it needlessly scans the whole string. In order to optimize it we can use view or event Stream to scan the input lazily, e.g: def cleanTrim(str: String, chars: Set[Char], k: Int): String = str.view.foldLeft("") { case (r, c) => if

Does dask dataframe apply preserve rows order?

泪湿孤枕 提交于 2020-01-06 06:33:23
问题 I am considering using a closure with the current state, to compute the rolling window (which in my case is of width 2), to answer my own question, which I have recently posed. Something on the lines of: def test(init_value): def my_fcn(x,y): nonlocal init_value actual_value = (x + y) * init_value init_value = actual_value return init_value return my_fcn where my_fcn is a dummy function used for testing. Therefore the function might be initialised thorugh actual_fcn = test(0); where we assume

NHibernate Lazy Loading Behaviour

走远了吗. 提交于 2020-01-04 05:12:19
问题 I was reading this article on Nhibernate Lazy Loading http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx and it uses and example of a class structure like this: The article then shows the following code: using (ISession session = SessionFactory.OpenSession()) { var fromDb = session.Get<Order>(_order.Id); int sum = 0; foreach (var line in fromDb.OrderLines) { // just some dummy code to force loading of order line sum += line.Amount; } } It then goes on to talk about: the n+1

Does Haskell/Frege ever recalcuate elements of a lazy list?

删除回忆录丶 提交于 2020-01-04 03:54:08
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Does Haskell/Frege ever recalcuate elements of a lazy list?

╄→гoц情女王★ 提交于 2020-01-04 03:54:05
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Getting stack overflow error when trying Haskell-style lazy evaluation in Scala

你说的曾经没有我的故事 提交于 2020-01-03 17:17:07
问题 To practice, I'm writing some useless methods/functions in Scala. I'm trying to implement a fibonacci sequence function. I wrote one in Haskell to use as a reference (so I don't just end up writing it Java-style). What I've come up with in Haskell is: fib a b = c : (fib b c) where c = a+b Then I can do this: take 20 (fib 0 1) [1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946] So I tried translating this to Scala: def fib(a:Int, b:Int):List[Int] = { val c = a+b c :: fib(b

How to write a lazy, variable argument version of “orElse”

非 Y 不嫁゛ 提交于 2020-01-03 16:52:11
问题 Is it possible to write a generalised orElse method from Option that takes a variable number of arguments? That is, instead of: lazy val o1 = { println("foo"); None } lazy val o2 = { println("bar"); Some("bar") } lazy val o3 = { println("baz"); Some("baz") } // ... o1 orElse o2 orElse o3 // orElse ... You could use: orElse(o1, o2, o3) //, ... 回答1: According to the The Scala Language Specification (4.6 Function Declarations and Definitions) you cannot define varargs by-name parameters: