lazy-evaluation

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

两盒软妹~` 提交于 2019-12-29 07:28:08
问题 With the question Listing all the contents of a directory by breadth-first order results in low efficiencyI learned that the low efficiency is due to a strange behavior of the recursive monad functions. Try sequence $ map return [1..]::[[Int]] sequence $ map return [1..]::Maybe [Int] and ghci will fall into an endless calculation. If we rewrite the sequence function in a more readable form like follows: sequence' [] = return [] sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)} and

Enclosing variables within for loop

半世苍凉 提交于 2019-12-29 07:06:09
问题 So consider the following chunk of code which does not work as most people might expect it to #cartoon example a <- c(3,7,11) f <- list() #manual initialization f[[1]]<-function(x) a[1]+x f[[2]]<-function(x) a[2]+x f[[3]]<-function(x) a[3]+x #desired result for the rest of the examples f[[1]](1) # [1] 4 f[[3]](1) # [1] 12 #attempted automation for(i in 1:3) { f[[i]] <- function(x) a[i]+x } f[[1]](1) # [1] 12 f[[3]](1) # [1] 12 Note that we get 12 both times after we attempt to "automate". The

Return first non-null value

人盡茶涼 提交于 2019-12-29 03:58:07
问题 I have a number of functions: String first(){} String second(){} ... String default(){} Each can return a null value, except the default. each function can take different parameters. For example, first could take no arguments, second could take in a String, third could take three arguments, etc. What I'd like to do is something like: ObjectUtils.firstNonNull(first(), second(), ..., default()); The problem is that because of the function call, this does eager evaluation. Where'd I'd like to

Laziness and tail recursion in Haskell, why is this crashing?

孤街浪徒 提交于 2019-12-29 03:10:37
问题 I have this fairly simple function to compute the mean of elements of a big list, using two accumulators to hold the sum so far and the count so far: mean = go 0 0 where go s l [] = s / fromIntegral l go s l (x:xs) = go (s+x) (l+1) xs main = do putStrLn (show (mean [0..10000000])) Now, in a strict language, this would be tail-recursive, and there would be no problem. However, as Haskell is lazy, my googling has led me to understand that (s+x) and (l+1) will be passed down the recursion as

Haskell Lazy ByteString + read/write progress function

匆匆过客 提交于 2019-12-29 03:04:23
问题 I am learing Haskell Lazy IO. I am looking for an elegant way to copy a large file (8Gb) while printing copy progress to console. Consider the following simple program that copies a file silently. module Main where import System import qualified Data.ByteString.Lazy as B main = do [from, to] <- getArgs body <- B.readFile from B.writeFile to body Imgine there is a callback function you want to use for reporting: onReadBytes :: Integer -> IO () onReadBytes count = putStrLn $ "Bytes read: " ++

How are lazy val class variables implemented in Scala 2.10?

邮差的信 提交于 2019-12-28 05:58:30
问题 This answer to What's the (hidden) cost of Scala's lazy val? shows how they were implemented in Scala 2.7. But as the comments say, this must have changed since then, so I'm curious, what's the current (2.10) implementation of class lazy val variables? 回答1: Compiled this with scala 2.10.2: class Foo { lazy val bar = math.pow(5, 3) } Then decompiled the result with JD-GUI: import scala.math.package.; import scala.reflect.ScalaSignature; @ScalaSignature(bytes="\006\001e1A!\001\002\001\013\t

infinite lists, lazy evaluation and length

烂漫一生 提交于 2019-12-24 12:09:55
问题 Haskell noob here: I'm still trying to understand the mechanics of the language, so if my question is plain stupid, forgive me and point me to some link which I can learn from (I've searched awhile in similar topics here on stackoverflow, but still I can't get this). I came out with this function: chunks :: Int -> [a] -> [[a]] chunks n xs | length xs <= n = [xs] | otherwise = let (ch, rest) = splitAt n xs in ch:chunks n rest so that ghci> chunks 4 "abracadabra" ["abra","cada","bra"] ghci>

How can I create a Lazy C++ template class that handles types with no default constructor?

£可爱£侵袭症+ 提交于 2019-12-24 11:58:42
问题 Deleting this question in favor the following; an answer to which now handles classes with no default constructor: How to abstract lazy initialization in C++? In a nutshell, the code uses placement new/delete. See http://en.wikipedia.org/wiki/Placement_syntax for details... 回答1: Just use boost::optional<T> instead of pair of your members m_bInitialized and m_value . Probably you could just use boost::optional<T> instead of your template class Lazy ... If you really want to make it in your own

Haskell: Replace mapM in a monad transformer stack to achieve lazy evaluation (no space leaks)

余生长醉 提交于 2019-12-24 08:58:26
问题 It has already been discussed that mapM is inherently not lazy, e.g. here and here. Now I'm struggling with a variation of this problem where the mapM in question is deep inside a monad transformer stack. Here's a function taken from a concrete, working (but space-leaking) example using LevelDB that I put on gist.github.com: -- read keys [1..n] from db at DirName and check that the values are correct doRead :: FilePath -> Int -> IO () doRead dirName n = do success <- runResourceT $ do db <-

Can't call a lazy lambda function with parameters via boost::phoenix::function

我与影子孤独终老i 提交于 2019-12-24 03:37:25
问题 I tried to call boost::phoenix::function based on lambda function with parameters and failed. If I call it without parameters in such a way: const auto closure = [](){ cout<< "test" << endl; }; typedef decltype(closure) ClosureType; const boost::phoenix::function<ClosureType> lazyFunc (std::move(closure)); lazyFunc()(); all compiles nice. But when I declare at least one parameter of lambda: const auto closure = [](int& param) { cout<<"Test" << param << endl; }; typedef decltype(closure)