Laziness in Python - Computerphile

Laziness is a virtue - well, in programming anyway! Professor Thorsten Altenkirch on how you can use the 'yield' to compute certain things "on demand"
To Infinity & Beyond: • Infinite Data Structur...
Python Sudoku Solver: • Python Sudoku Solver -...
/ computerphile
/ computer_phile
This video was filmed and edited by Sean Riley.
Computer Science at the University of Nottingham: bit.ly/nottscomputer
Computerphile is a sister project to Brady Haran's Numberphile. More at www.bradyharan.com

Пікірлер: 639

  • @not_proton
    @not_proton3 жыл бұрын

    This guy looks like every computer scientist to ever exist, in the same body

  • @NuclearSlayer52

    @NuclearSlayer52

    3 жыл бұрын

    the Avatar

  • @JackVermicelli

    @JackVermicelli

    3 жыл бұрын

    I immediately thought of Brent Spiner's character in Independence Day.

  • @erinphilippe5993

    @erinphilippe5993

    3 жыл бұрын

    @@JackVermicelli same!

  • @noobian3314

    @noobian3314

    3 жыл бұрын

    🤣🤣🤣🤣🤣🤣🤣🤣🤣

  • @isbestlizard

    @isbestlizard

    2 жыл бұрын

    Yesssss and talks like Dr Strangelove

  • @EnigmacTheFirst
    @EnigmacTheFirst3 жыл бұрын

    4:10 “3+4 is... nothing. Oh, it’s 7. Okay, that’s good” I don’t know why this had me laughing so hard

  • @dudeabideth4428

    @dudeabideth4428

    3 жыл бұрын

    That made me laugh too. Like a smart professor struggling with a gui interface

  • @ahmedfenti9462

    @ahmedfenti9462

    3 жыл бұрын

    Bacon u making lthings boring

  • @Robstafarian

    @Robstafarian

    3 жыл бұрын

    My face hurts from laughing at that.

  • @hjp360

    @hjp360

    3 жыл бұрын

    same with me.

  • @Twisted_Code

    @Twisted_Code

    3 жыл бұрын

    and then he messed up trying to call nats, not originally expecting it to return and iterator "that was not very clever". They got a kick out of me, sort of reminding me I'm not the only one that occasionally forgets these things. Thanks for leaving that one in @Computerphile

  • @WigglyWings
    @WigglyWings3 жыл бұрын

    I don't know about coding but I am kind of an expert in Laziness.

  • @worthlessguy7477

    @worthlessguy7477

    3 жыл бұрын

    brambedkar59 and you’re not alone

  • @deus_ex_machina_

    @deus_ex_machina_

    3 жыл бұрын

    Indeed. Not to brag, but I'm one of the foremost experts in the field.

  • @Twisted_Code

    @Twisted_Code

    3 жыл бұрын

    if IFeelLikeIt: yield work

  • @nobodyofconsequence6522

    @nobodyofconsequence6522

    3 жыл бұрын

    To be fair a lot of coding is about laziness, so you probably have an aptitude of it.

  • @Twisted_Code

    @Twisted_Code

    3 жыл бұрын

    @@nobodyofconsequence6522 this is... surprisingly true. Especially when you get to the point of trying to make your program more efficient. You're really just trying to get it to do as much with as little time as possible, and sometimes that may be through laziness of not handling something that's out of the scope of the original program design. Basically saying "not my problem; I'm just going to do what I was told to do, nothing more". And, surprisingly, limiting the scope like this actually makes future programming more modular. In other words, it works.

  • @ThorstenAltenkirch
    @ThorstenAltenkirch3 жыл бұрын

    Ok since I am at it here is another comment: no the sieve of Erasthostenes is not an efficient method to compute primes, I just used it as a way to illustrate computing with infinite datastructures. It is known since a while that Primes is in RP, that is randomised polynomial time. Some years ago it was even shown to be in P, i.e. you don't even need a dice.

  • @galvanart3623

    @galvanart3623

    3 жыл бұрын

    Professor, could you then kindly suggest a more efficient method of code, to find primes?

  • @jens6398

    @jens6398

    3 жыл бұрын

    The sieve is efficient. But you implemented trial division.

  • @ThorstenAltenkirch

    @ThorstenAltenkirch

    3 жыл бұрын

    @@jens6398 I don't know what you mean by "trial division" but I have implemented the sieve.

  • @jens6398

    @jens6398

    3 жыл бұрын

    @@ThorstenAltenkirch No. There is *never* any divisibility test in the sieve. Instead there is elimination of multiples, as you actually explained in the beginning of the video. To quote Wikipedia: Primes can also be produced by iteratively sieving out the composites through divisibility testing by sequential primes, one prime at a time. It is not the sieve of Eratosthenes but is often confused with it, even though the sieve of Eratosthenes directly generates the composites instead of testing for them. Trial division has worse theoretical complexity than that of the sieve of Eratosthenes in generating ranges of primes.

  • @arirahikkala

    @arirahikkala

    3 жыл бұрын

    I hope KZread won't completely mangle my code here. I grabbed a convenient library that provides a `primes` in Haskell and set out to calculate, on the one hand, the count of times that a number is crossed out when finding all the primes less than some given n using the sieve of Eratosthenes: > sieved n = sum . map (\p -> length . takeWhile ( length . takeWhile (\p -> c `mod` p /= 0) . takeWhile (

  • @Erik_The_Viking
    @Erik_The_Viking3 жыл бұрын

    Professor Altenkirch is hysterical. I wish I had him during my CS classes.

  • @AWES0MEDEFENDER

    @AWES0MEDEFENDER

    3 жыл бұрын

    Hilarity?

  • @SirFancyPants21

    @SirFancyPants21

    3 жыл бұрын

    Serdar Kocak hysterical is often used as “even more than hilarious”

  • @Zoronoa01

    @Zoronoa01

    3 жыл бұрын

    @sirati97 he means hilarious

  • @chrisbreidenstein3831

    @chrisbreidenstein3831

    3 жыл бұрын

    Lol take it easy

  • @Zoronoa01

    @Zoronoa01

    3 жыл бұрын

    @@chrisbreidenstein3831 he meant hilarious

  • @justingolden21
    @justingolden213 жыл бұрын

    TLDR: "yield" in python is used in generators to loop through something and store the data lazily so when you call the function next it doesn't have to start at the beginning but rather picks up where it left off (often in a loop)

  • @phillip5187

    @phillip5187

    2 жыл бұрын

    Ty!

  • @Jeacom
    @Jeacom3 жыл бұрын

    me: nooo, you can't define a recursive function without an exit condition!! Altenkirch: haha, stack goes brrrrr.

  • @MubashirullahD

    @MubashirullahD

    3 жыл бұрын

    haha. We could have done for i in range(n): yield i

  • @ThomasBomb45

    @ThomasBomb45

    3 жыл бұрын

    @@MubashirullahD That's basically a circular definition. Range() needs to be defined as well

  • @MubashirullahD

    @MubashirullahD

    3 жыл бұрын

    @@ThomasBomb45 I don't know what you mean by circular? :) I'm assuming everyone knows range is lazy in python 3.

  • @ThomasBomb45

    @ThomasBomb45

    3 жыл бұрын

    @@MubashirullahD because his nats() function's output is exactly the same as range() except it is infinite. Rather than use range internally, he explicitly calls yield to demonstrate how it works

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    @@ThomasBomb45 # Range is not needed i=0 while True: i+=1 yield i

  • @tylersanders6854
    @tylersanders68543 жыл бұрын

    @1:30 he can say the word "really" without moving a single muscle.

  • @luismanuelricogutierrez6863

    @luismanuelricogutierrez6863

    2 жыл бұрын

    This video is all about laziness, I guess

  • @psymontung4527

    @psymontung4527

    2 жыл бұрын

    Laziness lol

  • @VivekPayasi

    @VivekPayasi

    2 жыл бұрын

    This cracked me up :D

  • @alexstabell3064
    @alexstabell30642 жыл бұрын

    This is honestly some of the most beautiful code I've ever seen. I've made a many sieve, but never a lazy sieve. I can't believe you can do this incredible calculation with only a few lines of code and an understanding of coroutines.

  • @chaosphoenixhex
    @chaosphoenixhex2 жыл бұрын

    Dude, I feel so relaxed just looking at this guy. It’s a true inspiration, thank you. I need a beer now.

  • @SirKickassington
    @SirKickassington3 жыл бұрын

    Agreed. He is the most likable professor ever. And in keeping with the important lesson on laziness, I’m typing this lying down.

  • @luciojb
    @luciojb3 жыл бұрын

    - "WHEHRE IS THIS THING???" - "ok ok yeah yeah imma do it" Best lines ever

  • @sharkinahat
    @sharkinahat3 жыл бұрын

    I guess I'll be That Guy today: "Now is better than never. Although never is often better than *right* now."

  • @aragonnetje

    @aragonnetje

    3 жыл бұрын

    The zen of python is never a bad thing to reference

  • @cheaterman49

    @cheaterman49

    3 жыл бұрын

    So Zen! :-)

  • @Twisted_Code

    @Twisted_Code

    3 жыл бұрын

    >>> import _this_

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    'now' == 'right now'. 'Right' is just a stress with no real meaning. You sentence is basically 'Now is better than never. Never is better than now.' This can be reduced to False.

  • @Twisted_Code

    @Twisted_Code

    3 жыл бұрын

    @@y.z.6517 I think you might've been staring at your IDE too long

  • @tobyclark6312
    @tobyclark63123 жыл бұрын

    I hadn’t considered Nottingham as a potential university before but just looked at it now and will definitely be considering it as a first/second choice!

  • @jayachandra677
    @jayachandra6773 жыл бұрын

    No you cannot multiply strings python: print("haha string goes b" + "r"*10) >haha string goes brrrrrrrrrr

  • @SteelSkin667

    @SteelSkin667

    3 жыл бұрын

    I'm stealing that

  • @Excgo

    @Excgo

    3 жыл бұрын

    I see, you have been on r/programmerhumor today.

  • @hamiltonianpathondodecahed5236

    @hamiltonianpathondodecahed5236

    3 жыл бұрын

    alternatively print('ha' * 2 + 'string go b' + 'r' * 10)

  • @theRealChadWarden42069

    @theRealChadWarden42069

    3 жыл бұрын

    reddit

  • @righo96

    @righo96

    3 жыл бұрын

    this is str*int, not str*str

  • @bingloveskoki
    @bingloveskoki3 жыл бұрын

    Just wanted to say that I really enjoyed your short paper about the contribution of Martin Hofmann. Thanks!

  • @TotalImmort7l
    @TotalImmort7l3 жыл бұрын

    Laziness in python is writing codes without getting annoyed by ';' and understanding the beauty of a < x < y < b.

  • @omri9325

    @omri9325

    3 жыл бұрын

    Lex Fridman

  • @TotalImmort7l

    @TotalImmort7l

    3 жыл бұрын

    @@omri9325 yep, he described this feature of python as the most beautiful thing a programming language can ever have.

  • @mibdev

    @mibdev

    3 жыл бұрын

    I dislike python but love a It's genius.

  • @ZipplyZane

    @ZipplyZane

    3 жыл бұрын

    I know I could look it up, but I'd love for you to tell me what that does. Does it just test all those conditions, i.e. returning true only if a is less than x, x is less than y, and y is less than b?

  • @TotalImmort7l

    @TotalImmort7l

    3 жыл бұрын

    @@ZipplyZane See it in this way: Given expression: a 1. First, the interpreter evaluates (a Let's say, the result of "a Then the current expression will be True As in #1, the interpreter evaluates (True Don't think it in terms of stacks and ASTs. It is better to see this as a mathematical expression.

  • @JacobCanote
    @JacobCanote3 жыл бұрын

    This is a perfect example of real life coding.

  • @saranobutt
    @saranobutt3 жыл бұрын

    I don't know why this is recommended to me, but I'll watch it because I do love to learn new things and this is definitely a subject that I have no knowledge on.

  • @eliudnjai

    @eliudnjai

    3 жыл бұрын

    It's a sign Sara. Join the computer science field

  • @Phroggster

    @Phroggster

    3 жыл бұрын

    m_CompSciStudents++;

  • @andarielbaal3405

    @andarielbaal3405

    3 жыл бұрын

    Starting with this kind of video will only scare you)))

  • @burlacuvalentin4481

    @burlacuvalentin4481

    3 жыл бұрын

    It is a great dream that my phone at leats recommends me smth like this. I suggest you watch the lambda calculus videos they are real fun!

  • @AbdulIsik

    @AbdulIsik

    3 жыл бұрын

    Programming is actually very easy and fun, just start putting your free hours in it and you'll become a lazy programmer in no time!

  • @TheRuralpoet
    @TheRuralpoet2 жыл бұрын

    Enjoyed this... He had a great sense of humour... Intellect with humility is the greatest... Thanks

  • @nekkowe
    @nekkowe3 жыл бұрын

    I'm always happy to see a new episode with Prof. Altenkirch :>

  • @GifCoDigital
    @GifCoDigital3 жыл бұрын

    I now finally see the big advantage of using generators!!! Great video!!

  • @ZeedijkMike
    @ZeedijkMike3 жыл бұрын

    The content is great and his warm dry humor makes me giggle.

  • @Saver006
    @Saver0063 жыл бұрын

    Trying to show your homies a cool programming trick be like: ''...so now when I press this button... oh it doesn't work...yyym'''

  • @dooza
    @dooza3 жыл бұрын

    I'VE WATCHED SO MANY VIDEOS FROM COMPUTERPHILE AND NEVER SEEN THE VOICE BEHIND THE CAMERA. FINALLY.

  • @trueriver1950

    @trueriver1950

    3 жыл бұрын

    This is because with social distancing via Zoom (or similar) there is already a camera on the interviewer. Pre-Covid it would be non-lazy to add a second camera pointed at the camera operator. Or to put it another way, in a video conference everyone is a voice behind a camera, including the professor

  • @ConnorKennedy16
    @ConnorKennedy163 жыл бұрын

    I think it's between Altenkirch and Brailsford as my favorite overall Computerphile presenters in terms of raw depth-of-content. Pound is a close third.

  • @code-dredd
    @code-dredd3 жыл бұрын

    "I wanted to talk about laziness, but I was to lazy to do talk about it" Missed opportunity 🙃

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    You are not lazy, or you wouldn't have commented.

  • @MegaArti2000
    @MegaArti20003 жыл бұрын

    This yield statement puzzles me so hard man

  • @raingram

    @raingram

    3 жыл бұрын

    It's like "return" but it starts from that line the next time you run the function. It just "pauses" at that point, waiting for you to call it again.

  • @KbIPbIL0

    @KbIPbIL0

    3 жыл бұрын

    yield is multiple return yield makes your function "G" a generator, which yields a sequence. when you call next(g) it continues running until the next yield statement and the function next() returns the thing you yielded.

  • @christophebeaulieu4916

    @christophebeaulieu4916

    3 жыл бұрын

    @@raingram I've literally never seen it that way. You're amazing. Thanks for your comment. I get it now

  • @hanvyj2

    @hanvyj2

    3 жыл бұрын

    @@raingram great explanation

  • @jphanson

    @jphanson

    2 жыл бұрын

    @@christophebeaulieu4916 ~. i’m

  • @xario2007
    @xario20072 жыл бұрын

    Never knew about yield - yet I use Haskell and Python. Very helpful!

  • @gedtoon6451
    @gedtoon64512 жыл бұрын

    This is a good explanation about yield. With regard to Sudoku, all good Sudoku puzzles only have one solution.

  • @TheJaguar1983
    @TheJaguar19833 жыл бұрын

    As soon as I see the title, I figured it would be about generators. Very useful things.

  • @ahmadalkhansa5294
    @ahmadalkhansa52943 жыл бұрын

    For a moment I thought I didn't understand seive but suddenly profesor corrects it.😅 It's beautiful to show that it's ok to make mistakes

  • @RichardHamnett
    @RichardHamnett3 жыл бұрын

    This method is paramount for feeding data / data augmentations into machine learning model training

  • @anindyasundarmanna6683
    @anindyasundarmanna66832 жыл бұрын

    This video is absolute gold!

  • @MecchaKakkoi
    @MecchaKakkoi3 жыл бұрын

    The shirts are always top shelf!!!!

  • @rz2374
    @rz23743 жыл бұрын

    I know i am 39 minutes late but I have never been this early before. Seeing only 62 comments is so weird. And this is nicely timed as I was just researching about yield statements.

  • @darcipeeps
    @darcipeeps3 жыл бұрын

    Just learned about this yesterday in my programming languages course

  • @TerjeMathisen
    @TerjeMathisen3 жыл бұрын

    The Sieve code is interesting, but I really worry about the efficiency! There are only two ways to generate the next prime here, and that is to either remember all previous primes, and iterate until you find one which is not a multiple of any of them (but only up to sqrt(n)), or you have to use standard sieve thinking and strike out at least a bunch of multiples each time you find a new prime. The second approach has much more useful Big-O properties. The Sudoku solver otoh will work with constant memory, it should be more or less equal to a standard recursive solver with backtracking?

  • @raqha4575
    @raqha45753 жыл бұрын

    i love his coffee mug

  • @RUFU58
    @RUFU583 жыл бұрын

    When you’re not programming you’re guarding the train in The Matrix. Top man!

  • @AzraelCC
    @AzraelCC3 жыл бұрын

    I don't understand why Computerphile videos would have any dislikes. That said: NICE.

  • @sarwagyajain4841
    @sarwagyajain48413 жыл бұрын

    With recursion in generators does the call stack keep growing (especially in the case in the video) or does it optimize and keep only the relevant variable?

  • @shawon265
    @shawon2653 жыл бұрын

    This is probably the shortest pythoniest-python program I have ever seen!

  • @clayz1
    @clayz13 жыл бұрын

    Write a prime factoring routine and apply it looking for remainders of n (the number currently being tested).

  • @beskamir5977
    @beskamir59773 жыл бұрын

    "Laziness is a virtue - well, in programming anyway!" No need to specify that it's in programming. Without laziness we wouldn't have had a reason to domesticate animals, build societies, develop science and technologies, create machines, etc. Basically we'd still be in the stone age if our species wasn't lazy and constantly looking to make life easier for ourselves.

  • @beskamir5977

    @beskamir5977

    3 жыл бұрын

    @Joseph Malone I disagree. By using an animal to plow your field (and later a machine) we decrease the work we need to do. Ideally we wouldn't be doing that job at all but at least until we get decent enough ai, not doing that job would mean we wouldn't get to eat. Therefore laziness is expressed as "optimization" rather than as completely not doing the required task because the task is essential for our survival. In the case of tasks that are less essential for our survival such as copying books, the laziness factor is more subtle as the motivation there is more about saving costs than not doing the original job. Although saving costs can be considered as part of laziness since it allows those involved to get the same thing for less effort. Essentially minimizing effort is inherently linked with and usually driven by laziness.

  • @cryonim

    @cryonim

    3 жыл бұрын

    @Joseph Malone As a character's voiceline in R6 goes, "Efficiency is clever laziness".

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    @@beskamir5977 Explain why people play games? By playing games, we basically seek *more* work that are unnecessary.

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    @@beskamir5977 Our brain tells from fun and not fun works. Hunting is inherently fun to our brain (or our ancesters would have died from laziness). When our ancesters started ploughing, they avoided the pain of starvation, but doing so also deprived them of the fun of hunting. Most modern works are boring, because our brains were not evolved to do that. BTW, many women enjoy shopping, because shopping is basically paid gathering.

  • @beskamir5977

    @beskamir5977

    3 жыл бұрын

    @@y.z.6517 I think you basically answered your own question. Laziness happens when the thing we need to do is boring. When it's fun laziness doesn't apply as much.

  • @rooneye
    @rooneye2 жыл бұрын

    You can tell this guy knows what he's talking about here. He looks like an EXPERT in the art of lazy.

  • @justjoshingaround
    @justjoshingaround3 жыл бұрын

    Minor suggestion. I think he should have tried to show what happens at the end of his yield sudoku solver. Using "return" with "yield" has interesting behavior with a "StopIteration" exception.

  • @Ekevoo
    @Ekevoo3 жыл бұрын

    This mug. I love this mug. I want this mug.

  • @CryTaxel
    @CryTaxel3 жыл бұрын

    Doesn't this create a callstack that gets bigger and bigger with every iteration (like all non-tail-recursions) or am I missing something?

  • @noobinvestor3180

    @noobinvestor3180

    3 жыл бұрын

    Aalex not quite actually..generators or iterators in general holds the address to the particular value it is now referencing and moves to the “next” address when next() is called

  • @chris8443

    @chris8443

    3 жыл бұрын

    Yes. Also, python doesn't support tail recursion. Edit: as pointed out, of course python supports tail recursion. :-) What I meant is that it doesn't support _tail call elimination / tail call optimization_.

  • @Schnorzel1337

    @Schnorzel1337

    3 жыл бұрын

    Tried it with the exact code and for default settings python 3.7 it gave an "RecursionError: maximum recursion depth exceeded" after the prime 733.

  • @sadhlife

    @sadhlife

    3 жыл бұрын

    @@noobinvestor3180 it will, the yield from is the issue here, it's basically recursion

  • @sadhlife

    @sadhlife

    3 жыл бұрын

    @@chris8443 not yet :)) with the new PEG parser, we might get something nice

  • @kkloikok
    @kkloikok3 жыл бұрын

    "oh it's ... That's bad" Story of my life.

  • @SuperBartles

    @SuperBartles

    3 жыл бұрын

    "This is what being a programmer is actually like" tutorial

  • @tobiastriesch3736
    @tobiastriesch37363 жыл бұрын

    Props for rocking the unicorn mug 🤩

  • @zeroone8548
    @zeroone85483 жыл бұрын

    this is amazing

  • @superwinner5010
    @superwinner50103 жыл бұрын

    Hah, a few weeks ago I got interested in prime numbers and after some time I got it to be able to rival C in speed, best I got it was all primes under 10^11 in 93.6s which is blazing fast, but I could have still made it even faster, but it gets very complex fast.

  • @helloworld9018
    @helloworld90183 жыл бұрын

    Speaking about sudoki solver with yield, will next solution always be unique? Or at some point it can return a duplicate of previously found solution? How can one control this process?

  • @flyingsquirrel3271
    @flyingsquirrel32713 жыл бұрын

    If you want a lazy natural number generator that doesn't blow the stack after a few iterations, do it manually: class Nats: def __init__(self, n): self.n = n - 1 def __next__(self): self.n += 1 return self.n def __iter__(self): return self Now you can use "Nats(2)" just like he used "nats(2)" in the video. Of course the sieve generator still reaches maximum recursion depth pretty quickly, but it gets way further with this Nats implementation. Edit: I just noticed that "blowing the stack" isn't quite right here. Since it's python, it's all safe and heap allocated anyway, so it just hits the recursion limit.

  • @corynivens6510

    @corynivens6510

    3 жыл бұрын

    You don’t even need a class here: def nats(n): while True: yield n n+=1 Or better yet def nats(s): yield from range(s, sys.maxsize) And lastly: s = range(2, sys.maxsize)

  • @flyingsquirrel3271

    @flyingsquirrel3271

    3 жыл бұрын

    @@corynivens6510 Haha you're right. The while True solution is much cleaner. I don't like the sys.maxsize thing though. I know its usually gonna be big enough, but theoretically python ints can grow infinitely big (until your ram is full), so that implementation is technically not equivalent to the other ones.

  • @flyingsquirrel3271

    @flyingsquirrel3271

    3 жыл бұрын

    Lol I just looked through the itertools module and of course this iterator exists in the std lib! So here's the definite solution: from itertools import count (then use "count" instead of "nats")

  • @corynivens6510

    @corynivens6510

    3 жыл бұрын

    Anything higher than sys.maxsize will result in an overflow. The only caveat with s = range is that range doesn’t support next(), so you’d technically need s=iter(range...)

  • @corynivens6510

    @corynivens6510

    3 жыл бұрын

    Ah man, the itertools solution really is the one isn’t it? itertools or nothing haha

  • @ElizaberthUndEugen
    @ElizaberthUndEugen3 жыл бұрын

    If you use the `filter` predicate, it gets even more obvious that the sieve function precisely mimics the idea of layers of sieves (aka filters!). def sieve(gen): i = next(gen) yield i yield from sieve(filter(lambda k: not is_multiple(k, i), gen)) This evaluates to filter(lambda k: not is_multiple(N, i), ... filter(lambda k: not is_multiple(5, i), filter(lambda k: not is_multiple(3, i), filter(lambda k: not is_multiple(2, i), gen))) ...) where N is the nth prime.

  • @pedrovasconcelos8260
    @pedrovasconcelos82603 жыл бұрын

    There is a fundamental difference between iterators and laziness: lazy evaluation memoizes results of subcomputations whereas iterators do not. For example, the Haskell version of the infinite list of Fibbonacci numbers can be defined as fibs = 0:1:zipWith (+) fibs (tail fibs) If you translate this definition to iterators you'd end up with something like the following: def fibs(): yield 0 yield 1 yield from (i+j for i,j in zip(fibs(),tail(fibs()))) def tail(it): # couldn't find this in the standard library... next(it) # drop the first element while True: n = next(it) yield n However, the Python code above takes exponential time whereas the Haskell one takes linear time (ignoring the log factor for the increasing sizes of numbers). To get linear time using iterators you have to manually code the memoization (using a loop for simplicity): def fast_fibs(): a,b = 0,1 while True: yield a a,b = b,a+b

  • @pedrovasconcelos8260

    @pedrovasconcelos8260

    3 жыл бұрын

    @@mrdkaaa Lazy evaluation a.k.a. call by need requires memoization of results, otherwise it is simply call by name. For reference check the chapter on graph reduction i. SPJ's "The Implementation of functional languages" (it's out of print by freely available from his web page).

  • @haroldostenger5160
    @haroldostenger51602 жыл бұрын

    hi, the cribe is a nice approach, but it fails with recursion error although I expected it not to fail because of the yield from. I'll try to tweak it and fix it.

  • @davidwallis4281
    @davidwallis42813 жыл бұрын

    nats() seems very 'functional', but it contains state, and the function returns a different value with the same (no) arguments which seems the opposite to what functional often tries to do. Still learning...

  • @lennyschwarz6683
    @lennyschwarz66837 ай бұрын

    9:12 The "Three!" is perfect

  • @timseguine2
    @timseguine23 жыл бұрын

    I could be wrong, but I am pretty sure this will blow the stack if you do this in python

  • @yokilewis4894
    @yokilewis48943 жыл бұрын

    8:19 This kind of naming always gets me. In Haskell, when doing `filter`, do you filter out the elements that make the predicate true, or do you keep them? (It takes a while to get used to it!) Similarly, do you sieve out the elements, or are you actually keeping them?

  • @darcipeeps

    @darcipeeps

    3 жыл бұрын

    Same thing as other search filters

  • @y.z.6517

    @y.z.6517

    3 жыл бұрын

    Always run a 2-line test code first to figure stuff like that out. This is even more important, if you dip in and out of several languages and libraries.

  • @2rfg949
    @2rfg9493 жыл бұрын

    haha I thought you wrote that wrong!! but I was deferring to your professorial nature! I love you videos

  • @2rfg949

    @2rfg949

    3 жыл бұрын

    the coolest thing i got from this is modularity. the rest is fascinating, i did not know yield, and recursion does my head in a bit, but i can use it. but modularity is such a level up for me in my coding,. thank you for that my friend!!!

  • @ShaunakDe
    @ShaunakDe3 жыл бұрын

    if we kept this running, would the program only halt when the intermidiate variable became too large? Or would it halt before that due to python overheads ?

  • @le09das
    @le09das3 жыл бұрын

    Wish I had this guy as a teacher.

  • @stephanfitzpatrick1769
    @stephanfitzpatrick17693 жыл бұрын

    The nats generator shows is mathematically more elegant, but it will not render an infinite sequence since python will create a call stack and you'll eventually receive a RecursionError. itertools.count would avoid that or a while loop

  • @BenjaminGoldberg1
    @BenjaminGoldberg13 жыл бұрын

    Is python's "yield" similar to raku's gather/take?

  • @cn-ml
    @cn-ml3 жыл бұрын

    Is "yield from y" just syntactic sugar for "foreach (x in y) yield x"?

  • @JamesTJoseph
    @JamesTJoseph3 жыл бұрын

    It's a wonderful idea to handle large files.

  • @atifachaudhry
    @atifachaudhry3 жыл бұрын

    The following generator based version of sieve() doesn't have stack overflow and is more efficient. Plus, IMHO, it explains what't going on in the algorithm more clearly; but is a bit longer. def sieve(n=2): prev, skip = [], False while True: for p in prev: if n%p == 0: skip=True; break; if not skip: yield n prev.append(n) n, skip = n+1, False

  • @justahker3988

    @justahker3988

    3 жыл бұрын

    You could also add in the fact that any composite number n must have a factor 2

  • @dread69420
    @dread694203 жыл бұрын

    8:04 - 8:15 programming in a nutshell

  • @siliconmessiah1745
    @siliconmessiah17453 жыл бұрын

    I don't know if it for this video specifically, but professor seems to bring about an air of laziness through his mannerisms as well😂

  • @Twisted_Code
    @Twisted_Code3 жыл бұрын

    Why doesn't nats crash when it hits about 1000 or so? Seems like it would hit the stack limit, unless I'm misunderstanding how the control flow works (or unless I mis-implemented it) Edit: nevermind, I must have mis-implemented it. I kept trying various explanations (such as tail recursion) but eventually just tried it again about a week later.

  • @TheDhammaHub
    @TheDhammaHub3 жыл бұрын

    Laziness as in "waiting to evaluate an expression as late as possible"? Or laziness as in getting nothing done? xD

  • @SteelSkin667

    @SteelSkin667

    3 жыл бұрын

    The latter is much easier to implement!

  • @__.__-_.

    @__.__-_.

    3 жыл бұрын

    pull request let me do nothing all day

  • @Argon270

    @Argon270

    3 жыл бұрын

    IKR! There are 2 types of laziness in programming.

  • @StreuB1
    @StreuB13 жыл бұрын

    4:26 'nutz' 4:40 'yeet' Got it Prof. Thorsten is friggin awesome.

  • @baudneo
    @baudneo2 жыл бұрын

    That coffee cup is glorious.

  • @yahyakhaddam
    @yahyakhaddam3 жыл бұрын

    I loved your shirt

  • @pskry
    @pskry3 жыл бұрын

    Did anyone check stackoverflow for a python question titled "Why doesn't my sieve function work?"

  • @TrippSaaS
    @TrippSaaS2 жыл бұрын

    If computer scientists were Pokemon, this guy is the final evolution.

  • @NJudah3
    @NJudah33 жыл бұрын

    so cool!

  • @rtj6874
    @rtj68743 жыл бұрын

    Interesting! Not sure if this counts as lazy but I too once read about the sieve and wrote a python script to tell me if any input number was a prime or not and it simply returns true or false. """ I got interested in primes one day after reading about "Sieve of Eratosthenes" as a quick way of determining if any number is a prime. """ def IsPrime(prime): if prime

  • @kartoffelmozart
    @kartoffelmozart3 жыл бұрын

    But this way of nesting generators inside of generators will quite quickly give a RecursionError will it not? Because you make another nested generator for every prime you find

  • @mastershooter64
    @mastershooter643 жыл бұрын

    laziness is me writing a for loop to make a list of alphabets or numbers instead of actually typing it out myself. in a few years I'm gonna start making a neural network that can write half of the code for me lol

  • @pafnutiytheartist
    @pafnutiytheartist3 жыл бұрын

    The problem with this approach is that it reaches recursion depth pretty fast. I was able to sieve primes using a slightly improved version of this method to about 3500. After that yield from is just unviable

  • @guyindisguise

    @guyindisguise

    3 жыл бұрын

    It's possible with mutable state instead of recursion e.g. def nats(n): while True: yield n n += 1 edit: fixed typo

  • @CecilWesterhof

    @CecilWesterhof

    3 жыл бұрын

    That was the first thing I thought. It can (on my system) go to 130, but with 131 I get a RecursionError. Do you mind sharing your code?

  • @pafnutiytheartist

    @pafnutiytheartist

    3 жыл бұрын

    @@CecilWesterhof I did the same thing as in the comment above for generating natural numbers and the rest is exactly like described in the video. I'm not sure how to use mutable state for the sieve itself.

  • @CecilWesterhof

    @CecilWesterhof

    3 жыл бұрын

    @@pafnutiytheartist That is strange. With that change it goes from 130 to 497. Not nearly the 3500 you talk about.

  • @netslayeruk
    @netslayeruk3 жыл бұрын

    Having a hard time understanding this. For the seive, he is typing next and he's running the function... For 2 then 3 then 4. Is he doing this just to reach the solution for 4? What's the advantage over running the original function with 4 as a parameter?

  • @thinboxdictator6720

    @thinboxdictator6720

    3 жыл бұрын

    it suppose to show just primes if you start with 4,it won't remove multiples of 2 and 3 : >>> from sieve import sieve , nats >>> p = sieve(nats(4)) >>> next(p) 4 >>> next(p) 5 >>> next(p) 6 >>> next(p) 7 >>> next(p) 9 >>>

  • @nicolozorzetto3871

    @nicolozorzetto3871

    3 жыл бұрын

    NetSlayerUK in that case no advantage. In the second example (prime numbers) the syntax is a lot simpler and human readable instead of nested loops

  • @netslayeruk

    @netslayeruk

    3 жыл бұрын

    @@thinboxdictator6720 With the original function you could put in X and it would sieve from integers '2 to X' and return the sieved contents of prime numbers up to X. I'm just stumped on how this is different.

  • @patrickmutuku
    @patrickmutuku3 жыл бұрын

    Interesting, I'll try use generators more now.

  • @kristofszobacsi5123
    @kristofszobacsi51233 жыл бұрын

    3:32 Great Band!!

  • @emilejetzer7657
    @emilejetzer76573 жыл бұрын

    I see a lot of comments on how this implementation of the sieve is poor; that’s because it’s a simple example of a technique that can be useful. It’s not meant to be used exactly as shown, but it can do the job anyway. The sudoku solver shows a better use case.

  • @helloworld9018

    @helloworld9018

    3 жыл бұрын

    Agreed. Some people just don't get the meaning of the video. Professor didn't say he is going to do 100% accurate implementation. He wanted to show what you can do with yield and how to be 'lazy'.

  • @BlitzPSH
    @BlitzPSH3 жыл бұрын

    11:09 I don't think you need global here. Global is only needed if you're changing a reference variable in the outer scope, which you're not doing when simply mutating a list.

  • @TechyBen
    @TechyBen3 жыл бұрын

    (Looks at my cat sleeping) Nature is "efficient", just turns out efficiency is comfortable sometimes. :)

  • @hodsinay6969
    @hodsinay69693 жыл бұрын

    when calculating primes the way you said in python it will do it layzily, but it will probably not have enough space in a matter of seconds, generators are not something that you just save an infinate amount of without repercussions, neither are levels of recursion. I suggest to get the primes lazily with something more like: def get_primes(): primes = set() i = 2 while True: if all(i % p != 0 for p in primes): yield i primes.add(i) i += 1 this code does basiclly the same as the code in the video (even though there are probably better ways to calculate prime numbers) I know that it shows the power of generators less, but it probably is a bad idea to use a generator that uses a generator that uses a generator...

  • @johnbennett1465

    @johnbennett1465

    3 жыл бұрын

    As promised, I came over here to check it out. This is much better than his. It would be nice to not save all the primes you already found, but I can't think of anyway to avoid it.

  • @hodsinay6969

    @hodsinay6969

    3 жыл бұрын

    @@johnbennett1465 there are a lot of ways to improve this algorithm of getting primes. for instance not checking all the primes but only the ones that are lower than the number sqrt (and saving the primes in a list for that), or just checking all the numbers up to the number I am checking sqrt's, in the long run it is probably better. I just wanted to implement the same algorithm as he did without using unneeded recursion.

  • @slash_me

    @slash_me

    3 жыл бұрын

    @@hodsinay6969 you could make it a little more pythonic like this: import itertools def get_primes(): primes = set() for i in itertools.count(2): if all(i % p != 0 for p in primes): yield i primes.add(i) That also brings back the usage of generators: itertools.count is a generator that counts to infinity!

  • @hodsinay6969

    @hodsinay6969

    3 жыл бұрын

    @@slash_me that's a great suggestion, I didn't know this generator before...

  • @johnbennett1465

    @johnbennett1465

    3 жыл бұрын

    @@hodsinay6969 your suggestions do improve the speed. I was more worried about running out of memory. Your suggestions don't help with that. Admittedly you have to create a lot of primes before it is a problem. 🙂 I just don't like programs that keep eating up memory and try to avoid creating them when I can. Having to port my spell checker to a microprocessor with only 256 bytes (yes bytes) of RAM memory may have scared me. 😅 Note that the code and compressed word list was in ROM.

  • @PaulaBean
    @PaulaBean3 жыл бұрын

    PEP8: no spaces before colons, and put spaces around your operators.

  • @KbIPbIL0

    @KbIPbIL0

    3 жыл бұрын

    colons are not operators, no?

  • @nerminskenderovic
    @nerminskenderovic2 жыл бұрын

    Now how do you write this simple sieve generator function in JavaScript?

  • @femioyekan8184
    @femioyekan81843 жыл бұрын

    Same thing in C# and I'm sure other languages.

  • @the3nder1
    @the3nder13 жыл бұрын

    I need that mug.

  • @elorz007
    @elorz0073 жыл бұрын

    Another way of understanding "yield from" is thinking that it "returns" a function but you didn't call it yet. (Useful concept to also understand function pointers / blocks / closures / lambdas). Kind of.

  • @masebor
    @masebor3 жыл бұрын

    i only listened to the first 40seconds and i approve.

  • @topsiterings
    @topsiterings3 жыл бұрын

    Nice!

  • @christinebraun9610
    @christinebraun96103 жыл бұрын

    0:15 is me anytime I get an email from my university admin

  • @dalirkosimov4623

    @dalirkosimov4623

    3 жыл бұрын

    🤣🤣😩

  • @liluziskrrt4116
    @liluziskrrt41163 жыл бұрын

    Best language ever

  • @alexrobertson35

    @alexrobertson35

    3 жыл бұрын

    oh no you're gonna start a war (btw c++ best (: )

  • @pipony8939

    @pipony8939

    3 жыл бұрын

    @@alexrobertson35 c++ segfault incoming

  • @alexrobertson35

    @alexrobertson35

    3 жыл бұрын

    @@pipony8939 I was using smart pointers!

  • @pipony8939

    @pipony8939

    3 жыл бұрын

    @@alexrobertson35 smart? why not genius pointers instead?

  • @alexrobertson35

    @alexrobertson35

    3 жыл бұрын

    @@pipony8939 tbh most of the time I don't even use smart one