You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
** Also note: this is super useful for doing inline operation
Implementation
New time contributor here, so I did not know that proposals were needed before making API change. I already have proposed implementation using the patterns that the slices packages uses:
// Map returns a new slice containing the result of applying the mapper function to each element of the provided.// The result has the same length as the input slice.// The result is never nil.// The result will be in the same order as the input slice.// The mapper function must not modify the elements of the input slice.funcMap[S~[]E, E, Rany](sliceS, mapperfunc(int, E) R) []R {
mappedSlice:=make([]R, len(slice))
fori, v:=rangeslice {
mappedSlice[i] =mapper(i, v)
}
returnmappedSlice
}
// Filter returns a new slice containing only the elements of the provided slice for which the filter function returns true.// The result has length <= len(slice).// The result is never nil.// The result will be in the same order as the input slice.// The filter function must not modify the elements of the input slice.funcFilter[S~[]E, Eany](sliceS, filterfunc(int, E) bool) []E {
filteredSlice:=make([]E, 0, len(slice))
fori, v:=rangeslice {
iffilter(i, v) {
filteredSlice=append(filteredSlice, v)
}
}
returnClip(filteredSlice)
}
The text was updated successfully, but these errors were encountered:
Reading through the similar issues, all of the similar proposals have been closed and they seem to point to #47203 (comment) as the reason to why.
Deleted Map, Filter, Reduce (probably better as part of a more comprehensive streams API somewhere else)
Reasoning to why these haven't been implemented is in anticipation of this "streams API" which I would assume would refer to the iter package: #61899. Likewise, slices seems to be getting these iterator functions as recent as a month ago: https://go-review.googlesource.com/c/go/+/568477.
However, it seems as though functions like Map and Filter might have fallen through the cracks?
Proposal Details
I propose the creation of two new function,
Map
andFilter
, to theslices
package:Map
Map
creates a new slice containing the results of running all indices of a slice through a provided functionsExample:
Filter
Filter
create a new slice containing a subset of indices of an existing slice based on running all indices through a provided filter functionExample:
Reasoning
Personally I find myself creating these exact function in most of my projects and they help a ton with writing clean code.
It allows me to change from code like this:
and simplify it something like this:
** Also note: this is super useful for doing inline operation
Implementation
New time contributor here, so I did not know that proposals were needed before making API change. I already have proposed implementation using the patterns that the
slices
packages uses:The text was updated successfully, but these errors were encountered: