Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C with Lambdas and Closures

Name: Anonymous 2013-07-16 19:11

Best of both worlds or horrible abomination?

Name: Anonymous 2013-07-18 10:11

Clojure is just syntatic sugar. See these examples that do the same:

Ruby

a = [1, 2]
b = "hello"
f = lambda do
  a << 42
  puts b
end
f.()
# a is not [1, 2, 42], "hello" is printed


C

struct State_1
{
  int** a;
  int* a_end;
  const char** b;
};

void f(const struct State_1* state)
{
  (*state->a)[state->a_end] = 42;
  (*a_end)++;
  puts(*state->b);
}

int main()
{
  State_1 s1;
  int a[256];
  int a_end = 0;
  const char* b = "hello";

  a[a_end++] = 1;
  a[a_end++] = 2;

  s1.a = &a;
  s1.a_end = &a_end;
  s1.b = &b;

  return 0;
}


These do exactly the same thing. The real difference is that C does it about 120 times faster.

In terms of code length, Ruby is slightly better. However, considering performance issues, it's hardly worth it. In real world, typing those extra letters takes practically zero time compared to the time that is spent designing higher level structures.

To answer to your question: I don't think it would be horrible abomination. However, I thing it closures are not needed in C. They bring more float to a language that is right now simple and elegant.

If you want lambdas so desperately, go try C++11. Let's see how happy you are then. HAH. Enjoy your bloat.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List