Laziness in Python - Computerphile

Computerphile · 370K views · 2020 · 14:21

Open on YouTube

Python, Ruby & scripting languagesFunctional programmingAlgorithms & data structuresexplainer

Why Stack Overflow recommends it

Stack Overflow users link it because its tiny recursive prime-sieve generator is the source of the code they ask about, port to other languages, or try to understand.

Recommended by 3 people across 1 different months, 2020 to 2021.

What it is

A Computerphile episode in which Professor Thorsten Altenkirch explains lazy evaluation in Python using generators, 'yield' and 'yield from'. He builds an infinite stream of natural numbers and a recursive Sieve of Eratosthenes that produces primes on demand.

What people said

Recommends eol · 2020-08 · Stack Overflow · on “filter values from a javascript generator-stream”

This Laziness in Python - Computerphile [this video] video presents this program to produce primes with generators: def nats(n): yield n yield from nats(n+1) def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i%n!=0) p = sieve(nats(2)) next(p) # 2 next(p) # 3 next(p) # 5 next(p) # 7 next(p) # 11 I decided to implement this sieve-algorithm using JavaScript generators. I'm pretty confident with JavaScript in general but have never worked with generators directly before. Here's what I have so far: function* nextNaturalNumber(n) { yield n; yield* nextNaturalNumber(n + 1); } function* sieve(n) { let count = 0; let g = nextNaturalNumber(2); while (count++ < n) { const possiblePrime = g.

Recommends Ravexina · 2020-08 · Stack Overflow · on “How it's possible to loop into an infinite generator to remove some items?”

I recently watched a video [this video] describing laziness in Python with an example which finds prime numbers. We got a function which yields a number from an infinite stream of numbers: def nums(n): yield n yield from nums(n+1) Nothing fancy here. The part I don't understand is i for i in s if i%n != 0 in sieve function which is in charge of finding the actual prime numbers. def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i%n != 0) Seems it's creating a new generator passing it to sieve function, keeping only the numbers that are not multiples of n . My question is that, isn't s an infinite stream? How it's possible to loop into all of it to remove necessary numbers creating

Recommends solid.py · 2020-08 · Stack Overflow · on “Using yield from to refactor a factorial implementation, yielded unexpected results”

@superbrain No it's not, using `yield from`, as a form of tail recursion is a valid use case, as you can see in this video as well [this video]

Cites Pol Dellaiera · 2020-08 · Stack Overflow · on “CallbackFilterIterator and Prime numbers generation issue”

I'm trying to use the PHP \CallbackFilterIterator [LINK] working in a script that prints Prime numbers. Basically what I'm trying to achieve is a very basic Sieve of Eratosthenes [LINK] , see this nice video from Computerphile [this video] . Here's the simplified version of the code <?php /** * Run this code with: php -n to make sure no configuration will be used * so xdebug will not be used either. */ declare(strict_types=1); function llist($n = 2) { yield $n; return yield from llist($n + 1); } $callback = fn(int $p) => fn(int $a, int $b, Iterator $it): bool => $a % $p !== 0; $i = 0; $iterator = llist(); while (true) { $prime = $iterator->current(); $iterator->next(); var_dump($prime); if ($i++

Cites Esau · 2020-10 · Stack Overflow · on “Generate Prime Numbers via Eratosthene's Sieve C#”

Inspired By: Laziness in Python - Computerphile [this video] Which I translated to C#: using System; using System.Collections.Generic; using System.Linq; namespace GenertatorTest { class Program { /// <summary> /// Natural Numbers Genarator /// </summary> /// <param name="first">first number in the sequence</param> /// <returns>the sequence: first, first + 1, first + 2, ... ∞</returns> static IEnumerable<int> natural(int begin) { yield return begin; foreach (var item in natural(begin + 1)) { yield return item; } } /// <summary> /// Primary Numbers Genarator /// </summary> /// <param name="nums">natural numbers sequence which we want to apply Eratosthene's Sieve on</param> /// <returns>infinite pr

Cites Linear Algebra fans · 2020-11 · Stack Overflow · on “Python example from Computerphile about yield and yield from”

Topic : Laziness in Python - Computerphile URL : [this video] From this example it tried to generate prime numbers from a Infinity series of number from 2 to Infinity in Python with yield and yield from feature Code: def nats(n): yield n yield from nats(n+1) def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i%n!=0) p = sieve(nats(2)) next(p) next(p) In function nats it generate Infinity number start from n and yield n every time when next() was called series = nats(1) next(series) # output 1 next(series) # output 2 .... next(series) # output n Question In function sieve it calls generator object and yield n which come from s ;s is nats(2) in this example The next line is the most

Cites Ibrahim Sherif · 2021-08 · Stack Overflow · on “Prime number generators using Sieve of Eratosthenes”

This python code generates prime numbers using Sieve's technique. I am having trouble understanding the code in particular the yield from parts. How to trace such a code or make it simpler to understand. This code is from the video [this video] from Computerphile Youtube channel about laziness in python. def nats(n): yield n yield from nats(n+1) def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i%n != 0) p = sieve(nats(2)) next(p) # generates prime numbers 2, 3, 5, 7, 11, ........

Cites lstbl · 2021-08 · Stack Overflow · on “lazy computing of primes with python without recursion error”

I came across this great video from computerphile [this video] which talked about lazy computing. In the video, they lay out this really beautiful bit of python code that will calculate primes using the sieve method "lazily" def nats(n): yield n yield from nats(n+1) def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i%n!=0) The issue comes when you actually implement the code to calculate a large number of primes. Because you are recursively searching, the recursion depth is reached relatively quickly. I can only calculate a prime up to 2281 If I modify the code to get rid of recursion with the first function: def nats(n): while True: yield n n += 1 Then I can get to prime 12379 b

Cites notepadNinja · 2021-08 · Stack Overflow · on “Python generator expression recursion”

This is a question about Python internals. The following code is taken from this video about laziness in python [this video] : def nats(n): yield n yield from nats(n + 1) def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i % n != 0) s = sieve(nats(2)) print(next(s), next(s), next(s), next(s), next(s)) # Primes: 2, 3, 5, 7, 11... The sieve function generates prime numbers lazily (read this [LINK] for the original concept). Conceptually, we add filters to the "sieve", so every number (say, 10) is tested against all the previously found prime numbers (so 2, 3, 5 and 7) until the next prime is found, i.e. 11. 11 is then added to the "list" of filters, and so on. This part (i for i in