What's wrong with this call to `fold`, which cause...
# getting-started
e
What's wrong with this call to
fold
, which causes IntelliJ to give me a very odd suggestion?
IntelliJ suggests (on line 3) changing
List<Int>
to
List<Int>
. (Spoiler: It doesn't help.)
😨 1
The problem turned out to be due to the placement of the plus signs. If I move them from the starts of lines 5 and 6 to the ends of lines 4 and 5, the code works.
s
interesting, this might be one of those case where the Kotlin compiler gets confused (= the expression is ambiguous) due to the language not using semicolons!?? 🤔
m
interesting, if you remove the type ( just have fold have "acc, num -> ") then it works
Copy code
return numList.fold(emptyList()) { acc, num ->
    acc.filter { it <= num } +
        num +
        acc.filter { it > num }
}
s
@Michael de Kaste it's probably not removing the types but putting the pluses at the end of the line that made it work.
👍 3
p
It is indeed because of the location of the pluses. By having them at the start of the lines you're actually invoking the
unaryPlus
operator, i.e.
Copy code
acc.filter { it <= num }
+ listOf(num)
+ acc.filter { it > num }
is equivalent to
Copy code
acc.filter { it <= num }
listOf(num).unaryPlus()
acc.filter { it > num }.unaryPlus()
🤯 1
👍 1