stream.Map

Convert the current elements in the stream into a different type.

Function signature

    func Map(fn interface{}) output

Arguments

Name Type Description
fn func This function must receive 1 arguments, that must have the same type that current elements in the stream. The output of this function will be the new type op elements in the stream

Output

Name Type Description
stream koazee.S It returns the stream modified by the current operation

Errors


</tbody>

Type Message
err.items-nil A nil stream can not be iterated
err.invalid-argument The map operation requires a function as argument
err.invalid-argument The provided function must retrieve 1 argument
err.invalid-argument The provided function must return 1 value
err.invalid-argument The type of the argument in the provided function must be %s

Examples

package main

import (
	"fmt"
	"github.com/wesovilabs/koazee"
	"strings"
)

var numbers = []int{1, 3, 5, 7, 9}

func main() {
	output := koazee.Stream().
		With(numbers).
		Map(func(item int) string {
			return strings.Repeat("a", item)
		}).Out().Val().([]string)
	fmt.Printf("The output is %v\n", output)
}
package main

import (
	"fmt"
	"github.com/wesovilabs/koazee"
)

type genre int

const (
	female genre = iota
	male
)

type primate struct {
	name   string
	age    int
	family string
	genre  genre
}

func newPrimate(name string, age int, family string, genre genre) *primate {
	return &primate{
		name:   name,
		age:    age,
		family: family,
		genre:  genre,
	}
}

var primates = []*primate{
	newPrimate("John", 15, "Capuchin", male),
	newPrimate("Laura", 12, "Squirrel monkey", female),
	newPrimate("Benjamin", 23, "Spider monkey", male),
	newPrimate("George", 19, "Golden Lion Tamarin", male),
	newPrimate("Jane", 33, "Orangutan", female),
	newPrimate("Sarah", 7, "Gibbon", female),
}

func main() {
	ages := koazee.StreamOf(primates).
		Map(func(primate *primate) int {
			return primate.age
		}).Out().Val()

	fmt.Printf("The ages of the primates is %v years\n", ages)

}