5 ms·
I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense
by bthomas 14y ago
I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense
- johnsonqq 14y agoThis is why you study CS at university before you start work as a programmer. Otherwise you end up being amazed by the basic CS concepts, or worse, you end up reinventing them badly.
- kachnuv_ocasek 14y agoThank God for abstraction.
- kmfrk 14y agoDoes studying CS at university make people as insufferably grumpy and unhelpful as you?
- johnsonqq 14y agoCome on, I've got a reasonable point here. People post on forums asking these questions which should have been answered during their educations. If civil engineers had a forum site and someone asked "how do I calculate the load a simple beam", people would say, what the fuck are you doing in your job if you don't know that? Who is employing you and why? Where did you go to college? Who was your tutor because next time I'm at a dinner at my college I'm going to ask him how the fuck did they pass you. Our current trend of saying college doesn't matter really starts to be a problem when professional programmers (yeah I'm assuming that the guy in the question is) are asking basic questions like this. This shows that we do need college education for programmers. It also irritates me - this guy could have taken the time to learn all these basics, but for whatever reason he thought it didn't matter, and now he's paying for it.
- monkeyfacebag 14y agoNeither a college education nor continued employment in the field is a requirement to ask a stackoverflow question. In fact, I'd venture to say that a version of stackoverflow where the default answer to everything was "you should have learned this in college" would not be very useful (or used) at all. If you find the discussion beneath you, I suggest that you not participate and find a better use of your time. Others may be getting value from it and I can't for the life of me understand why you think this is a bad thing.
- cabalamat 14y agoLoads of people didn't go to college, and program. Loads of people went to college doing something other than CS, and program. Loads of people haven't yet been to college, and intend to go and do CS, and program. Why do you look down on all these people?
- brown9-2 14y agoPython has a lot of users who are not programmers by profession, but rather mathematicians or scientists or other professions where Python is an excellent tool for getting their main job done. So assuming that only well-educated programmers can have questions about a programming language is asinine. My university education didn't cover coroutines either.
- icebraining 14y agoSO is not a site for software engineers, it's a site for programmers, regardless of their level of knowledge, education or age. And by the way, I'd rather work with fellow programmers who don't know what a coroutine is than with an insufferable elitist like your posts make you out to be.
- Leszek 14y agoOne of the false assumptions you're making here is that CS == programming. There's a difference between being a civil engineer, where you have to be able to calculate loads on beams, and being a physicist, where you try to understand why beams behave the way they do.
- deleted 14y ago[deleted]
- jarin 14y agoIt is pretty awesome to be privileged and living in an affluent country, eh?
- josephlord 14y agoIt is one thing suggesting it is unwise to develop a new language without a CS background or broad and deep experience with other languages of some form as you might end up with something like PHP but there are plenty of ideas to develop that a CS background helps fairly little. Generally curiosity and willingness to learn is worth more.
- bthomas 14y agoFWIW I majored in computer science at a major school, though admittedly I was a shitty student. I spent more time building things than going to class.
- Yttrill 14y agoThis does reflect very sadly on schools, and shows up when trying to explain things to programmers used to conventional languages, especially if you're trying to explain why C is fundamentally broken. Roughly there are three fundamental control transfer operations: conditional jumps, subroutine calls, and coroutine calls. A subroutine is a slave, the caller is a master. With coroutines, the caller and callee are peers. Coroutine calling is more fundamental and easier to program, but it isn't available in C. The theory is about continuations. A continuation is just "the rest of the program". You can think of it as the program counter (PC). Subroutine calling works by passing a continuation to a routine, when the routine is finished it invokes the continuation. This is done by pushing the program counter on the stack, and the subroutine popping it with a return statement. Coroutines work by exchange of control. When you call a coroutine you give it your current continuation to call when its ready. But also you do not call the coroutine at the beginning. You call it where it last left off: at its last continuation point. A set of coroutines are usually called fibres. They represent interleaving of control. You can emulate them with pre-emptive threads and locks, but coroutines are synchronous and non-premptive. Felix and Go both make heavy use of coroutines (called fthreads and goroutines). Iterators as in Python are a special case. In general on today's badly designed CPU's you have to think of coroutines as requiring stack swapping. Threads do this which is why you can emulate coroutines with threads. By far the most well known coroutine scheduler is .. the Operating System. Its basically a coroutine of applications. This is why you can read and write to files: the real world is event driven but callbacks are impossible to program with. So the operating control inverts the events by stack swapping so your application can be written as a master. You think you're calling the OS, and the OS thinks its calling you. You're both masters. That's coroutines. Python (and Felix) both have yield, but that's a special case. In general the model is reading and writing channels: yielding is just writing the "sole" channel and getting a function result is just reading it. A channel is basically a "place to swap stacks".
- csense 14y ago> Coroutine calling is more fundamental and easier to program Citation needed. For coroutines, each coroutine needs its own stack. That means you have to have a dynamic memory system baked into the language. And maybe garbage collection too. > on today's badly designed CPU's you have to think of coroutines as requiring stack swapping I can't think of an implementation of yield (let alone general coroutines) that doesn't require a separate stack for each coroutine. I admit that I haven't learned very many of the stranger forgotten architectures that are out there, so I might be blinded by the limitations of a somewhat conventional experience. But I'm also thinking it might even be provable that each coroutine needs its own stack: Think of a program that has m generator functions, where f_1 calls f_2, f_2 calls f_3, ..., f_{m-1} calls f_m. Each of these subroutines creates n copies of its next-level generator, and steps those generators and yields to the parent unpredictably (for example, depending on input from a user-supplied file). It seems like if m and n are large enough, you'll have no choice but to resort to swapping stacks.