Wednesday, September 24, 2014

array and map comprehensions


There was alot of talk on the golang mailing list about adding array comprehensions to Go, but nothing happened, thread. Gython now supports array and map comprehensions.

array comprehension

a = []int( x for x in range(3) )

An array comprehension is like a Python list comprehension, but it is for a fixed data type and looks like a generator expression.

array comprehension - go output

 __comp__0 := []int{};
 idx0 := 0;
 iter0 := 3;
 for ( idx0 ) < iter0 {
  x := idx0;
  __comp__0 = append(__comp__0, x);
  idx0 += 1;
 }
 a := &__comp__0;

map comprehension

m = map[int]string{ a:'xxx' for a in range(10)}

A map comprehension is like a Python dict comprehension.

map comprehension - go output

  __comp__0 := &map[int]string{  };
  idx0 := 0;
  iter0 := 10;
  for ( idx0 ) < iter0 {
    
    a := idx0;
    (*__comp__0)[a] = "xxx";
    idx0 += 1;
  }
  m := __comp__0;

No comments:

Post a Comment