News for hackers. Stuff that matters.
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @04:22PM (#2720004)
This is just too clumsy, and this is exactly why I think go will take off.

  package main
  
  import "fmt"
  
  func main() {
      add := func(x int, y int) int{
        return x+y
      }
      max := func(x int, y int) int{
        if x>y {return x}
        return y
      }
    fmt.Printf("lambda: add:%d\n",add(2,3))
    fmt.Printf("lambda: max:%d\n",max(2,3))
  }
Although it'd be nice if you could have a ternary or something like

  return {x} if x>y else {y}
Re:Lambdas in C by gok on Friday July 01, 2011 @05:24PM (#2720215)
Is that really that much better than C blocks?

    #include <stdio.h>
    int main (int argc, char const *argv[]) {
        int (^add)(int, int) = ^(int x, int y) {
            return x + y;
        };
        int (^max)(int, int) = ^(int x, int y) {
            return (x > y) ? x : y;
        };
        printf("block: add:%d\n", add(2,3));
        printf("block: max:%d\n", max(2,3));
        return 0;
    }
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @05:36PM (#2720249)
I personally think so, as I think blocks in C are a clumsy way around the issue (probably because C doesn't natively have garbage collection) and it feels natural in go, but I know others will disagree.

Also, I don't think you can make it anonymous, can you?

  func(x int, y int){fmt.Printf("%d\n",x+y)}(2,4)
Re:Lambdas in C by gok on Friday July 01, 2011 @06:44PM (#2720449)
You sure can.

   ^(int x, int y){printf("%d\n",x+y);}(2,4);
Automatic memory management is interesting, but orthogonal.
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @04:27PM (#2720020)
also, this works too:

  val := func(x int, y int) int{return x+y}(2,4)
Re:Lambdas in C by huhtenberg on Friday July 01, 2011 @06:02PM (#2720324)
Nice, but how is this useful though?