Last active
January 6, 2023 10:50
-
-
Save ecoopnet/288d7284bc1c6dc3f6070201afd5e8ec to your computer and use it in GitHub Desktop.
Dumps collections as String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 任意のコレクション型に文字列を返す dump メソッドを追加する。 | |
private fun Any.dump(): String { | |
if (this is Map<*, *>) { | |
return dump() | |
} | |
if (this is List<*>) { | |
return dump() | |
} | |
if (this is Collection<*>) { | |
return dump() | |
} | |
return toString() | |
} | |
private fun <K,V> Map<K,V>.dump(): String { | |
return map { (k, v) -> | |
"$k:[${v?.dump()}]" | |
}.joinToString("\n") | |
} | |
private fun <V> Collection<V>.dump(): String { | |
return joinToString(",") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment