Kotlin Implementation for Kotlin-to-Clojure Conversion
Here's a Kotlin implementation that converts Kotlin collections to Clojure data structures, similar to the Clojure function you provided:
import clojure.lang.Keyword
import clojure.lang.PersistentArrayMap
import clojure.lang.PersistentVector
import clojure.lang.PersistentList
import clojure.lang.PersistentHashSet
import java.util.Date
import java.util.ArrayList
import kotlin.collections.Map.Entry
object KotlinToClojure {
@JvmStatic
fun massageInput(x: Any?, keywordizeKeys: Boolean = true): Any? {
return when (x) {
null -> null
is List<*> -> convertList(x, keywordizeKeys)
is Set<*> -> convertSet(x, keywordizeKeys)
is Map<*, *> -> convertMap(x, keywordizeKeys)
is Array<*> -> convertArray(x, keywordizeKeys)
is Date -> formatTimestamp(x.time)
is Collection<*> -> convertCollection(x, keywordizeKeys)
else -> x
}
}
private fun convertList(list: List<*>, keywordizeKeys: Boolean): PersistentVector {
val result = ArrayList<Any?>()
for (item in list) {
result.add(massageInput(item, keywordizeKeys))
}
return PersistentVector.create(result)
}
private fun convertSet(set: Set<*>, keywordizeKeys: Boolean): PersistentHashSet {
val result = ArrayList<Any?>()
for (item in set) {
result.add(massageInput(item, keywordizeKeys))
}
return PersistentHashSet.create(result)
}
private fun convertArray(array: Array<*>, keywordizeKeys: Boolean): PersistentVector {
val result = ArrayList<Any?>()
for (item in array) {
result.add(massageInput(item, keywordizeKeys))
}
return PersistentVector.create(result)
}
private fun convertCollection(collection: Collection<*>, keywordizeKeys: Boolean): PersistentVector {
val result = ArrayList<Any?>()
for (item in collection) {
result.add(massageInput(item, keywordizeKeys))
}
return PersistentVector.create(result)
}
private fun convertMap(map: Map<*, *>, keywordizeKeys: Boolean): PersistentArrayMap {
val result = ArrayList<Any?>()
for ((key, value) in map) {
val processedKey = when (key) {
is String -> if (keywordizeKeys) Keyword.intern(key) else key
else -> massageInput(key, keywordizeKeys)
}
result.add(processedKey)
result.add(massageInput(value, keywordizeKeys))
}
return PersistentArrayMap.create(result)
}
private fun formatTimestamp(time: Long): String {
// Implement your timestamp formatting logic here
return Date(time).toString() // Simple example - replace with ISO-8601 formatting
}
}
Usage Examples
// Convert Kotlin collections to Clojure data structures
val kotlinMap = mapOf("name" to "John", "age" to 30)
val clojureMap = KotlinToClojure.massageInput(kotlinMap)
val kotlinList = listOf(1, 2, 3, mapOf("a" to 1, "b" to 2))
val clojureVector = KotlinToClojure.massageInput(kotlinList)
val kotlinSet = setOf("x", "y", "z")
val clojureSet = KotlinToClojure.massageInput(kotlinSet)
Key Features
-
Type Conversion:
- Kotlin
List → Clojure PersistentVector
- Kotlin
Set → Clojure PersistentHashSet
- Kotlin
Map → Clojure PersistentArrayMap
- Kotlin
Array → Clojure PersistentVector
-
Recursive Processing:
- Nested collections are processed recursively
- Handles mixed types within collections
-
Keywordization:
- String map keys can be converted to Clojure keywords
- Optional via
keywordizeKeys parameter (defaults to true)
-
Special Types:
Date objects are converted to formatted strings
- Can be extended for other special types
Note: You'll need the Clojure JAR as a dependency in your Kotlin project to use the Clojure data structures directly. If you want to work with Clojure data structures in Kotlin, you might need additional helper functions to make them more Kotlin-friendly.