News for hackers. Stuff that matters.
Re:Loopless Programming by agumonkey on Saturday October 19, 2019 @07:41PM (#21301393)
You can say this now but not long ago map/filter/reduce were almost esoteric.
Re:Loopless Programming by whateveracct on Saturday October 19, 2019 @09:00PM (#21301688)
I'd say even just 5 years ago they were weird FP features, with LINQ being the most mainstream version. Now Go is the only mainstream language without them hehe.
Re:Loopless Programming by suzuki on Monday October 21, 2019 @09:09AM (#21311820)
You can write your own LINQ in Go with a very short program. See https://github.com/nukata/linq-in-go for example.

Here is a self-contained excerpt:

  type Any = interface{}

  type Enumerator func(yield func(element Any))

  // Select creates an Enumerator which applies f to each of elements.
  func (loop Enumerator) Select(f func(Any) Any) Enumerator {
    return func(yield func(Any)) {
      loop(func(element Any) {
        value := f(element)
        yield(value)
      })
    }
  }

  // Range creates an Enumerator which counts from start
  // up to start + count - 1.
  func Range(start, count int) Enumerator {
    end := start + count
    return func(yield func(Any)) {
      for i := start; i < end; i++ {
        yield(i)
      }
    }
  }
Now you can write the following:

  squares := Range(1, 10).Select(func(x Any) Any { return x.(int) * x.(int) })
  squares(func(num Any) {
    Println(num)
  })
  // Output:
  // 1
  // 4
  // 9
  // 16
  // 25
  // 36
  // 49
  // 64
  // 81
  // 100
I'd say it is so elegant in Go!
Re:Loopless Programming by jadbox on Tuesday October 22, 2019 @11:26AM (#21324712)
It's a little annoying though to be forced to use interface{} as type parameters for higher order list operations as there's a performance overhead when using it. Hopefully this is remedied when Go gets type generic support.
Re:Loopless Programming by whateveracct on Tuesday October 22, 2019 @12:08PM (#21325305)
Not to mention the complete lack of type safety (there are unsafe-style casts even in the example code.)
Re:Loopless Programming by suzuki on Thursday October 24, 2019 @09:44AM (#21344596)
Good point! I'm looking forward to generics in Go.
Re:Loopless Programming by jadbox on Tuesday October 22, 2019 @11:24AM (#21324680)
It's my #1 problem with Go, and there's no good solution imho other than Go getting some kind of generic type support.