# Accumulate

### Introduction

Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of strings:

* "cat", "Dog", "b4t", "gO"

And the operation:

* upcase a string

Your code should be able to produce the collection of strings:

* "CAT", "DOG", "B4T, "GO"

Check out the test suite to see the expected function signature.

{% tabs %}
{% tab title="Version 1.0" %}

```go
package accumulate

// Accumulate change slice content base on the converter
func Accumulate(s []string, converter func(string) string) []string {
	result := s
	for i, r := range s {
		result[i] = converter(r)
	}
	return result
}

```

{% endtab %}

{% tab title="Other solution" %}

```go
package accumulate

type Converter func(string) string

func Accumulate(in []string, cn Converter) (out []string) {
	for _, s := range in {
		out = append(out, cn(s))
	}
	return
}
```

{% endtab %}
{% endtabs %}

### Point&#x20;

* If you defined return value in func definition, you don't need declare return variable in return clause
* out is a slice, use append to add value. not `out[0] = s`&#x20;

Other solution would also work and looks simpler. But it has a worse performance than my solution.&#x20;

**version1.0**&#x20;

`BenchmarkAccumulate-8 10000000 183 ns/op 0 B/op 0 allocs/op`&#x20;

**Other**&#x20;

`BenchmarkAccumulate-8 2000000 961 ns/op 344 B/op 18 allocs/op`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dingyj.gitbook.io/blog/golang/exercism/accumulate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
