Smalltalk collect on Go slice of int

package main

import "fmt"

type intSlice []int

func (i intSlice) collect(block func(i int) int) intSlice {
	r := make(intSlice, len(i))
	for j, v := range i {
		r[j] = block(v)
	return r

func main() {
	numbers := intSlice{1, 2, 3}
	squared := func(i int) int { return i * i }

	fmt.Printf("%v", numbers.collect(squared))

Try it.

Implementations of select,inject,detect are left as an excercise for the reader.

comments powered by Disqus