I have some ugly code that I’d like to improve. My...
# getting-started
e
I have some ugly code that I’d like to improve. My goal is to create an array
results
as long as the list
neededPermissions
where every element of
results
has the value
PackageManager.PERMISSION_GRANTED
except for a single element. Here’s my code:
Copy code
val results = neededPermissions.map { PackageManager.PERMISSION_GRANTED }.toIntArray()
results[0] = PackageManager.PERMISSION_DENIED
I know how to do what I want in Scheme (with
cons
and
cdr
). What’s a nice way to do it in Kotlin?
🦗 1
s
I might be completely misunderstanding your question but
Copy code
val results = intArrayOf(PackageManager.PERMISSION_DENIED) + IntArray(neededPermissions.size - 1) { PackageManager.PERMISSION_GRANTED }
1
e
Thanks, @Shawn. That answers my question. I also appreciate seeing how to use an approach lighter weight than
map
to create an array of constant values.
👍 3
k
Can you give an example of what you want?
e
@karelpeeters: @Shawn’s code shows what I want.
k
Well what's wrong with that code them? What do you need to be different?