lazy-evaluation

Clojure: rest vs. next

橙三吉。 提交于 2019-12-17 22:12:21
问题 I'm having a hard time understanding the difference between rest and next in Clojure. The official site's page on laziness indicates that the preference should probably be to use rest , but it doesn't really explain clearly the difference between the two. Can anybody provide some insight? 回答1: As the page you linked described, next is stricter than (the new behaviour of) rest because it needs to evaluate the structure of the lazy cons to know whether to return nil or a seq. rest on the other

How to expand an ellipsis (…) argument without evaluating it in R

别说谁变了你拦得住时间么 提交于 2019-12-17 19:44:26
问题 I need a function that accepts an arbitrary number of arguments and stores them in a variable as an expression without evaluating them. I managed to do it with match.call but it seems a little "kludgy". foo <- function(...) { expr <- match.call() expr[[1]] <- expression expr <- eval(expr) # do some stuff with expr return(expr) } > bla Error: object 'bla' not found > foo(x=bla, y=2) expression(x = bla, y = 2) Clarification To clarify, I'm asking how to write a function that behaves like

Do Immutable.js or Lazy.js perform short-cut fusion?

妖精的绣舞 提交于 2019-12-17 17:32:28
问题 First, let me define what is short-cut fusion for those of you who don't know. Consider the following array transformation in JavaScript: var a = [1,2,3,4,5].map(square).map(increment); console.log(a); function square(x) { return x * x; } function increment(x) { return x + 1; } Here we have an array, [1,2,3,4,5] , whose elements are first squared, [1,4,9,16,25] , and then incremented [2,5,10,17,26] . Hence, although we don't need the intermediate array [1,4,9,16,25] , we still create it.

Is Scala mapValues lazy?

强颜欢笑 提交于 2019-12-17 16:56:17
问题 When I call System.err.println("Before") System.err.flush() val foo: Map[Int, T] = t mapValues (fn(_)) System.err.println(foo.head) //prevent optimiser from delaying the construction of 'foo' System.err.println("After") System.err.flush() with fn having a debug print statement inside, I get this output: Before ...head item... After ...debug print statement from fn... ...debug print statement from fn... I don't understand why the debug print statements are being called after "After" is printed

Does Java have lazy evaluation?

倖福魔咒の 提交于 2019-12-17 16:33:38
问题 I know that Java has smart/lazy evaluation in this case: public boolean isTrue() { boolean a = false; boolean b = true; return b || (a && b); // (a && b) is not evaluated since b is true } But what about: public boolean isTrue() { boolean a = isATrue(); boolean b = isBTrue(); return b || a; } Is isATrue() called even if isBTrue() returns true? 回答1: In Java (and other C-like languages), this is referred to as short-circuit evaluation. * And yes, in the second example isATrue is always called.

Lazy loading images how

烂漫一生 提交于 2019-12-17 16:18:27
问题 I am developing an eshop .At products page based on category i putted some javascript based filtering. However a problem arises if a category has a lot of products. This link has something similar i do ... http://www.snowandrock.com/sunglasses/snowboard/fcp-category/list?resetFilters=true How ever this page is painfully slow and is over 2mb !!! Every product for me needs half killobyte but the image is the problem.. So i am looking how to lazy load images.. Since my page has pagination unlike

Removing syntactic sugar: List comprehension in Haskell

懵懂的女人 提交于 2019-12-17 12:16:34
问题 Can I unsugar list comprehension in this expression: [(i,j) | i <- [1..4], j <- [i+1..4]] This is the output: [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] How can I, with map, filter and so on, write that piece of code? edit Here an other: [(i,j,k) | i <- [1..6], j <- [i+1..6],k <- [j+1..6]] This is the output: [(1,2,3),(1,2,4),(1,2,5),(1,2,6),(1,3,4),(1,3,5),(1,3,6),(1,4,5),(1,4,6),(1,5,6),(2,3,4),(2,3,5),(2,3,6),(2,4,5),(2,4,6),(2,5,6),(3,4,5),(3,4,6),(3,5,6),(4,5,6)] 回答1: List comprehensions (in

Removing syntactic sugar: List comprehension in Haskell

﹥>﹥吖頭↗ 提交于 2019-12-17 12:16:23
问题 Can I unsugar list comprehension in this expression: [(i,j) | i <- [1..4], j <- [i+1..4]] This is the output: [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] How can I, with map, filter and so on, write that piece of code? edit Here an other: [(i,j,k) | i <- [1..6], j <- [i+1..6],k <- [j+1..6]] This is the output: [(1,2,3),(1,2,4),(1,2,5),(1,2,6),(1,3,4),(1,3,5),(1,3,6),(1,4,5),(1,4,6),(1,5,6),(2,3,4),(2,3,5),(2,3,6),(2,4,5),(2,4,6),(2,5,6),(3,4,5),(3,4,6),(3,5,6),(4,5,6)] 回答1: List comprehensions (in

Pattern for lazy thread-safe singleton instantiation in java

試著忘記壹切 提交于 2019-12-17 10:35:14
问题 the lazy thread-safe singleton instantion is kinda not easy to understand to every coder, so i wanted to create a class in our enterprise framework that would do the job. What do you think about it? Do you see something bad about it? Is there something similar like in Apache Commons? How can i make it better? Supplier.java public interface Supplier<T> { public T get(); } LazyThreadSafeInstantiator.java public class LazyThreadSafeInstantiator<T> implements Supplier<T> { private final Supplier

`def` vs `val` vs `lazy val` evaluation in Scala

丶灬走出姿态 提交于 2019-12-17 08:14:05
问题 Am I right understanding that def is evaluated every time it gets accessed lazy val is evaluated once it gets accessed val is evaluated once it gets into the execution scope? 回答1: Yes, though for the 3rd one I would say "when that statement is executed", because, for example: def foo() { new { val a: Any = sys.error("b is " + b) val b: Any = sys.error("a is " + a) } } This gives "b is null" . b is never evaluated and its error is never thrown. But it is in scope as soon as control enters the