Open Lasering opened 1 year ago
These methods are more difficult to argue for their inclusion but they are still useful (names could be improved):
extension [CC[_, _]](factory: MapFactory[CC])
def iterateKey[A, K](iterable: IterableOnce[A])(key: A => K): CC[K, A] =
iterate(iterable)(e => (key(e), e))
def iterateValue[A, V](iterable: IterableOnce[A])(value: A => V): CC[A, V] =
iterate(iterable)(e => (e, value(e)))
You can use view
to avoid intermediate collection:
list.view.map { a =>
val value = //some code to compute the value, mostly likely using `a`
a.name -> value
}.toMap
Does this have some drawbacks for you?
Does this have some drawbacks for you?
In terms of performance no, but in terms of readability and consistency with the IterableFactory yes. I find it easier to read:
Map.iterate { a =>
val value = //some code to compute the value, mostly likely using `a`
a.name -> value
}
Mainly because the intention of creating a map is in the beginning of the code, and the cognitive load of reading the code is smaller.
Those are indeed very handy. I've added those to my small homegrown lib.
https://github.com/jozic/scalax-collection#maptomap
https://github.com/jozic/scalax-collection#tomapwithkey
https://github.com/jozic/scalax-collection#tomapwithvalue
But if they appear in this lib, i will happily switch.
I'm constantly doing (or variations of):
Which is annoying since I'm allocating an intermediate list when I would prefer to build the map directly.
A method like this could be added to MapFactory:
(Feel free to bike shed the names/implementation)
Currently IterableFactory has some very good utility methods to construct iterables such as
fill
,iterate
,range
, andtabulate
. MapFactory on the other hand has nothing.