Live data from Hacker News

Streams – Lazy evaluation in C++14

jscheiny.github.io

11–20 of 30 posts

Re: Streams – Lazy evaluation in C++14

#11
post #5

@wwwwwwwwww you have been hell-banned and I dont see anything particularly offensive in your immediate comment history. > is there any reason to add lazy evaluation to c++ [...] I think you are suffering from a misapprehension. Nothing is being added to C++ the language. This is just an implementation of lazy streams in C++14. Think of it as an optional 3rd party library. > lazy evaluation is one of those features wh…

Second example say you want to compute (x + y) * z - w where each of them are long vectors but want to keep the look of the code readable and explicit. Naive way to do it will create lots of temporaries and unnecessary loops. Every binary operation is going to create a temporary (and entail a loop). But if you have laziness you just need one loop. Can you elaborate on this? Why can laziness provide better code here?

1. loop: x + y, loop (x + y) * z, loop (x + y) * z - w.

2. loop: (x[i,...,n] + y[i,...,n]) * z[i,...,n] - w[i,...,n]

although I don't think the loops are going to be the main overhead in this case.

Re: Streams – Lazy evaluation in C++14

#12
post #5

@wwwwwwwwww you have been hell-banned and I dont see anything particularly offensive in your immediate comment history. > is there any reason to add lazy evaluation to c++ [...] I think you are suffering from a misapprehension. Nothing is being added to C++ the language. This is just an implementation of lazy streams in C++14. Think of it as an optional 3rd party library. > lazy evaluation is one of those features wh…

Second example say you want to compute (x + y) * z - w where each of them are long vectors but want to keep the look of the code readable and explicit. Naive way to do it will create lots of temporaries and unnecessary loops. Every binary operation is going to create a temporary (and entail a loop). But if you have laziness you just need one loop. Can you elaborate on this? Why can laziness provide better code here?

If each of the four variables is a vector, naively you might do (x + y) first, producing a new vector. Then you would multiply by z, producing a second new vector. Finally, subtract w, producing a third new vector. You have now iterated over the length of the vectors three times, and allocated three new vectors (two of which are no longer needed).

A better way to do all that would be to allocate a single result vector and populate it with the full computed expression for each element. This can be much faster for large vectors. Python's numexpr (among others) is designed to do just this.

Re: Streams – Lazy evaluation in C++14

#14
post #2

Ah, python's itertools available in c++, at last.

There's a previous implementation of itertools in C++: https://github.com/ryanhaining/cppitertools .

Thanks for the reminder. I had come across cppitertools before and wanted to explore it and then totally forgot. Not sure why you got downvoted here, have tried to compensate.

BTW your handle rung a bell. I was wracking my brain to remember where have I heard of it before, then finally, oh yes MacSTL. It really deserves more love. Have not used it, but sure browsed the code.

Re: Streams – Lazy evaluation in C++14

#15
post #5

@wwwwwwwwww you have been hell-banned and I dont see anything particularly offensive in your immediate comment history. > is there any reason to add lazy evaluation to c++ [...] I think you are suffering from a misapprehension. Nothing is being added to C++ the language. This is just an implementation of lazy streams in C++14. Think of it as an optional 3rd party library. > lazy evaluation is one of those features wh…

> Without this features it is pretty much impossible to approach Fortran like speeds and expressivness in array operations in C++. Fortran is strict, though, is it not?

Fortran has some built-in array operations that are tricky to implement in C. I think srean is saying that lazyness would help do that.

Re: Streams – Lazy evaluation in C++14

#16

honest question: is there any reason to add lazy evaluation to c++ other than to just add more features to the language? lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

>lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

Good question.

Let's say you have a program like this little python example:

    inputs = [raw_read() for i in range(1000)] # Read numbers from command line
    numbers = map(int, inputs) # Turn them into integers
    for num in numbers: # Check for 42
      if num == 42:
        return true
    return false # It's not there
If you use lazy evaluation, each item in the "map" object will only be calculated as needed. So if the first item in the "inputs" list is "42", it will only do a single string-to-integer conversion.

If you use greedy evaluation, it will map all strings to integers at once, which might be wasteful.

There are other benefits as well; this is the most obvious.

Re: Streams – Lazy evaluation in C++14

#17
post #5

@wwwwwwwwww you have been hell-banned and I dont see anything particularly offensive in your immediate comment history. > is there any reason to add lazy evaluation to c++ [...] I think you are suffering from a misapprehension. Nothing is being added to C++ the language. This is just an implementation of lazy streams in C++14. Think of it as an optional 3rd party library. > lazy evaluation is one of those features wh…

How would this lazy-vector-mapping compare to the optimizations used in std::valarray? Or are those optimizations just the ones you mention here?

Re: Streams – Lazy evaluation in C++14

#18
post #16

honest question: is there any reason to add lazy evaluation to c++ other than to just add more features to the language? lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

>lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides Good question. Let's say you have a program like this little python example: inputs = [raw_read() for i in range(1000)] # Read numbers from command line numbers = map(int, inputs) # Turn them into integers for num in numbers: # Check for 42 if num == 42: return true return false # It's not there If you use lazy…

But in a language with mutating side effects, and iterators, I'm not sure what this buys you over:

    for (auto& num = file.line_iterator(); num != file.end(); num++) {
        if (atoi(num) == 42)
            return true;
    return false;
Note that 'line_iterator()' isn't something that (as far as I know) is defined in the C++ standard library, but there's nothing preventing it.

Re: Streams – Lazy evaluation in C++14

#19

honest question: is there any reason to add lazy evaluation to c++ other than to just add more features to the language? lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

Edward Kmett's answer to this[1] question on Quora does a good job of explaining the value of lazy evaluation.

[1] http://www.quora.com/Programming-Languages/When-did-you-real...

Re: Streams – Lazy evaluation in C++14

#20
post #18
post #16

Earlier quoted context omitted.

>lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides Good question. Let's say you have a program like this little python example: inputs = [raw_read() for i in range(1000)] # Read numbers from command line numbers = map(int, inputs) # Turn them into integers for num in numbers: # Check for 42 if num == 42: return true return false # It's not there If you use lazy…

But in a language with mutating side effects, and iterators, I'm not sure what this buys you over: for (auto& num = file.line_iterator(); num != file.end(); num++) { if (atoi(num) == 42) return true; return false; Note that 'line_iterator()' isn't something that (as far as I know) is defined in the C++ standard library, but there's nothing preventing it.

> But in a language with mutating side effects, and iterators, I'm not sure what this buys you...

Side-effect free, functional style of programming.

Post reply on HN