David Smith
04/22/2021, 3:48 PMIterable<T>.map
returns a List<R>
not the original data structure (whatever implemented the Iterable
. This seems very strange to me, is it expected? I would expect the map
function to affect only the contents of the data structure, not the data structure itself.Nir
04/22/2021, 3:52 PMNir
04/22/2021, 3:52 PMVampire
04/22/2021, 3:53 PMIterable<T>.map
could return Iterable<R>
Vampire
04/22/2021, 3:54 PMList<R>
implements Iterable<R>
🙂David Smith
04/22/2021, 3:54 PMNir
04/22/2021, 3:54 PMNir
04/22/2021, 3:55 PMDavid Smith
04/22/2021, 3:55 PMNir
04/22/2021, 3:56 PMNir
04/22/2021, 3:56 PMTomasz Krakowiak
04/22/2021, 3:56 PMsomeMap.map{ it.toString()}
... hmm?Nir
04/22/2021, 3:57 PMmap
in Kotlin isn't that it doesn't return the same data structureNir
04/22/2021, 3:57 PMmap
isn't just returning a lazy iterator, that you later decide to collect in a data structureNir
04/22/2021, 3:57 PMVampire
04/22/2021, 3:58 PMMyDatabaseResult
that lazily gives you the results from the database when you query them.
How should the result of map
create an instance of that class to satisfy your requirement?David Smith
04/22/2021, 3:58 PMmap
function on that that works lazily or strictly, depending on the iterator?Nir
04/22/2021, 3:58 PMNir
04/22/2021, 3:58 PMdmitriy.novozhilov
04/22/2021, 3:59 PMIterable
interface have only one method: fun iterator(): Iterator<T>
. There is no information how to construct original type on which map
was called.Nir
04/22/2021, 3:59 PMNir
04/22/2021, 3:59 PMdmitriy.novozhilov
04/22/2021, 3:59 PMclass MySuperIterable : Iterable<String> {
...
}
fun test(it: MySuperIterable) {
val result = it.map { it.length }
}
In this example there is no way to create MySuperIterable
which holds Int
David Smith
04/22/2021, 3:59 PMDavid Smith
04/22/2021, 4:00 PMmap
would need it’s own interfaceVampire
04/22/2021, 4:00 PMNir
04/22/2021, 4:00 PMDavid Smith
04/22/2021, 4:00 PMmap
would change the data structureNir
04/22/2021, 4:01 PMNir
04/22/2021, 4:01 PMDavid Smith
04/22/2021, 4:01 PMPage<T>
type which wraps up a list with some other infoNir
04/22/2021, 4:01 PMNir
04/22/2021, 4:02 PMDavid Smith
04/22/2021, 4:02 PMNir
04/22/2021, 4:02 PMNir
04/22/2021, 4:03 PMmyPage.asSequence().map { ... }.toPage();
Vampire
04/22/2021, 4:04 PMmap
know how to instantiate a Page<R>
?
And more so if the type bounds for T
maybe don't even allow R
.David Smith
04/22/2021, 4:04 PMtoPage
function because there is extra data in the Page object (page number, page size etc)Nir
04/22/2021, 4:04 PMmapPage
or something like that. You could also choose to have Page<T> implement Sequence<T> rather than Iterable<T>David Smith
04/22/2021, 4:04 PMNir
04/22/2021, 4:04 PMmap
could magically know how to do thatDavid Smith
04/22/2021, 4:05 PMmapPage
but that was a bit of a hack, I thought there would be some interface that I could implementNir
04/22/2021, 4:05 PMtransform
, which takes a lambda which accepts a List<T> and returns a List<T>Nir
04/22/2021, 4:05 PMDavid Smith
04/22/2021, 4:05 PMmap
interfaceDavid Smith
04/22/2021, 4:05 PMNir
04/22/2021, 4:06 PMNir
04/22/2021, 4:06 PMDavid Smith
04/22/2021, 4:06 PMNir
04/22/2021, 4:06 PMDavid Smith
04/22/2021, 4:06 PMNir
04/22/2021, 4:06 PMNir
04/22/2021, 4:07 PMmap
itself, then you'll have the same issue again with say filter
Nir
04/22/2021, 4:07 PMtransform
David Smith
04/22/2021, 4:07 PMDavid Smith
04/22/2021, 4:07 PMDavid Smith
04/22/2021, 4:07 PMNir
04/22/2021, 4:07 PMNir
04/22/2021, 4:07 PMNir
04/22/2021, 4:08 PMNir
04/22/2021, 4:08 PMNir
04/22/2021, 4:08 PMDavid Smith
04/22/2021, 4:08 PMephemient
04/22/2021, 4:09 PMNir
04/22/2021, 4:09 PMephemient
04/22/2021, 4:09 PM.mapTo(builder) { transform(it) }
Nir
04/22/2021, 4:10 PMPage<T>.transform(f: (List<T>) -> List<T>): Page<T>
Nir
04/22/2021, 4:10 PMNir
04/22/2021, 4:11 PMnewPage = oldPage.transform { oldList ->
oldList.map { .... }.filter { ... } etc etc
}
David Smith
04/22/2021, 4:25 PMfun <A, B> Page<A>.map(f: (A) -> B): Page<B> {
val bs = this.values.map(f)
return Page.slice(bs, this.totalElements, Pageable(this.pageNumber, this.pageSize))
}
David Smith
04/22/2021, 4:27 PMfilter
is a bit of a weird function compared to map because by it’s nature it changes the data structureDavid Smith
04/22/2021, 4:29 PMthis.pageSize
etcDavid Smith
04/22/2021, 4:36 PMNir
04/22/2021, 4:39 PMNir
04/22/2021, 4:40 PMNir
04/22/2021, 4:40 PMRuckus
04/22/2021, 5:02 PMDavid Smith
04/22/2021, 5:03 PMDavid Smith
04/22/2021, 5:05 PMNir
04/22/2021, 5:08 PMDavid Smith
04/22/2021, 5:08 PMNir
04/22/2021, 5:08 PMNir
04/22/2021, 5:08 PMDavid Smith
04/22/2021, 5:09 PMNir
04/22/2021, 5:09 PMNir
04/22/2021, 5:09 PMNir
04/22/2021, 5:09 PMTomasz Krakowiak
04/22/2021, 5:42 PMNir
04/22/2021, 5:46 PMNir
04/22/2021, 5:46 PM