tobyhede / go-underscore

Helpfully Functional Go - A useful collection of Go utilities. Designed for programmer happiness.
Other
1.3k stars 61 forks source link

several suggestions #8

Open ggaaooppeenngg opened 9 years ago

ggaaooppeenngg commented 9 years ago

I browsed your code and have some suggestions.

first, I think underscore need a more general function instead of specifying a function for a certain type,for instance in order to map string you need to specify a SMap function pointer.Your code was excellent,could be there any way to implement such general function? such like not returning []interface{},but receive a point of return value as argument,in order to reflect the result.

second, in Each, I think sync.WaitGroup is more comlicated because it is implemented by lock,and why not do like


var done = make(chan struct{}{},m.Len()) //m is the map or slice
for i:= 0;i<s.Len();i++{
    go func(){
        donw <-
    }() 
}

for i:= 0;i<s.Len();i++{
    done <-
}

I thins this dose the same work,but in more Go way.

third, function decleration is not constant,Each has argument name but Map does not which will make some confusion.

fourth chan argument should be more strict ,to spefigy read-only or write-only reader need argument like job <- chan,which should only read from in the function body.

fifth , jobs in Each should be bufferd capabible of len of workers number, so that workers will not block on read or write and will be more akin parallel

That's my thoughts,maybe it's wrong or not appropriate,hoping to discuss!

elgs commented 8 years ago

Shouldn't the index of the array be passed in the Map's callback function?