lazy-evaluation

Lazy List of Prime Numbers

徘徊边缘 提交于 2019-12-20 08:40:29
问题 How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality. 回答1: Here's a short Haskell function that enumerates primes from Literate Programs: primes :: [Integer] primes = sieve (2 : [3, 5..]) where sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0] Apparently, this is not the Sieve of Eratosthenes (thanks, Landei). I think it's still an instructive

Haskell: How does non-strict and lazy differ?

此生再无相见时 提交于 2019-12-20 07:59:09
问题 I often read that lazy is not the same as non-strict but I find it hard to understand the difference. They seem to be used interchangeably but I understand that they have different meanings. I would appreciate some help understanding the difference. I have a few questions which are scattered about this post. I will summarize those questions at the end of this post. I have a few example snippets, I did not test them, I only presented them as concepts. I have added quotes to save you from

Changing names of resulting variables in custom dplyr function

て烟熏妆下的殇ゞ 提交于 2019-12-20 02:17:32
问题 Background In order to speed up generating grouped summaries across multiple tables; as I'm doing most of that while in dplyr workflow, I've drafted a simple function that generates the desired metrics # Function to generate summary table generate_summary_tbl <- function(dataset, group_column, summary_column) { group_column <- enquo(group_column) summary_column <- enquo(summary_column) dataset %>% group_by(!!group_column) %>% summarise( mean = mean(!!summary_column), sum = sum(!!summary

Does Excel evaluate both result arguments supplied to the IF function?

懵懂的女人 提交于 2019-12-19 16:32:47
问题 Excel's if function takes three arguments, a condition, an if-true value, and an if-false value. Does Excel work out the value of all three arguments, or does it only work out the value of the condition and the corresponding result? Clarification: I'm not wondering what the result of the if will be, I'm wondering whether or not it calculates the value of all arguments before calculating the result of the function. This is equivalent to asking whether or not the if function uses lazy or strict

How do I cope with lazy iterators?

风格不统一 提交于 2019-12-19 10:23:20
问题 I'm trying to sort an array with a map() over an iterator. struct A { b: Vec<B>, } #[derive(PartialEq, Eq, PartialOrd, Ord)] struct B { c: Vec<i32>, } fn main() { let mut a = A { b: Vec::new() }; let b = B { c: vec![5, 2, 3] }; a.b.push(b); a.b.iter_mut().map(|b| b.c.sort()); } Gives the warning: warning: unused `std::iter::Map` that must be used --> src/main.rs:16:5 | 16 | a.b.iter_mut().map(|b| b.c.sort()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(unused_must_use)] on by

Avoid or delay evaluation of things which may not be used

不想你离开。 提交于 2019-12-19 07:28:09
问题 How can lazy evaluation be achieved in Python? A couple of simple examples: >>> def foo(x): ... print(x) ... return x ... >>> random.choice((foo('spam'), foo('eggs'))) spam eggs 'eggs' Above, we didn't really need to evaluate all the items of this tuple, in order to choose one. And below, the default foo() didn't really need to be computed unless the lookup key was actually missing from the dict: >>> d = {1: "one"} >>> d.get(2, foo("default")) default 'default' >>> d.get(1, foo("default"))

make a lazy var in scala

為{幸葍}努か 提交于 2019-12-19 05:34:17
问题 Scala does not permit to create laze vars, only lazy vals. It make sense. But I've bumped on use case, where I'd like to have similar capability. I need a lazy variable holder. It may be assigned a value that should be calculated by time-consuming algorithm. But it may be later reassigned to another value and I'd like not to call first value calculation at all. Example assuming there is some magic var definition lazy var value : Int = _ val calc1 : () => Int = ... // some calculation val

make a lazy var in scala

自古美人都是妖i 提交于 2019-12-19 05:34:11
问题 Scala does not permit to create laze vars, only lazy vals. It make sense. But I've bumped on use case, where I'd like to have similar capability. I need a lazy variable holder. It may be assigned a value that should be calculated by time-consuming algorithm. But it may be later reassigned to another value and I'd like not to call first value calculation at all. Example assuming there is some magic var definition lazy var value : Int = _ val calc1 : () => Int = ... // some calculation val

In Swift, why does assigning to a static variable also invoke its getter

て烟熏妆下的殇ゞ 提交于 2019-12-19 02:52:38
问题 I understand that in Swift, static vars are implicitly lazy: https://stackoverflow.com/a/34667272/1672161 But I'm not clear on why this happens: protocol HatType {} class Hat: HatType { init() { print("real hat") } } class MockHat: HatType { init() { print("mock hat") } } struct HatInjector { static var hat: HatType = Hat() } HatInjector.hat = MockHat() // Output: // real hat // mock hat What I'm seeing is that the assignment to the static var, is also invoking the getter in a sense. This isn

In Haskell, what's the difference between using takeWhile or using a “regular” inequality in this list comprehension?

痞子三分冷 提交于 2019-12-18 14:51:51
问题 I'm trying to learn me a Haskell (for great good), and one of the many different things I'm doing is trying to tackle some Project Euler problems as I'm going along to test my mettle. In doing some of the Fibonacci based problems, I stumbled on and started playing around with the recursive infinite list version of the Fibonacci sequence: fibs = 1 : 2 : zipWith (+) fibs (tail fibs) For one of the PE problems, I needed to extract the subsequence of even Fibonacci numbers less than 4,000,000. I