9 ms·
C++14: Transducers
- spooningtamarin 11y agoBenchmark showing that the composition is exactly the same in a for loop as unrolled functions would be nice.
- mavam 11y agoThe proposed Ranges TS has some nice benchmarks: https://ericniebler.github.io/std/wg21/D4128.html https://ericniebler.github.io/std/wg21/D4128.html. See Appendix 1 and 4. (It's not exactly what you're asking for, but showcases that such claims often hold true in practice.)
- rwmj 11y agoTL;DR: Higher order function features that have been available in functional languages since the 1970s will soon be available in C++, with more verbose syntax, explicit type decls everywhere, and (probably) enormous error messages when you get things wrong. The "new" feature will interact badly with other language features like exceptions or RAII and in any case you won't be allowed to use them in the subset of C++ that your company permits you to use.
- agumonkey 11y ago#define c++25 sml
- vvanders 11y agoI think the relevant bit is that we're seeing this without GC which makes it applicable to a whole set of problems that weren't previously relevant(which is also why I think Rust is so interesting). Yeah, we get it that functional languages came first, but rather than looking down on C++ why not celebrate that functional constructs are making it into places where they haven't been before.
- rwmj 11y agoWhat are the problems where GC can't be used? I'm going to guess very tiny embedded systems and hard real time. What are the places where C++ is being used? Unfortunately a lot more than those, resulting in expensive, unreliable, insecure software.
- marshray 11y ago'hard real time' -> multimedia and action games
- unwind 11y agoNot sure what you mean by that arrow, but if you're implying those things on the right are examples of hard real-time, I disagree. In hard real-time systems, there's generally the idea that if scheduling fails, things will break and/or property (or, worse, people) will be damaged. Think of things like industrial high-torque motor control and avionics.
- mpweiher 11y agoAlso, hard real-time systems often don't have to be particularly fast. They are just not allowed to ever be late.
- AnimalMuppet 11y agoThis. "The right answer, too late, is wrong."
- mpweiher 11y agoAnd it doesn't matter if you got the right answer 100 times faster than needed 99 times if you are late once.
- marshray 11y agoPhysical property damage isn't a requirement of the definition of 'hard real time', it's just that common examples are often given that way. If deadlines are missed in multimedia applications the product isn't meeting its stated and implied requirements. The consequences may be different, but a television that drops perceptible video frames is just as 'defective' (from an engineering point of view) as an industrial control system which drops motor control signals.
- paxcoder 11y agoWhat's worse is that we're using "modern" programming languages for which these things aren't even on the horizon. It's so far from the mainstream norm to implement these ancient time-savers that you can't help being excited about Swift, even though such a language should be mundane by now.
- Kristine1975 11y ago>The "new" feature will interact badly with other language features like exceptions or RAII How so?
- mempko 11y agoSure, but it's faster! You won't need a LISP machine for this baby to fly!
- CyberDildonics 11y agoYou forgot amazingly fast.
- gue5t 11y agoAs far as I can discern, transducers are exactly the same as iterator adaptors in Rust: you compose transformations on a stream and get back a computation that pumps one element through the entire set of transformations on each iteration, i.e. it fuses the loops together. Then you can either run this computation and collect results into a data structure of your choosing or compose different stream transformations onto the end. The C++ implementation of this notion looks like an inscrutable, complete trainwreck, like the rest of the language.
- spooningtamarin 11y agoAnd fortunately for us who cannot comprehend it C++17 brings in ranges.
- kybernetikos 11y agoTransducers do not require that every element that comes out of the source results in a single element going into the destination, it can just as well be 0 or many, they also work well with mutable or immutable destinations (because they're a reduce rather than a map so they can modify the destination).
- gue5t 11y agoPerhaps my description was unclear or assumed too much familiarity with Rust, but iterator adaptors such as .filter() and .flat_map support the 0-/many-output use cases you describe, and Vec::extend vs .collect() are a couple of mutable and immutable (resp.) ways to choose the destination of a composition of iterator adaptors.
- kybernetikos 11y agoYou're right, and that description doesn't make sense. Let me see if I can explain why I said what I said: When you call next on an iterator, you expect it to give you the next value or to tell you that there will be no more values. It's not supposed to give you no value 'for now' or many values. With a transducer it can change your destination value in any way it likes according to your final reducer function. Perhaps it's better to say that iterators are controlled by their destination and so are used in a pull fashion while how reduction functions are used is defined by the source so can be used in push mode. It's easy to emulate pull mode with a push source - you just keep pumping values into one end until one comes out the other, but you can't emulate push with a pull destination, as you can't tell without running your adapters how many values will need to go into the pipe to produce your next value. Imagine you have an event source, and you want values coming out of it to go into some other data structure as they arrive via the same kind of chain of transformations as you might use on a simple data structure. It'd be pretty ugly to do this with iterator adapters (probably have to have an iterator stream of futures, but then you can't do a normal map/filter, etc.), but the reduce on the event stream can pump values through as they come in, regardless of if any of them ever make it to the end of the pipe.
- mavam 11y agoWe've been seeing similar attempts recently, all attempting to fill a long overdue gap: the composability of algorithms in the standard library. At this point, there exists a TS [1] for ranges [2] along with an implementation [3]. It will be a thin layer on top of the iterator-based standard library, sometimes dubbed "STL2." On the one hand, it's great to new attempts to compose algorithms mushrooming, but on the other hand it would be great to see more aligned efforts to collaboratively push forward the TS and its implementation. [1] A technical specification (TS) can be thought of as a self-contained mini standard that vendors can implement, but do not have to in order to be "standard-compliant". A is a promising candidate for future inclusion into the standard, however. [2] https://ericniebler.github.io/std/wg21/D4128.html https://ericniebler.github.io/std/wg21/D4128.html [3] https://github.com/ericniebler/range-v3 https://github.com/ericniebler/range-v3
- deleted 11y ago[deleted]
- AndrewGaspar 11y agoI tried implementing transducers in MSVC about a year ago. I got pretty far in implement a lot of the basic set of transducers one would use in development. However, I ultimately abandoned it because they weren't really a great fit with C++. You could make it all work, but the meta template code is extremely difficult to reason about, and MSVC specifically had a lot of issues processing the code (pretty quickly bumped up against MSVC's type name size restriction!). There are also a lot of issues to think about regarding lifetime semantics - figuring out the right behavior was pretty difficult, and debugging issues with it pretty frustrating, especially when the code was optimized. The pros were that, at least for my limited testing, performance was essentially equivalent, for optimized code, between the transducer implementation and the equivalent hand rolled code (e.g., doing a map+filter transducer vs. a foreach with an if), and that using transducers offered further optimizations if you applied them completely up and down the stack, by allowing you to, say, early terminate a sequence that would otherwise have been done in entirety behind some opaque function call. I don't really consider code readability to be a benefit here since transducers are, somewhat inherently, a heady subject. You can see my work here: https://github.com/AndrewGaspar/transducers.hpp https://github.com/AndrewGaspar/transducers.hpp I haven't done a formal write-up or anything on it, but check out the tests if you want to see what consuming them would kind of look like. EDIT: One of the things I'm proud of was rigging it, without need of dynamic typing, such that a transducer could produce heterogeneous types. Obviously your sink would need to be capable of accepting all these different types being produced by the transducible process, but it allowed for some interesting integration with <iostream> - say, creating a transducer that inserts comma's between integers for output (see https://github.com/AndrewGaspar/transducers.hpp/blob/master/include/transducers/interjecting.hpp https://github.com/AndrewGaspar/transducers.hpp/blob/master/... and https://github.com/AndrewGaspar/transducers.hpp/blob/master/unit_tests/interjecting.cpp https://github.com/AndrewGaspar/transducers.hpp/blob/master/...). EDIT 2: I notice that early termination was not shown in this blog post. I implemented this by essentially introducing a known wrapping type (https://github.com/AndrewGaspar/transducers.hpp/blob/master/include/transducers/escape_hatch.hpp https://github.com/AndrewGaspar/transducers.hpp/blob/master/..., sorry I know it's a bad name). The trick with this was I only wanted the returned values of a transducible process to be wrapped by this struct (since it has the overhead of one bool value, and a check to actually determine if it could early terminate) if a single transducer in the chain can early terminate. It also made each individual transducer slightly more complicated to implement, unfortunately.
- rburhum 11y agoI have been doing C++ for almost 2 decades and at a particular stage I was able to say that I knew the language with a self-proclaimed score of "8 out of 10". Nowadays, I would give myself a "4.5". The crazy thing is that, to me, some new features of the language feel like they are being pushed because [x] language has it more than the actual need to solve a particular problem that was to difficult to solve using only the constructs available previously. Two nights ago I was at a C++ Meetup and this guy was explaining how to solve this problem using a crazy-complicated pattern with a new proposed C++ feature. Eventually I had to stop him and ask him why we could not simply do this with a priority heap, a threadpool and some semaphores (simple shit). "Oh yeah, you could do that, too, but this way is cooler".
- meshko 11y agoThe same thing is happening all over the industry. Things are growing organically in all kinds of directions, new technologies appearing at a rate and order of magnitude higher that anything that was happening 10 years ago. It is making it much harder to say current and the net benefit (so far) appears to be negative (I still can't believe that node.js made this terrible untyped language take over the world). I am hoping something good comes out of this, but it is certainly not the nice and cozy industry we knew and loved 10-15-20 years ago. Oh well, it too will pass.
- OmarIsmail 11y agoPractical javascript can now be typed very easily (TypeScript or Facebook's flow). Any "serious" JS shop is going to be using typed-JS in the next 12 months.
- meshko 11y agoThe problem is right there in your response. Do I do TypeScript or Facebook's flow (whatever that is, I don't know)? Or perhaps CoffeeScript? Or wait, that one is not typed. Or is it? Lots of people like it though! Except the ones that perfer EcmaScript 2015. Because it is the future. At least for the next 2 weeks when 2016 starts. But EcmaScript 2015 is not typed either. So much for your promise of all serious JS shops doing typed JS in next 12 months. Also, yeah, great, "serious" shops will be typed. What about "not serious" shops? They don't count? And their users who have to suffer through crappy products built using incomplete tools don't count? Oh, and you know what all these half a dozen most popular and legion of less popular solutions to the problem of "JavaScript sucks" have in common? They add yet another step into the toolchain making the developer's life so much harder. It is nobody's fault that we are were we are, but please, let's call it as it is and not pretend that this pathetic situation is somehow progress or even acceptable.
- jokoon 11y agoTo be honest, if I'd make a language, I would make something like C++, break backward compatibility with C, but just much simpler and more beginner friendly. No templates, but standard maps, sets, arrays. Both compiled and interpreted. No virtual functions. Clean, simple syntax close to C, but with all the nicer stuff C++ brought along, like lambdas. C++ is industrial-strong, but sometimes it seems it's not taught because it's too expert, in favor of java or C# while C/C++ is the language that should be taught. I don't understand languages like rust and go which introduce a non trivial new syntax. Why not keep the C flavor ? I don't see how people are going to adopt languages like that. There should be a statically compiled language which has the same philosophy of python.
- pcwalton 11y ago> I don't understand languages like rust and go which introduce a non trivial new syntax. Why not keep the C flavor ? I don't see how people are going to adopt languages like that. There should be a statically compiled language which has the same philosophy of python. I don't understand what having "the same philosophy of Python" has to do with not keeping "the C flavor". The delta between Python's syntax and C's syntax is basically equal to the delta between Go's syntax and C's syntax, or Rust's syntax and C's syntax.
- jokoon 11y agoI was talking about 2 different things. What is good about C is its simplicity. Python is easy to learn too, and it has nice language facilities and syntax sugar. About the deltas, maybe you're right, but I disagree about the difficulty of the languages. For example go function prototypes seem pretty heavy to fully grasp and understand. Rust also have expert programming facilities and paradigms.
- sqeaky 11y agoWhat would you have for generic programming instead of templates? How would std::vector be implemented?
- 11y ago
- jheriko 11y agothis is revolting code. period. it does nothing special or novel whatsoever - except maybe to set a quality bar for writing unmaintainable code. the very first example given is simple, but demonstrates a thorough lack of understanding for what constitutes readable code. if i had to code review this from a junior i wouldn't let it go in without significant changes, and i'd make time to teach them some of the most basic concepts about producing maintainable and readable code... but only because its easier than firing them. i am skeptical that the label 'transducer' has any solid value whatsoever here. i can understand the concept... its basically to encapsulate stages of algorithms neatly. the fact that it manifests one way in Clojure says nothing about the value of using exactly the same mechanism in C++ to achieve the same... normal classes, normal functions, and not being rubbish at writing code combine to give a much better result for the same class of problems.
- xedarius 11y agoI think the best part of 'modern c++' is the fact that you don't have to use any of it (I'll make a concession for move constructors, that just makes sense).