lazy-evaluation

enquo() inside a magrittr pipeline

我是研究僧i 提交于 2019-12-23 19:24:50
问题 I just would like to understand what's going wrong here. In the first case (working), I assign the enquo() -ted argument to a variable, in the second case, I use the enquoted argument directly in my call to mutate . library("dplyr") df <- tibble(x = 1:5, y= 1:5, z = 1:5) # works myfun <- function(df, transformation) { my_transformation <- rlang::enquo(transformation) df %>% gather("key","value", x,y,z) %>% mutate(value = UQ(my_transformation)) } myfun(df,exp(value)) # does not work myfun_2 <-

Haskell is evaluating much faster then I thought it would (no complaints)

本秂侑毒 提交于 2019-12-23 17:43:48
问题 I wrote the following to check if a number is prime: factorsOf :: Int -> [Int] factorsOf n = [factor | factor <- [2..sqrtOfN], n `rem` factor == 0] where sqrtOfN = round . sqrt $ fromIntegral $ n+1 isPrime :: Int -> Bool isPrime n | factorsOf n == [] = True | otherwise = False and it works, but I noticed something weird. If I run factorsOf on a large number (say 100000001), it takes a few seconds to calculate all the factors. If I run isPrime on the same number though, it will return almost

Evaluation of 'and' clause with regards to laziness

蹲街弑〆低调 提交于 2019-12-23 13:23:04
问题 I don't understand why the following code behaves the way it does: myand :: Bool -> Bool -> Bool myand True True = True myand _ _ = False containsAandB :: String -> IO Bool containsAandB s = do containsA <- do putStrLn $ "Check if 'a' is in " ++ s return $ 'a' `elem` s containsB <- do putStrLn $ "Check if 'b' is in " ++ s return $ 'b' `elem` s return $ containsA `myand` containsB This is the output when I test the function: *Main> containsAandB "def" Check if 'a' is in def Check if 'b' in in

Python lazy iterator

試著忘記壹切 提交于 2019-12-23 12:09:59
问题 I am trying to understand how and when iterator expressions get evaluated. The following seems to be a lazy expression: g = (i for i in range(1000) if i % 3 == i % 2) This one, however fails on construction: g = (line.strip() for line in open('xxx', 'r') if len(line) > 10) I do not have the file named 'xxx'. However, since this thing is lazy, why is it failing so soon? Thanks. EDI: Wow, I made a lazy one! g = (line.strip() for i in range(3) for line in open(str(i), 'r')) 回答1: From the

Can I read n files lazily as a single IO operation in Haskell?

99封情书 提交于 2019-12-23 10:49:18
问题 How can I read multiple files as a single ByteString lazily with constant memory? readFiles :: [FilePath] -> IO ByteString I currently have the following implementation but from what I have seen from profiling as well as my understanding I will end with n-1 of the files in memory. readFiles = foldl1 joinIOStrings . map ByteString.readFile where joinIOStrings ml mr = do l <- ml r <- mr return $ l `ByteString.append` r I understand that the flaw here is that I am applying the IO actions then

Examples of lazy evaluation techniques in Perl 5?

别等时光非礼了梦想. 提交于 2019-12-23 10:34:28
问题 I find that one of the most interesting features of both Haskell and Perl6 is the ability to defer calculating values until they are actually needed. Perl5 on the other hand likes to do everything immediately, but from what I can tell, contains all of the necessary primitives for lazy evaluation. Those are: taking a reference to @_ in a subroutine creates an array reference that is aliased to the identifiers in its argument list, even if some of those identifiers do not contain values yet.

Understanding evaluation of input arguments of functions

扶醉桌前 提交于 2019-12-23 08:55:07
问题 I am reading Advanced R by Hadley Wickham where some very good exercises are provided. One of them asks for description of this function: f1 <- function(x = {y <- 1; 2}, y = 0) { x + y } f1() Can someone help me to understand why it returns 3? I know there is something called lazy evaluation of the input arguments, and e.g. another exercise asks for description of this function f2 <- function(x = z) { z <- 100 x } f2() and I correctly predicted to be 100; x gets value of z which is evaluated

First bracketed assignment is as time-consuming as full assignment?

偶尔善良 提交于 2019-12-23 07:56:06
问题 Regarding this answer in: What exactly is copy-on-modify semantics in R, and where is the canonical source? We can see that, at the first time a vector is altered with '[<-' , R copies the entire vector even if only a single entry is to be modifed. At the second time, however, the vector is altered "in place". This is noticeable without inspecting the address of the objects if we measure the time to create and modify a large vector: > system.time(a <- rep(1L, 10^8)) user system elapsed 0.15 0

How does Clojure's laziness interact with calls to Java/impure code?

拥有回忆 提交于 2019-12-23 07:52:35
问题 We stumbled upon an issue in our code today, and couldn't answer this Clojure question: Does Clojure evaluate impure code (or calls to Java code) strictly or lazily? It seems that side-effects + lazy sequences can lead to strange behavior. Here's what we know that led to the question: Clojure has lazy sequences: user=> (take 5 (range)) ; (range) returns an infinite list (0 1 2 3 4) And Clojure has side-effects and impure functions: user=> (def value (println 5)) 5 ; 5 is printed out to screen

Disable Lazy Loading in Hibernate

可紊 提交于 2019-12-23 07:50:39
问题 How do I disable lazy loading in Hibernate? I am using persistence annotations, not an hbm xml file. I am fetching a single object by ID and want all properties loaded. The session is closed before I use the object. Thanks! 回答1: You need to annotate the properties that you want non-lazy loaded with FetchType.EAGER @ManyToOne(fetch = FetchType.EAGER) You see, it isn't the object that you are loading that is lazy loaded. Rather, that object's associations are lazy, and you need to tell them not