Wonder if there is a nice way to make this shorter...
# codereview
e
Wonder if there is a nice way to make this shorter
Copy code
protected fun prepareStandardSemaphores(device: VulkanDevice): ConcurrentHashMap<StandardSemaphores, VkSemaphore_Array> {
        val map = ConcurrentHashMap<StandardSemaphores, VkSemaphore_Array>()
        StandardSemaphores.values().forEach {
            map[it] = VkSemaphore_Array(swapchain.images.size) {
                vkDev.createSemaphore(semaphoreCreateInfo) 
            }
        }
        return map
    }
d
Copy code
protected fun prepareStandardSemaphores(device: VulkanDevice): ConcurrentHashMap<StandardSemaphores, VkSemaphore_Array> {
        return StandardSemaphores.values().associateWithTo(ConcurrentHashMap()) {
            VkSemaphore_Array(swapchain.images.size) {
                vkDev.createSemaphore(semaphoreCreateInfo) 
            }
        }
    }
Only 2 lines shorter but more functional I guess.
e
Nice, thanks
s
You could use fold instead of foreach so it would be something like that = ss.values.fold(CHM<SS,VKSAA>(), { (map, v) -> map.also { it[v] = .. })
e
problem with
associateByTo
is that it maps K and V the other way round as I would like it
d
What about
associateWithTo
?
e
I see none
d
Are you on 1.3.21?
e
e
weird, I dont see it
I explicitely imported, but it looks like
Array<T>
isnt recognized as
Iterator<T>
, although it has
Copy code
/**
     * Creates an iterator for iterating over the elements of the array.
     */
    public operator fun iterator(): Iterator<T>
d
OH, I forgot about that. Yeah,
Array
doesn't implement
Iterable
.
You could use
asList()
on the array.