lazy-evaluation

When is my Haskell expression evaluated?

自作多情 提交于 2019-12-21 14:12:27
问题 If I define λ> data Bar = Bar Int deriving Show λ> data Foo = Foo Bar deriving Show and λ> let foo = trace "foo" Foo (trace "bar" Bar 100) λ> let two = trace "two" 2 λ> let g (Foo x) y = y then I think I understand why I get λ> g foo two foo two 2 But if I then repeat this, I get λ> g foo two two 2 and I don't understand why foo appears not to have been evaluated for the second invocation of g , especially since it is clearly not (yet) somehow already available, as I can verify with λ> foo

Set a value in a dict only if the value is not already set

半世苍凉 提交于 2019-12-21 07:55:38
问题 What is the most pythonic way to set a value in a dict if the value is not already set? At the moment my code uses if statements: if "timeout" not in connection_settings: connection_settings["timeout"] = compute_default_timeout(connection_settings) dict.get(key,default) is appropriate for code consuming a dict, not for code that is preparing a dict to be passed to another function. You can use it to set something but its no prettier imo: connection_settings["timeout"] = connection_settings

Clojure: lazy magic

社会主义新天地 提交于 2019-12-21 07:36:06
问题 Almost 2 identical programs to generate infinite lazy seqs of randoms. The first doesn't crash. The second crash with OutOfMemoryError exception. Why? ;Return infinite lazy sequence of random numbers (defn inf-rand[] (lazy-seq (cons (rand) (inf-rand)))) ;Never returns. Burns the CPU but won't crash and lives forever. (last (inf-rand)) But the following crash pretty quickly: ;Return infinite lazy sequence of random numbers (defn inf-rand[] (lazy-seq (cons (rand) (inf-rand)))) (def r1 (inf-rand

clojure deferred function execution

怎甘沉沦 提交于 2019-12-21 07:32:08
问题 Ok Here is what i am trying to do (defn addresses [person-id] ;addresses-retrival ) (defn person [id] (merge {:addresses (addresses id)} {:name "john"})) In the above person function i want addresses to be retrieved only on demand , like only when i do (:addresses (person 10)) and not when (person 10) I am not sure if i am going about this right, being new to clojure. 回答1: You can use delay. (defn person [id] (delay {:addresses (addresses id) :name "john"})) (person 2) will then return a

clojure deferred function execution

微笑、不失礼 提交于 2019-12-21 07:31:32
问题 Ok Here is what i am trying to do (defn addresses [person-id] ;addresses-retrival ) (defn person [id] (merge {:addresses (addresses id)} {:name "john"})) In the above person function i want addresses to be retrieved only on demand , like only when i do (:addresses (person 10)) and not when (person 10) I am not sure if i am going about this right, being new to clojure. 回答1: You can use delay. (defn person [id] (delay {:addresses (addresses id) :name "john"})) (person 2) will then return a

How does Log4j implement lazy argument evaluation?

假装没事ソ 提交于 2019-12-21 07:16:39
问题 Given the Java argument evaluation mechanism, how does Log4j implement lazy evaluation when formatting the message with curly brackets "to avoid the cost of parameter construction" when log is disabled? e.g. logger.debug("Entry number: {} is {}", i, entry[i]); 回答1: I guess what Log4j means, is that with the curly brackets, they avoid constructing a String when its not necessary (e.g. the Level is not Debug): With logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i])); The

Implementing a “LazyProperty” class - is this a good idea?

ぃ、小莉子 提交于 2019-12-21 04:31:17
问题 I often find myself writing a property that is evaluated lazily. Something like: if (backingField == null) backingField = SomeOperation(); return backingField; It is not much code, but it does get repeated a lot if you have a lot of properties. I am thinking about defining a class called LazyProperty: public class LazyProperty<T> { private readonly Func<T> getter; public LazyProperty(Func<T> getter) { this.getter = getter; } private bool loaded = false; private T propertyValue; public T Value

Implementing a “LazyProperty” class - is this a good idea?

落花浮王杯 提交于 2019-12-21 04:31:16
问题 I often find myself writing a property that is evaluated lazily. Something like: if (backingField == null) backingField = SomeOperation(); return backingField; It is not much code, but it does get repeated a lot if you have a lot of properties. I am thinking about defining a class called LazyProperty: public class LazyProperty<T> { private readonly Func<T> getter; public LazyProperty(Func<T> getter) { this.getter = getter; } private bool loaded = false; private T propertyValue; public T Value

Observing lazyness in Haskell

跟風遠走 提交于 2019-12-21 04:02:23
问题 Is it possible to write a Haskell function which depends on whether values are calculated already or are thunks? E.g. if lazyShow :: [Int] -> String shows thunks as ? and calculated values normally, in GHCi we would see > let nats = [0..] > lazyShow nats 0 : ? > nats !! 5 5 > lazyShow nats 0 : 1 : 2 : 3 : 4 : ? 回答1: Clearly, lazyShow cannot have the type you state. If the string should depend on the current state of evaluation, then IO String as a result is the best you can hope for. If all

When exactly is the head of a Stream evaluated?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 03:54:10
问题 Normally if you create a Stream object, the head will be eagerly evaluated: scala> Stream( {println("evaluating 1"); 1} , 2, 3) evaluating 1 res63: scala.collection.immutable.Stream[Int] = Stream(1, ?) If we create a Stream to which we prepend in the same statement, it seems slightly surprising that the head is not evaluated prior to the concatenation. i.e. scala> 0 #:: Stream( {println("evaluating 1"); 1} , 2, 3) res65: scala.collection.immutable.Stream[Int] = Stream(0, ?) ( #:: is right