4 ms·
It's either an exaggerated academic demonstration or a joke, but it's convincingly presented in a serious tone. Nobody genuinely thinks it's a good idea to repl
by ryandrake 13y ago
It's either an exaggerated academic demonstration or a joke, but it's convincingly presented in a serious tone. Nobody genuinely thinks it's a good idea to replace a 9 line C function with 156 lines of templated C++ code (not counting the header full of templates classes it includes).
https://github.com/BartoszMilewski/Okasaki/blob/master/Triples/Triple.cpp https://github.com/BartoszMilewski/Okasaki/blob/master/Tripl...
- spc476 13y agoHow about if it's framed differently: he created coroutines in 156 lines of C++.
- zvrba 13y agoWell, almost. A coroutine is created once and it yields control to its "caller" each time it produces an item. To implement a coroutine, you only need to save its stack (local variables, instruction pointer). He created a lazy stream: instead of yielding control for each item produced, it makes a linked list of items where each node is created on demand. Thus, coroutines only return the "next" item, while streams compute and store the history of the whole computation.
- adamnemecek 13y agoNot to nitpick but you are talking about generators which IIRC are also incorrectly called coroutines in the Python community. A proper coroutines are not limited to yielding control to the caller, but anywhere.
- zvrba 13y agoI've described what's usually known as asymmetric coroutines or semicoroutines. "Asymmetric" because [as claimed by Lua manual at least] there are two separate calls for control transfer (yield and resume). Generators are less powerful. See http://www.lua.org/pil/9.1.html http://www.lua.org/pil/9.1.html
- GreaterFool 13y agoWell, are you suggesting that a real C++ developer wouldn't think it is a good idea to include a library or framework in his program? (because 156 lines of extra code is too much?) Have you ever used STL? It is full of template classes! It is a LOT of code in there. The author implemented a stream processing framework based on lazy evaluation and an interface taken straight out of functional programming (Haskell) approach. Tidy it up, make it more robust and why wouldn't it be industry strength and ready for production code? #include <lazy_streams> //3 lines of code to implement the original problem What would be wrong with that? I think C++ more than (m)any other language(s) sufferes from not-invented-here syndrome. If something is not in core language or STL then it might as well not exist -- people will always do their own thing from scratch. Or you grab some behemoth library like Boost which should cater to all your needs. In contrast in Haskell/OCaml/Clojure (probably others too but those I'm most familiar with) one can download a tiny library that allows to radically change the control flow of the program. What's demonstrated here is one such thing. It is based on sound principles (monads, functors...) and lazy streams which is a very well researched topic. Sure, this is C++ we are talking about and there sure will be dragons somewhere. But if Bartosz ever dedicates his time to make a full fledged and tested library I will be damn sure to use it!
- jejones3141 13y agoI would have to disagree. C++ is the opposite of NIH; it's a kitchen sink programming language.
- qdog 13y agoI....do not grok article. I don't use function pointers and jump tables all that much, but I'm pretty sure you could add a function pointer argument to printNTriples and replace the printf() with it and get somewhere if you really needed to get there. Starting to feel like I might be getting to the "Get off my damn lawn, kid!" part of my life.
- zvrba 13y ago> I'm pretty sure you could add a function pointer argument to printNTriples and replace the printf() You could, but the control is still driven by printNTriples. What if you had to break early, before generating all N triples? In this simple case, you could use a return value from the callback to decide whether to continue or not. In a more general case, you would you would encapsulate the state into a class (for example, the x,y,z,i locals from the original function would become instance memebers.)
- evincarofautumn 13y agoIt’s both a demonstration of the expressive power of C++—“In C++, you can say anything!”—and a joke—“…but it takes a very long time to say.” Several of my coworkers experienced with C++ are jumping ship to Haskell for new code, because C++ is becoming simply untenable.
- yati 13y agoI don't know how much experienced you are with modern C++, but the language has in fact evolved to be more usable and high level :)
- evincarofautumn 13y agoMore usable, yes. Usable, no. I’ve been working in C++ for about ten years, since a couple years after I started programming. It’s certainly gotten better over time—particularly in terms of containers and ownership semantics—but it’s still not good exactly. For instance, it’s possible for mortals to write exception-safe code now, but still not memory-safe code. I spend an inordinate of time language-lawyering for my coworkers to avoid future debugging hell, and though in my opinion I don’t even know the standard all that well, I’m certainly among maybe a few dozen people at my company who come close to knowing it in full. It’s just an absurd source of artificial expertise that needs to die.
- 5hoom 13y agoWhat problems have you found trying to write memory safe code? It seems to me that you barely have to think about memory ownership anymore ever since unique_ptr and shared_ptr came along.
- evincarofautumn 13y agoI suppose my main gripes are that moving is not free, you can reuse moved-from objects, and there is no story for checked borrowing of unique_ptrs—you have to use raw pointers or weaken the ownership semantics to shared_ptr so that you can use weak_ptr for safe borrowing. Not to mention that shared_ptr is often semantically questionable and used as an inefficient alternative to a garbage collector.
- vl 13y agoWell, before it was "any complex program contains bug-ridden implementation of Lisp", and now it's "any C++ program invents verbose Haskel". But seriously, while this is toy example, it demonstrates what perhaps will be viable for standard library or for the future language features. Have you tried to read STL implementation code? - it's pretty incomprehensible, but it's still extremely useful library.