functional programming haskell Programming Python

Some Lazy Fun with Streams

Update: fellow algorithms researcher Francisco Claude simply posted an incredible article about utilizing lazy analysis to unravel Tic Tac Toe video games in Widespread Lisp. Niki (my brother) additionally wrote a publish utilizing turbines with asynchronous prefetching to hide IO latency. Value a read I say!

I’ve lately been obsessing over this programming concept referred to as streams (also called infinite lists or turbines), which I discovered about from the Construction and Interpretation of Pc Packages guide. It’s sort of like an iterator that creates its personal knowledge as you go alongside, and it will possibly result in efficiency increases and splendidly readable code if you utilise them with greater order features reminiscent of map and scale back (which many things could be rewritten in). It additionally permits you to categorical infinitely giant knowledge buildings.

When common lists are processed with larger order features, that you must compute the whole record at each stage; when you’ve got 100 parts, and you map a perform to them, then filter them, then partially scale back them, you could be doing up to 300 operations, however what should you solely need to take the primary 5 parts of the end result? That might be a waste, therefore streams are typically a more sensible choice.

Although SICP particulars the way to do it in Scheme, in this weblog submit I will show some languages that have it inbuilt – Haskell and Python – and easy methods to implement streams your self in case you ever end up in a language without it1.


Haskell is a lazy language. It didn’t earn this status from not doing the dishes whenever you ask it to (though that’s one more reason it’s lazy). What it means in the context of formal languages is that analysis is postponed until completely needed (Here is a cute (illustrated) weblog publish describing this lazy evaluation stuff). Take this code for example:

Prelude> let x = [1..10]

At this stage you is perhaps tempted to say that x is the record of numbers from 1 to 10. Truly it only represents a promise that whenever you want that listing, x is your guy. The above code that creates an inventory from 1 to 10 nonetheless hasn’t been executed till I finally ask it to be (by referring to x):

Prelude> x

It’s type of like telling your mum you’ll do the dishes, but ready till she shouts your identify out again earlier than you set down your DS. Truly, it’s sliiiiightly totally different – if I as an alternative wrote:

Prelude> let x = [1..10] Prelude> let y = x ++ [11..20]

I have referred to x again once I declared y, but x nonetheless hasn’t evaluated. Solely after I shout y’s identify will y shout x’s identify and provides me again my entire record:

Prelude> y

Right here you ask your robotic to scrub half the dishes, however he’s too busy enjoying DS too (stupid robot). Lastly when your mum shouts, you shout on the robot, and he does his set of dishes, and also you do yours. But what’s the profit right here? It isn’t that I can get more DS time in…

Take for example an inventory of constructive integers from 1. Yes, all of them. In different languages it is perhaps onerous to precise this, but in Haskell it is as simple as [1..]. This means we now have an inventory of infinite integers, but we’ll solely calculate as far as we’d like:

Prelude> let z = [1..] Prelude> head z
Prelude> take 5 z

The syntax right here is amazingly terse, and it might make your code more environment friendly. However even if we don’t have the syntax for it in another language, we will present it ourselves very easily.


Python has an identical concept referred to as turbines, which are made using the yield key phrase instead of return, multiple time (or in a loop) in a perform:

def integers_from(N):
yield N
N += 1

>>> z = integers_from(1)
>>> z.subsequent()

Observe: Python turbines are stateful and are hence barely totally different to an infinite record in Haskell. For example returns totally different values in two places, and thus is time delicate – we can’t get z to ‘rewind’ like we might in Haskell, the place z is stateless. Statelessness can lead to easier to know code, among different advantages.

Rolling Our Own

Let’s reinvent this wheel in Python (however in a stateless manner), so if we ever discover ourselves craving infinite lists we will easily roll our own in just about any language with Lambdas.

I’ve chosen Python to implement this, regardless that it already helps infinite lists via turbines, simply because its syntax is more accessible. Certainly, the under can already be completed with Python’s built-in-functions (though with state). It’s in all probability not an excellent concept to do it this manner in Python, as it doesn’t have tail name optimisation (until you employ this hack using decorators and exceptions).

First we’ll take a look at including lazy evaluation, nevertheless the syntax requires it to be specific:

>>> x = lambda: 5
>>> y = lambda: 2 + x()

Here, x shouldn’t be 5, and y isn’t 7, they’re both features that may consider to that once we lastly run them; the expression inside the lambda gained’t be evaluated till we achieve this explicitly:

>>> x()
>>> y()

And that’s pretty much all of the heavy lifting. To make an infinite listing, we principally make a linked record the place we generate each node as we’d like it:

def integers_from(N): return (N, lambda: integers_from(N+1))

def head((H, _)): return H

def tail((_, T)): return T()

And there’s our infinite listing. To entry it use head() and tail() (recursively if vital):

>>> z = integers_from(1)
>>> head(z)
>>> head(tail(z))

Helper Features

First we should always make a approach for us to take a look at our streams:

def to_array(stream):
return scale back(lambda a, x: a + [x], [], stream)

Which is a scale back operation that places every head factor into an array (which is carried alongside as a parameter to scale back()). Here is scale back() (map() could be discovered on this gist):

null_stream = (None, None)
def scale back(f, end result, stream):
if stream is null_stream: return outcome
return scale back(f, f(outcome, head(stream)), tail(stream))

We would have liked some approach to inform if we had reached the top of a stream – not all streams are infinitely long. Meet our next perform, which can assist us terminate a stream:

def take(N, stream):
if N <= 0 or stream is null_stream: return null_stream return (head(stream), lambda: take(N-1, tail(stream)))

This can take the first N parts from the required stream. So now we will inspect the primary N parts:

>>> to_array(take(10, integers_from(1)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

For our upcoming example, we also need a filter() technique, which can filter out parts that meet a offered predicate:

def filter(pred, stream):
if pred(head(stream)):
return (head(stream), lambda: filter(pred, tail(stream)))
return filter(pred, tail(stream))

Now onto our instance 🙂

Textbook Instance

Here is the usual example to show the terseness of streams:

def sieve(stream):
h = head(stream)
return (h, lambda: sieve(
filter(lambda x: xpercenth != 0, tail(stream))))

Here is a perform which recursively filters something which is divisible by any number we now have previously seen in our stream. Math aficionados will notice that that is the Sieve of Eratosthenes algorithm.

>>> primes = sieve(integers_from(2))
>>> to_array(take(10, primes))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Recursively outlined knowledge, and solely as a lot of it as we would like – fairly neat.

And Now For Something Virtually Utterly Totally different

Once I first saw this, I questioned what software there is perhaps to have a stream of features. Here I have defined a stream which recursively applies a perform to itself:

def rec_stream(f):
return (f, lambda: rec_stream(lambda x: f(f(x))))

When may this be useful? It’d yield velocity enhancements for those who commonly need to recursively apply a perform a specific amount of occasions, but have pricey branching (so the situation examine at each degree is sluggish). It may be used as a abstraction for recursive iteration 2, which provides you back the perform ‘already recursed’ so to speak (although lazily).

One such recursive course of I can think of is Newton’s technique for approximating roots, outlined recursively as:

When f(x) = 0.

The more iterations you do the extra accurate the answer becomes. One use of Newton’s technique is to make use of it until you’ve reached a certain error tolerance. One other method, which I discovered about lately when studying concerning the fast inverse sq. root algorithm, which uses just one step of Newton’s technique as an affordable means to enhance it’s (already pretty good) preliminary guess. There’s a really great article right here which explains it very properly.

After studying that, I questioned a few stream that might include features of accelerating accuracy of Newton’s technique.

def newton(f, fdash):
return lambda x: x – f(x)/float(fdash(x))

The newton() perform accepts f(x) and f'(x), and returns a perform that accepts a primary guess.

def newton_solver(iters, f, fdash):
def clear up(v):
n = newton(lambda x: f(x) – v, fdash)
stream = rec_stream(n)
return to_array(take(iters, stream))[-1] return remedy

This one is a little more difficult. With a view to have it clear up for a worth aside from zero, I needed to either define it in f(x), since f(x) should equal zero, however I didn’t need the consumer to should iterate over the stream every time they needed to compute the square root of a special number, say. To allow it to return a perform that solved for sq. roots in the common case, I needed to make the interior perform remedy(), which would bind for the value the caller specifies, hence fixing f(x) = v for x. Hopefully this becomes clearer with an example:

>>> sqrt = newton_solver(1, lambda x: x**2, lambda x: 2*x) # 1 iter
>>> sqrt(64)(four) # Sqrt of 64 with initial guess of 4
>>> sqrt = newton_solver(three, lambda x: x**2, lambda x: 2*x) # 3 iters
>>> sqrt(64)(4)

Now we will move around this sq. root perform and it’ll all the time do 3 iterations of Newton’s technique.

This will not be sensible until compilers can optimise the ensuing perform (or if there is a method to do the reduction myself simply), however it was fun to do 🙂 As all the time feedback and ideas are appreciated. If anyone who reads that is good with compilers, advice can be great 😀

What you are able to do now’s learn SICP for more cool issues like streams and practical programming, or take a look at Sean Anderson’s bit hacks page for more cool hacks like the quick inverse sq. root. Or refactor your code to use map, scale back and streams 🙂