4 ms·
Ask HN: C with lambdas?
I was wondering if there was a language which would qualify as C with lambdas. To expand on that notion, I'm specifically looking for a language with manual memory management like plain C, functions as a first class citizen and eventually closures (although I'm not sure how that would fit with the manual memory management).
The reason why I really like C is that it's possible to finely tune data structures and optimize both for spatial and temporal complexity. However, the inability to use more abstract constructs such as lambdas and closures are in some cases a real PITA.
If this hasn't been done, any ideas why ? Is it technically feasible ?
- pdw 15y agoC++ has lambdas.
- friggeri 15y agoI should clarify : I'm not looking for an OO language or something with all the bloat of C++. Just a minimal superset of C with lambdas.
- chubs 15y ago"Just a minimal superset of C with lambdas" You pretty much exactly described Objective-C with that statement. I'm not sure if you intentionally meant that or not?
- friggeri 15y agoI guess I haven't looked into Objective-C that much, basically just stopped after seeing all the NS* addons, I'll definitely take a longer look, thanks.
- abionic 15y agoC++0x has lambdas and that is the nearest language to a C Language admirer..... and just using C++ doesn't implement using OOP practices... C++ doesn't force you to do OOP it just avails you the privilege
- tom_b 15y agoHave a look at: http://stackoverflow.com/questions/208835/function-pointers-closures-and-lambda http://stackoverflow.com/questions/208835/function-pointers-... This may help your thoughts in this direction. I'm beginning to think that lambdas/closures/function pointers are design techniques that we should be more fully exposed to in many languages. Of course there are trade-offs and limitations regardless of programming language, but it seems like you can get the most mileage maybe of thinking about your program design in those techniques rather than requiring some specific implementation of a lambda or closure structure. I'm not expert in this area - look into it more, write something up, and post back to us here.
- friggeri 15y agoThanks, interesting read. Will certainly follow up if I find more interesting stuff.
- JoachimSchipper 15y agoLook at Apple's blocks (LLVM/Darwin and FreeBSD only, I believe), GCC's anonymous or nested functions (may be supported by LLVM too, they tend to copy GCC language extensions), and (a very restricted subset of) C++. Also note that C is easy to parse. It's definitely possible to write a C-to-C compiler that supports parts of Apple's syntax (e.g. "oneFrom = ^(int anInt) { return anInt - 1; };"), just by replacing every ^ by a function pointer to a function defined at the end of the current file. Of course, blocks is much more than that (e.g. includes memory management), and there is a reason for that...
- apaprocki 15y agoLink to GCC anonymous function example: http://en.wikipedia.org/wiki/Anonymous_function#C_lambda_expressions http://en.wikipedia.org/wiki/Anonymous_function#C_lambda_exp...