lazy-evaluation

why foldl is not short circuiting with andFn function?

99封情书 提交于 2019-12-22 05:32:14
问题 My understanding is that foldl and foldr executes like : foldl f a [1..30] => (f (f (f ... (f a 1) 2) 3) ... 30) and foldr f a [1..30] => (f 1 (f 2 (f 3 (f ....(f 30 a)))))..) so.. foldr (&&) False (repeat False) can shortciruit as outermost f sees (&&) False ((&&) False (....)) sees first argument as false and does not need to evaluate the second argument (which is a large thunk). so what happens with andFn :: Bool -> Bool -> Bool andFn _ False = False andFn x True = x and foldl andFn True

java cache hashmap expire daily

末鹿安然 提交于 2019-12-22 05:28:20
问题 I would like to have a HashMap<String, String> , that every day at midnight, the cache will expire. Please note that it's J2EE solution so multiple threads can access it. What is the best way to implement it in Java? 回答1: Although the other suggestions can work to time the expiration, it is important to note that : Expiration of the hashmap can be done lazily i.e. without any monitor threads ! The simplest way to implement expiration is thus as follows : 1) Extend hash map , and create a

How do I write a constant-space length function in Haskell?

…衆ロ難τιáo~ 提交于 2019-12-22 04:44:08
问题 The canonical implementation of length :: [a] -> Int is: length [] = 0 length (x:xs) = 1 + length xs which is very beautiful but suffers from stack overflow as it uses linear space. The tail-recursive version: length xs = length' xs 0 where length' [] n = n length' (x:xs) n = length xs (n + 1) doesn't suffer from this problem, but I don't understand how this can run in constant space in a lazy language. Isn't the runtime accumulating numerous (n + 1) thunks as it moves through the list?

Programming with dplyr and lazyeval

浪子不回头ぞ 提交于 2019-12-22 04:26:08
问题 I am having issues refactoring dplyr in a way that preserves non-standard evaluation. Lets say I want to create a function that always selects and renames. library(lazyeval) library(dplyr) df <- data.frame(a = c(1,2,3), f = c(4,5,6), lm = c(7, 8 , 9)) select_happy<- function(df, col){ col <- lazy(col) fo <- interp(~x, x=col) select_(df, happy=fo) } f <- function(){ print('foo') } select_happy() is written according to the answer to this post Refactor R code when library functions use non

How do laziness and I/O work together in Haskell?

北城以北 提交于 2019-12-22 04:09:59
问题 I'm trying to get a deeper understanding of laziness in Haskell. I was imagining the following snippet today: data Image = Image { name :: String, pixels :: String } image :: String -> IO Image image path = Image path <$> readFile path The appeal here is that I could simply create an Image instance and pass it around; if I need the image data it would be read lazily - if not, the time and memory cost of reading the file would be avoided: main = do image <- image "file" putStrLn $ length $

How does this list comprehension over the inits of itself work?

你离开我真会死。 提交于 2019-12-22 04:06:07
问题 In the #haskell IRC channel someone asked Is there a succinct way to define a list where the nth entry is the sum of the squares of all entries before? I thought this sounded like a fun puzzle, and defining infinite lists recursively is one of those things I really need to practise. So I fired up GHCi and started playing around with recursive definitions. Eventually, I managed to get to λ> let xs = 1 : [sum (map (^2) ys) | ys <- inits xs, not (null ys)] which seems to produce the correct

Swift Struct with Lazy, private property conforming to Protocol

ぐ巨炮叔叔 提交于 2019-12-22 03:51:20
问题 First, I have a protocol that defines only a few, readonly properties, ex: protocol Example { var var1:String { get } var varArray:[String] { get } } I then want to create a struct that conforms to that protocol. The problem I'm running into, is that I have two conflicting requirements: The properties need to be lazily generated. The properties are related and need to be generated together. I can't seem to find a way to do this. The closest I've come is something like this: struct AStruct :

Haskell - strict vs non-strict with foldl

♀尐吖头ヾ 提交于 2019-12-22 03:38:11
问题 I have a question concerning the definition of strict vs non-strict. The Haskell wiki-book for Laziness (http://en.wikibooks.org/wiki/Haskell/Laziness), under the section "Black-box strictness analysis", makes the following assertion: [Assuming a function f which takes a single parameter.] The function f is a strict function if, and only if, f undefined results in an error being printed and the halting of our program. The wiki contrasts const with id , showing a non-strict and strict function

Lazy CSV Filtering / Parsing - Increasing Performance

有些话、适合烂在心里 提交于 2019-12-21 20:39:35
问题 Lazy Filtering CSV Files I had the need to filter through millions of log records, stored as numerous CSV files. The size of the records greatly exceeded my available memory so I wanted to go with a lazy approach. Java 8 Streams API With jdk8 we have the Streams API which paired with Apache commons-csv allows us to easily accomplish this. public class LazyFilterer { private static Iterable<CSVRecord> getIterable(String fileName) throws IOException { return CSVFormat .DEFAULT

Can the use of C++11's 'auto' deteriorate performance or even break the code?

試著忘記壹切 提交于 2019-12-21 19:45:26
问题 This question is the opposite of an existing question "Can the use of C++11's 'auto' improve performance?" One of the answers to that question indicated that usage of auto can have not only positive but also negative effects. I believe we need a separate question, with answers focusing on that side of auto . 回答1: With auto there is no conversion at the variable declaration+initialization line. But if such conversion must happen anyway, it better happen once during initialization than multiple