Cheers
Dave
Another one: map() in Pythonmap(lambda x:x*2, range(100))
--
Michael T. Jones | Chief Technology Advocate | m...@google.com | +1
650-335-5765
I'm not eager, but also not unwilling to write
a.AddIntervalComplexRational64(b)
if I have to, but I sure think that a compiler, preprocessor, macro
system, metaprogramming language, or some such should allow me the
luxury of building the cross product of a dozen orthogonal behaviors
by specifying code for the dozen behaviors and rules for composition
rather than me coding 4096 discrete solutions in vi. Saying no to this
is like saying, you don't need integer multiply, just add. a lot.
--
but I sure think that a compiler, preprocessor, macro
system, metaprogramming language, or some such should allow me the
luxury of building the cross product of a dozen orthogonal behaviors
by specifying code for the dozen behaviors and rules for composition
rather than me coding 4096 discrete solutions in vi.
If you have two unbuffered channels, var cx chan X, and var cy chan Y, and something like:cy <- foo(<-cx) and foo is a func (X) Yand you have two other pieces of code, one that sends on cx, and one that receives on cy, you will never move forward.The sender will block until the goroutine running foo receives from cx, and the receiver will block until the same goroutine sends on cy.
Another one: map() in Python:map(lambda x:x*2, range(100))How can i archive this in Go?nv := Map(func (x int) int { return x * 2 }, []int{1,2,3})I had tried reflect solution, the performance is really bad.func Map(f interface{}, v interface{}) interface{} {
}
- Davies
The generic Go solution to map() is:
s := []int{1,2,3}
f := func(x int){return x*2}
for i,_:= range s {
s[i] = f(s[i])
}
Which has even better readability than a call to map().
--
=====================
http://jessta.id.au
or even:
s := []int{1,2,3}
for i := range s {
s[i] *= 2
}
:-)
It's more like,
s := []int{1,2,3}
s2 := make([]int, len(s))
for i, v := range s {
s2[i] = v*2
}
Repeat for every type of slice you'd be doing a map on.
I also see the style used in sort package being recommended instead of
full generic support. Slices are a lot more commonly sorted than
anything else and re-implementing Swap and Len for every type of slice
is redundant. With proper generics, the Sort function can just take a
slice and a function which compares 2 items of the slice.
To paraphrase someone (I don't remember who):Go programs should be written in a go mindset.
This is NOT yet another "I want generics in Go" or "why Go doesn't
have generics?" post.
In fact, on the contrary, the idea is to enumerate the Use Cases for
Generics and how Go solves them without Generics.
In this way we could:
1- Point any newcomer to this thread when he/she asks the same old
question again and no wasting more time in that.
2- Isolate the USE CASES that call for (or force) a Generics based
solution, one by one and identify:
* Whether or not there are good enough solutions in Go right NOW,
with some sample of them.
* If there is NOT a PROPER solution right now, or it is too complex,
ugly or gives bad performance or maintenance:
* Would it be easily solvable (and worth) by adding an orthogonal
NON-generics feature to Go or not?
Who knows, we might find that generics always have good alternative
solutions! (or we may not)
> because every time implements the empty interface.
s/time/type/, sorry
In my opinion, it's acceptable to cope with this problem outside the compiler with the technique which is already used in package "html/template".In other words, the Go team can provide us with a set of offical templates in which standard algorithms are implemented and a tool with which we can compile the template into real go code. In this case, the automated code is easy to read and debug and maintain, and is also reliable. And we should be able to write our own template to implement our own business logic.This technique is a bit old but practical.
The Go team has not said they are opposed to programs that generate Go code. They have just said that the go tool is not the tool to use for that, use Make (or some other system). Most projects do not need the complication and they are what the go tools is aimed at.-Paul
Perhaps Russ doesn't miss generics when programming in go because he
is able to write generic functions when he wants to do so! :)
Not that I desire Generics, but I DO want something that, in the cases you need that kind speed, you can do without interface invoking costs, specially on primitive types.One example it the Sort package. The interface is an elegant solution, and its speed is probably fine for most cases. I believe that for non primitive types it probably can't get much better than that.
All that said, I've never seen a Go application (package main), "in the wild", that would either need or truly benefit from generics...
I'm currently hitting stumbling blocks with the lack of generics in Go after hitting some issues in this SO question http://stackoverflow.com/questions/11908074/enforcing-type-in-generic-code-with-empty-interfacesA lot of code in the book I'm reading uses core ADTs like Queue
Stack
Bag
and so on that are containers that utilize generics to make the code concise and compact.I'm having trouble trying to work out how to translate these effectively to Go without having to write lots and lots of code that essentially does the same thing.
I'm just wondering if anyone has experienced anything similar to this in their code (e.g. a generic Stack or Queue ADT) and what they did to get around it.
On Friday, April 20, 2012 11:05:15 AM UTC+1, josvazg wrote:This is NOT yet another "I want generics in Go" or "why Go doesn't
have generics?" post.
In fact, on the contrary, the idea is to enumerate the Use Cases for
Generics and how Go solves them without Generics.
In this way we could:
1- Point any newcomer to this thread when he/she asks the same old
question again and no wasting more time in that.
2- Isolate the USE CASES that call for (or force) a Generics based
solution, one by one and identify:
* Whether or not there are good enough solutions in Go right NOW,
with some sample of them.
* If there is NOT a PROPER solution right now, or it is too complex,
ugly or gives bad performance or maintenance:
* Would it be easily solvable (and worth) by adding an orthogonal
NON-generics feature to Go or not?
Who knows, we might find that generics always have good alternative
solutions! (or we may not)
But even just using slices I still need to duplicate code to work with different types. That's the problem I'm trying to get across. A stack is a pretty fundamental computer science concept and even just with slices, you'll need to reimplement push/pop for each individual slice type, wether it be []int, []string,[]byte etc.
Yes you can use a bit of reflection/casting but this is deferring the type checking to the runtime, and thus ultimately to the developer.
package pq
import (
"error",
"reflect"
)
type Comparible interface {
Less(interface{}) bool
Equal(interface{}) bool
}
// this has to be done for all native types: int, uint, float and thier bit count versions
func (a int) Less(b interface{}) bool {
bi, ok := b.(int)
if !ok { panic Error.New("type mismatch" }
return a<b;
}
func (a int) Equal(b interface{}) bool {
bi, ok := b.(int)
if !ok { panic Error.New("type mismatch" }
return a==b;
}
type PriorityQueue interface {
Enqueue(interface{}, Comparible)
Dequeue(*interface{}, *Comparible)
}
type pair struct {
entry interface{}
priority Comparible
}
type pq struct {
dt Type
ct Type
entries pair[]
}
func New(dt Type, ct Type) PriorityQueue {
return &pq{dt,ct,make(pair[])}
}
func (this *pq) Enqueue(data interface{}, priority Comparible) Error {
if this.dt != reflect.TypeOf(data) { return Error.New("data type mismatch") }
if this.pt != reflect.TypeOf(priority) { return Error.New("priority type mismatch") }
...
// add data priority to this.entries
}
func (this *pq) Dequeue(data *interface{}, priority *Comparible) Error {
if this.dt != reflect.TypeOf(*data) { return Error.New("data type mismatch") }
if this.pt != reflect.TypeOf(*priority) { return Error.New("priority type mismatch") }
...
// fill in data and priority with the correct entry from this.entries, removing the entry
}
Does that mean a C# type implementing IComparable must use a weird
inconsistent typing for the Less and Equal methods with possible
runtime exceptions? How can that be better than Go?
It is certainly okay if people don't use Go when they obviously want
to do something that is not possible in Go.
On Friday, April 20, 2012 6:52:35 AM UTC-4, Davies wrote:Another one: map() in Pythonmap(lambda x:x*2, range(100))- Polymorphic functions that operate on built-in generic types.func KeyIntersection(a map[T1]T2, b map[T1]T2) map[T1]T2 {}