lazy-evaluation

How is foldl lazy?

我的梦境 提交于 2019-12-04 00:45:26
There are lots of good questions and answers about foldl , foldr , and foldl' in Haskell. So now I know that: 1) foldl is lazy 2) don't use foldl because it can blow up the stack 3) use foldl' instead because it is strict ( ish ) How foldl is evaluated: 1) a whole bunch of thunks are created 2) after Haskell is done creating thunks, the thunks are reduced 3) overflow the stack if there are too many thunks What I'm confused about: 1) why does reduction have to occur after all thunk-ing? 2) why isn't foldl evaluated just like foldl' ? Is this just an implementation side-effect? 3) from the

How does Log4j implement lazy argument evaluation?

十年热恋 提交于 2019-12-04 00:09:26
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]); 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 String is always calculated even when its not logged. with logger.debug("Entry number: {} is {}", i, entry[i]);

InvalidOperationException in my Lazy<> value factory

倾然丶 夕夏残阳落幕 提交于 2019-12-03 22:03:17
I have a class containing something like the following: public static class Config { private static Lazy<ConfigSource> _cfgSrc = new Lazy<ConfigSource>( () => { /* "ValueFactory" here... */ }, true); public static ConfigSource ConfigSource { get { return _cfgSrc.Value; } } } In accessing the ConfigSource property, I encountered this InvalidOperationException : ValueFactory attempted to access the Value property of this instance. I don't see anything in my "value factory" method that accesses the Value property. Is there anything else that could be triggering this exception? This problem only

Swift - Lazy loading a property that can be made nil later

邮差的信 提交于 2019-12-03 21:37:24
I am looking for a way to lazy load my variable, but I want to be able to make it nil later and then recreate it on the get. For example in the instance that there is a memory warning i want to clear anything that isn't used and then recreate it when needed again later. Here is how I would do it in Objective-C and my current interpretation in swift. I am not sure that it preserves the variable for keeping current navigation. Obj-C Implementation @property (strong, nonatomic, readwrite) UINavigationController *navController; ... - (UINavigationController *)navController { if (!_navController) {

hibernate, to be lazy or not to be lazy? [closed]

混江龙づ霸主 提交于 2019-12-03 16:12:30
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 11 months ago . I have entity A , which has a many-to-many relation to entity B . So the table layout is : A, AB(mapping table), B To get an object of entity A: I call A.getById() which does getHibernateTemplate().get(A.class, id) using spring and hibernate. The problem is, sometimes ensuing code will just need A, sometimes ensuing code will continue to access the associated B

In Haskell, I want to read a file and then write to it. Do I need strictness annotation?

馋奶兔 提交于 2019-12-03 15:35:43
问题 Still quite new to Haskell.. I want to read the contents of a file, do something with it possibly involving IO (using putStrLn for now) and then write new contents to the same file. I came up with: doit :: String -> IO () doit file = do contents <- withFile tagfile ReadMode $ \h -> hGetContents h putStrLn contents withFile tagfile WriteMode $ \h -> hPutStrLn h "new content" However this doesn't work due to laziness. The file contents are not printed. I found this post which explains it well.

Lazy evaluation in R – is assign affected?

安稳与你 提交于 2019-12-03 15:01:34
问题 I read this basic question on renaming objects and @Shane 's answer to it, pointing me to lazy evaluation. Now I wonder if assign is evaluated lazily, too. Just like here: assign("someNewName",someOldObject) rm(someOldObject) The reason why I wonder about this is the following use case: Assume I got 10K+ R objects each of which has two attributes called originalName and additionalName . Now I want to write a function that can efficiently let the user switch from one name to the other without

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

。_饼干妹妹 提交于 2019-12-03 13:24:41
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 { get { if (!loaded) { propertyValue = getter(); loaded = true; } return propertyValue; } } public

@transient lazy val field serialization

 ̄綄美尐妖づ 提交于 2019-12-03 13:17:39
问题 I have a problem on Scala. I serialize an instance of class with @transient lazy val field. And then I deserialize it, the field is assigned null . I expect the lazy evaluation after deserialization. What should I do? Following is a sample code. object Test { def main(args: Array[String]){ //---------------- // ClassA - with @transient //---------------- val objA1 = ClassA("world"); println(objA1); // This works as expected as follows: // "Good morning." // "Hello, world" saveObject("testA

Speed up Haskell concurrency

可紊 提交于 2019-12-03 13:00:07
问题 MVar, TVar, IORef, ... I can't speedup a thunk problem (I think). (My original problem is a threaded code, I do "forkIO" n-times calling "addMany"; but I think my problem is on "shW" function) Let next code: {-# LANGUAGE BangPatterns #-} import Control.Concurrent import Control.Monad import System.Environment(getArgs) import Data.Int import Data.IORef -- "i" times, add "n" for each IORef (in "a") addMany :: [IORef Int64] -> Int64 -> Int64 -> IO () addMany !a !n !i = forM_ [1..i] (\_ -> forM_