Revert "Remove toPairs helper in favor of stdlib functionality"

This reverts commit 60797c21d0.
This commit is contained in:
kageru 2019-08-05 21:39:00 +02:00
parent 0ae5e83641
commit e3219c7800
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 13 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package moe.kageru.kagebot
import moe.kageru.kagebot.Util.failed
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.Util.toPairs
import org.javacord.api.entity.message.Message
import org.javacord.api.entity.message.MessageAuthor
import org.javacord.api.entity.message.Messageable
@ -54,7 +55,7 @@ object MessageUtil {
throw IllegalStateException("Embed must have even number of content strings (title/content pairs)")
}
return withEmbed {
contents.zipWithNext().forEach { (heading, content) ->
contents.toPairs().forEach { (heading, content) ->
addField(heading, content)
}
}

View File

@ -135,4 +135,15 @@ object Util {
Config.server.getMemberById(id).orElse(null)
}
}
/**
* Convert a list of elements to pairs, retaining order.
* The last element is dropped if the input size is odd.
* [1, 2, 3, 4, 5] -> [[1, 2], [3, 4]]
*/
fun <T> Collection<T>.toPairs(): List<Pair<T, T>> = this.iterator().run {
(0 until size / 2).map {
Pair(next(), next())
}
}
}