

Ask HN: C with lambdas? - friggeri

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).<p>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.<p>If this hasn't been done, any ideas why ? Is it technically feasible ?
======
JoachimSchipper
Look 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...

------
tom_b
Have a look at:

[http://stackoverflow.com/questions/208835/function-
pointers-...](http://stackoverflow.com/questions/208835/function-pointers-
closures-and-lambda)

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
Thanks, interesting read. Will certainly follow up if I find more interesting
stuff.

------
apaprocki
Link to GCC anonymous function example:

[http://en.wikipedia.org/wiki/Anonymous_function#C_lambda_exp...](http://en.wikipedia.org/wiki/Anonymous_function#C_lambda_expressions)

------
pdw
C++ has lambdas.

~~~
friggeri
I 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
"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
I 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.

