lazy-evaluation

Funky haskell lazy list implicit recursion

≡放荡痞女 提交于 2019-12-03 05:48:19
In Haskell, you can build infinite lists due to laziness: Prelude> let g = 4 : g Prelude> g !! 0 4 Prelude> take 10 g [4,4,4,4,4,4,4,4,4,4] Now, what exactly goes on when I try to construct a list like this? Prelude> let f = f !! 10 : f Prelude> f !! 0 Interrupted. Prelude> take 10 f [Interrupted. Prelude> The Interrupted. s are me hitting CTRL+C after waiting a few seconds. It seems to go into an infinite loop, but why is that the case? Explanation for non-Haskellers: The : operator is prepend : Prelude> 4 : [1, 2, 3] [4,1,2,3] This line: Prelude> let g = 4 : g says "let g be the list

Understanding the different behavior of thunks when GHCi let bindings are involved

我的梦境 提交于 2019-12-03 05:26:11
问题 I've been playing with some examples from Simon Marlow's book about parallel and concurrent programming in Haskell and stumbled across an interesting behavior that I don't really understand. This is really about me trying to understand some of the inner workings of GHC. Let's say I do the following in the REPL: λ» let x = 1 + 2 :: Int λ» let z = (x,x) λ» :sprint x x = _ λ» :sprint z z = (_,_) λ» seq x () () λ» :sprint z z = (3,3) Ok, this is pretty much what I expected except that z gets

Disadvantages of Lazy<T>?

允我心安 提交于 2019-12-03 04:53:46
问题 I recently started using Lazy throughout my application, and I was wondering if there are any obvious negative aspects that I need to take into account when using Lazy<T> ? I am trying to utilize Lazy<T> as often as I deem it appropriate, primarily to help reduce the memory footprint of our loaded, but inactive plugins. 回答1: I'll expand a bit on my comment, which reads: I've just started using Lazy, and find that it's often indicative of bad design; or laziness on the part of the programmer.

Python class member lazy initialization

泪湿孤枕 提交于 2019-12-03 04:47:57
I would like to know what is the python way of initializing a class member but only when accessing it, if accessed. I tried the code below and it is working but is there something simpler than that? class MyClass(object): _MY_DATA = None @staticmethod def _retrieve_my_data(): my_data = ... # costly database call return my_data @classmethod def get_my_data(cls): if cls._MY_DATA is None: cls._MY_DATA = MyClass._retrieve_my_data() return cls._MY_DATA You could use a @property on the metaclass instead: class MyMetaClass(type): @property def my_data(cls): if getattr(cls, '_MY_DATA', None) is None:

Lazy evaluation

南笙酒味 提交于 2019-12-03 03:36:14
How can I lazy evaluate second arg in std::conditional? #include "stdafx.h" #include <type_traits> struct Null{}; struct _1{enum {one = true,two = false};}; struct _2{enum {two = true, one = false};}; template<class T> struct is_nulltype { enum {value = false}; }; template<> struct is_nulltype<Null> { enum {value = true}; }; template<class T> struct X : std::conditional<is_nulltype<T>::value,Null,typename std::conditional<T::one,_1,_2>::type>::type { }; int _tmain(int argc, _TCHAR* argv[]) { X<Null> x;//won't compile no Null::one but I don't need that member in Null at all return 0; } The

How to create lazy_evaluated dataframe columns in Pandas

老子叫甜甜 提交于 2019-12-03 03:23:15
A lot of times, I have a big dataframe df to hold the basic data, and need to create many more columns to hold the derivative data calculated by basic data columns. I can do that in Pandas like: df['derivative_col1'] = df['basic_col1'] + df['basic_col2'] df['derivative_col2'] = df['basic_col1'] * df['basic_col2'] .... df['derivative_coln'] = func(list_of_basic_cols) etc. Pandas will calculate and allocate the memory for all derivative columns all at once. What I want now is to have a lazy evaluation mechanism to postpone the calculation and memory allocation of derivative columns to the actual

Speed up Haskell concurrency

谁说我不能喝 提交于 2019-12-03 03:13:45
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_ a (shW n)) -- MVar, TVar, IORef, ... read/write (x' = x + k) shR = readIORef shW !k !r =

How to understand clojure's lazy-seq

半世苍凉 提交于 2019-12-03 03:11:06
I'm trying to understand clojure's lazy-seq operator, and the concept of lazy evaluation in general. I know the basic idea behind the concept: Evaluation of an expression is delayed until the value is needed. In general, this is achievable in two ways: at compile time using macros or special forms; at runtime using lambda functions With lazy evaluation techniques, it is possible to construct infinite data structures that are evaluated as consumed. These infinite sequences utilizes lambdas, closures and recursion. In clojure, these infinite data structures are generated using lazy-seq and cons

Lazy “n choose k” in OCaml

北城余情 提交于 2019-12-03 03:10:52
As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant. For example, choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]] Any ideas? I would like to have the whole thing to be lazy, outputting a lazy list, but if you have a strict solution, that'll be very useful too. Here is a strict and suboptimal

Any difference between Lazy evaluation and Short-circuit evaluation?

自作多情 提交于 2019-12-03 02:56:56
From Wikipedia: Lazy evaluation is: In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed Short-circuit evaluation is: Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression So what's the difference between them for example when I have: if(false && true && true)