Skip to content

Operation map

Iván Corrales Solera edited this page Dec 2, 2018 · 2 revisions

stream.Map

It converts the current elements in the stream into a different type.

Signature

func (s stream.Stream) Map(fn interface{interface{}}) (out stream.Stream)

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
out stream.Stream It returns the stream

Errors

Type Description
err.items-nil The stream is nil
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 same thant the elements in the stream

Example

package main

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

var stream = koazee.StreamOf([]string{"Dog", "Cat", "Monkey", "Rabbit", "Turtle"})

func main() {
  stream = stream.Map(func(val string) int { return len(val) }).Do()
  for _, e := range stream.Out().Val().([]int) {
    fmt.Println(e)
  }
}

Download

Benchmark

Converting a stream of strings in a new stream of strings with uppercase

Test Identifier Stream Len Speed
BenchmarkMapString10ToUpper-4 10 1366 ns/op
BenchmarkMapString100ToUpper-4 10 7496 ns/op
BenchmarkMapString1000ToUpper-4 10 79248 ns/op
BenchmarkMapString5000ToUpper-4 10 507272 ns/op

Converting a stream of strings in a new stream of int with the len of all the elements

Test Identifier Stream Len Speed
BenchmarkReduceInt10Sum-4 10 324 ns/op
BenchmarkReduceInt100Sum-4 100 629 ns/op
BenchmarkReduceInt1000Sum-4 1000 3782 ns/op
BenchmarkReduceInt5000Sum-4 5000 9533 ns/op

|