Alan Evans
01/13/2019, 9:54 PMfun List<Int>.maxSubListSum(n: Int) =
take(n)
.sum()
.let { sum ->
drop(n)
.foldIndexed(sum to sum) { i, (sum, max), next ->
(sum + next - this[i]).let {
it to if (it > max) it else max
}
}.second
}
(Sorry for no thread).Alan Evans
01/13/2019, 10:22 PMfun List<Int>.maxSubListSum(n: Int) =
foldIndexed(0 to 0) { i, (sum, max), next ->
(sum + next - (getOrNull(i - n) ?: 0)).let {
it to if (it > max) it else max
}
}.second
Alan Evans
01/13/2019, 10:51 PMfun Iterable<Int>.maxSubListSum(n: Int) = asSequence().maxSubListSum(n)
fun Sequence<Int>.maxSubListSum(n: Int) =
zip(IntArray(n).asSequence() + this)
.fold(0 to 0) { (sum, max), (next, prev) ->
(sum + next - prev).let {
it to if (it > max) it else max
}
}.second
Dico
01/13/2019, 11:15 PMDico
01/13/2019, 11:16 PMDico
01/13/2019, 11:16 PMDico
01/13/2019, 11:17 PMlouiscad
01/13/2019, 11:18 PMAlan Evans
01/13/2019, 11:26 PMDico
01/13/2019, 11:30 PMDico
01/13/2019, 11:30 PMAlan Evans
01/13/2019, 11:31 PMit
and maybe using `max()`:
(sum + next - prev).let { newSum ->
newSum to max(max, newSum)
}
But the most confusing thing in all these is the fold
syntax and that's not my fault.Alan Evans
01/13/2019, 11:34 PMfold
and that's plain hard to read. I tried to give good names to its arguments, but there's not a lot more you can do.Alan Evans
01/13/2019, 11:49 PM.let{ newSum ->
but it's subjective:
fun List<Int>.maxSubListSum(n: Int) =
foldIndexed(0 to 0) { i, (sum, max), next ->
val newSum = sum + next - (getOrNull(i - n) ?: 0)
newSum to max(max, newSum)
}.second
Less nesting, bit cleaner. Comment could help, but I'm allergic to them.louiscad
01/14/2019, 4:09 AM"Explicit return type would make the code more readable."Maybe, and or just turn on hints in your IDE. I'm on Slack, not in an IDE, and indentation is not the best in your snippet IMHO, hence my comment.
louiscad
01/14/2019, 4:10 AMigor.wojda
01/14/2019, 7:08 AMAlan Evans
01/14/2019, 11:32 AMmaxSubListSum
tells you that the result is a Sum
of part of the list of Int
.
Suppose I had maxSubListSum(n: Int): Int
and maxSubListProduct(n: Int): Int
. Both return Int
, but only the function name tells you what the function returns, the rest is just noise, only relevant if you were actually using the function, in which case you'd be in an IDE, not slack.Dico
01/14/2019, 11:38 AMList<Int>
should actually be represented as a Long
to be stable, so I get that there is a clue, it doesn't mean that we don't need the IDE to tell us the actual type.
In my opinion, when an expression covers 4-5 lines, it's generally too long and the programmer probably could've spent less time writing it as statements.louiscad
01/14/2019, 11:39 AMNumber
or something else despite the name, and a PR reviewer would not know if not reviewing in an IDE, like most of the time.Dico
01/14/2019, 11:42 AMAlan Evans
01/14/2019, 11:49 AMList<Int>
should actually be represented as a Long
to be stable", that would be unexpected as the standard functions don't do that public fun Iterable<Int>.sum(): Int
"when an expression covers 4-5 lines, it's generally too long and the programmer probably could've spent less time writing it as statements.", not much less as I did write as statements, and then I inlined it!
If I were working with you, I'd be applying the style the team decides.Dico
01/14/2019, 11:51 AMlong
. I agree that the style decided on by the team takes priority :)Dico
01/14/2019, 11:53 AMDico
01/14/2019, 11:54 AMAlan Evans
01/14/2019, 11:59 AM