[go-nuts] Re: Small complete examples which show the power of Go?

2016-08-15 Thread anupam . kapoor

,
| I always liked Rob Pike's concurrent prime seive:
| https://play.golang.org/p/9U22NfrXeq
`
i think what you are looking for is an elegant paper by doug-mcllroy
called "squinting at power series"

which bring the power of channels etc to stream processing...

--
kind regards
anupam

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Small complete examples which show the power of Go?

2016-08-10 Thread Val
Iterations over slices, maps, channels are very cool, usually straight to 
the point :

func main() {
for _, a := range []int{6, 4} {
for _, b := range []int{2, 3} {
for fname, f := range map[string]func(int, int) int{
"plus":  func(x, y int) int { return x + y },
"minus": func(x, y int) int { return x - y },
"times": func(x, y int) int { return x * y },
"div":   func(x, y int) int { return x / y },
} {
fmt.Println(a, fname, b, "is", f(a, b))
}
}
}
}
Playground 

Then you may tell some specific details : why the underscores for ignored 
iteration variables, and why the map iteration order is not the same as the 
order in the code. Also, I find these iterations quite versatile in 
practice, but they work only on built-in types (you won't have java-like 
custom iterables).

Cheers
Val

On Wednesday, August 10, 2016 at 9:53:38 AM UTC+2, 
gary.wi...@victoriaplumb.com wrote:
>
> Hi,
>
> I'm giving a talk at work introducing Go and I'm looking for small 
> examples to show Go's potential. For example, the following program 
> demonstrates a bare-bones webserver in 13 lines:
>
> import (
>
> "fmt"
> "net/http"
> )
>  
> func home(w http.ResponseWriter, r *http.Request) {
> fmt.Fprintf(w, "Hello, world!")
> }
>  
> func main() {
> http.HandleFunc("/", home)
> http.ListenAndServe(":8080", nil)
> }
>
> Has anyone here got any more little snippets like this to show the power 
> and potential of Go? It doesn't have to be in networking, just little a 
> little snippet to make people think "wow, that's cool!".
>
> Thanks.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Small complete examples which show the power of Go?

2016-08-10 Thread sphilippov
Easy parallelization and concurrency:

package main 

import (
 "runtime"
)
 
func FoldParallel(data []float64, initialValue float64, op func(float64, 
float64) float64) float64 {
sliceSize := len(data) / runtime.NumCPU()
results := make(chan float64, runtime.NumCPU())
numResults := 0
for i := 0; i < len(data); i += sliceSize {
numResults++
go func(dataSlice []float64) {
result := float64(initialValue)
for _, v := range dataSlice {
op(result, v)
}
results <- result
}(data[i : i+sliceSize])
}
result := initialValue
for i := 0; i < numResults; i++ {
result = op(result, <-results)
}
return result
}

func main() {
var data [1000]float64

// parallel sum
FoldParallel(data[:], 0, func(v1, v2 float64) float64 { return v1 + v2 
})
// parallel mul
FoldParallel(data[:], 1, func(v1, v2 float64) float64 { return v1 * v2 
})
}



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.