Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Commit 59efc00

Browse files
committed
code cosmetics
1 parent c249359 commit 59efc00

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/main/kotlin/krangl/Builder.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fun <T> DataFrame.Companion.fromRecords(records: Iterable<T>, mapping: (T) -> Da
3939

4040
val columnData = columnNames.map { it to emptyList<Any?>().toMutableList() }.toMap()
4141

42-
for (record in rowData) {
42+
for(record in rowData) {
4343
columnData.forEach { colName, colData -> colData.add(record[colName]) }
4444
}
4545

@@ -77,25 +77,24 @@ inline fun <reified T> Iterable<T>.asDataFrame(): DataFrame {
7777
}
7878

7979

80-
8180
//typealias PropExtractor<T> = T.(T) -> Any?
8281
typealias PropExtractor<T> = T.() -> Any?
8382

8483

8584
fun main() {
86-
data class Car(val name:String, val weight: Double)
85+
data class Car(val name: String, val weight: Double)
8786

8887
val cars = listOf(Car("BMW", 123.0), Car("Tesla", 245.0))
8988
val df = dataFrameOf(AnyCol("car", cars))
90-
df.unfold("car", listOf(Car::name,Car::weight, Car::weight))
89+
df.unfold("car", listOf(Car::name, Car::weight, Car::weight))
9190
}
9291

9392
@JvmName("unfoldByProperty")
94-
fun DataFrame.unfold(
93+
fun DataFrame.unfold(
9594
columnName: String,
9695
properties: List<KCallable<*>>,
9796
keep: Boolean = true,
98-
addPrefix:Boolean=false
97+
addPrefix: Boolean = false
9998
): DataFrame {
10099

101100
// todo make sure that unfolded columns are not yet present in df, and warn if so and append _1, _2, ... suffix
@@ -109,23 +108,25 @@ fun DataFrame.unfold(
109108
}
110109
}
111110

112-
return if (keep) unfolded else unfolded.remove(columnName)
111+
return if(keep) unfolded else unfolded.remove(columnName)
113112
}
114113

115114

116115
inline fun <reified T> DataFrame.unfold(
117116
columnName: String,
118117
properties: List<String> = detectPropertiesByReflection<T>().map { it.name },
119118
keep: Boolean = true,
120-
addPrefix:Boolean=false
119+
addPrefix: Boolean = false
121120
): DataFrame {
122121

123122
val extProperties = properties + properties.map { "get" + it.capitalize() }
124123
val propsOrGetters = detectPropertiesByReflection<T>()
125124

126125
val filtMembers = propsOrGetters
127126
// match by name
128-
.filter { it.parameters.size==1 && extProperties.contains(it.name) } // discard extension functions in class body
127+
.filter { it.parameters.size == 1 }
128+
// discard extension functions in class body
129+
.filter { extProperties.contains(it.name) }
129130

130131
// todo make sure that unfolded columns are not yet present in df, and warn if so and append _1, _2, ... suffix
131132
val unfolded = filtMembers.fold(this) { df, kCallable ->
@@ -138,7 +139,7 @@ inline fun <reified T> DataFrame.unfold(
138139
}
139140
}
140141

141-
return if (keep) unfolded else unfolded.remove(columnName)
142+
return if(keep) unfolded else unfolded.remove(columnName)
142143
}
143144

144145

@@ -167,7 +168,7 @@ inline fun <reified T> DataFrame.rowsAs(mapping: Map<String, String> = names.map
167168
// select the one with most parameters
168169
.maxByOrNull { it.parameters.size }
169170

170-
if (bestConst == null) error("[krangl] Could not find matching constructor for subset of ${mapping.values}")
171+
if(bestConst == null) error("[krangl] Could not find matching constructor for subset of ${mapping.values}")
171172

172173
val objects = rows.map { row ->
173174
val args = bestConst.parameters.map { constParamName ->
@@ -176,7 +177,7 @@ inline fun <reified T> DataFrame.rowsAs(mapping: Map<String, String> = names.map
176177

177178
try {
178179
bestConst.call(*args.toTypedArray())
179-
} catch (e: IllegalArgumentException) {
180+
} catch(e: IllegalArgumentException) {
180181
throw IllegalArgumentException("Could not instantiate record class constructor $bestConst with $args")
181182
}
182183
}
@@ -273,8 +274,9 @@ class InplaceDataFrameBuilder(private val header: List<String>) {
273274
}
274275

275276
// https://github.com/holgerbrandl/krangl/issues/125
276-
require(rawColumns.isNotEmpty()) { "Can not infer column types in empty data-frame. " +
277-
"To create an empty data-frame use the following syntax dataFrameOf(StringCol(\"user\", emptyArray()), DoubleCol(\"salary\", emptyArray()))"
277+
require(rawColumns.isNotEmpty()) {
278+
"Can not infer column types in empty data-frame. " +
279+
"To create an empty data-frame use the following syntax dataFrameOf(StringCol(\"user\", emptyArray()), DoubleCol(\"salary\", emptyArray()))"
278280
}
279281

280282
// require(rawColumns.isEmpty() || tableColumns.map { it.length }.distinct().size == 1) {
@@ -306,10 +308,10 @@ fun DataFrame.Companion.fromResultSet(rs: ResultSet): DataFrame {
306308

307309
// http://www.cs.toronto.edu/~nn/csc309/guide/pointbase/docs/html/htmlfiles/dev_datatypesandconversionsFIN.html
308310
colTypes.map {
309-
when (it) {
311+
when(it) {
310312
Types.INTEGER, Types.SMALLINT -> listOf<Int>()
311313
Types.BIGINT -> listOf<Long>()
312-
Types.DECIMAL, Types.FLOAT, Types.NUMERIC,Types.REAL,Types.DOUBLE -> listOf<Double?>()
314+
Types.DECIMAL, Types.FLOAT, Types.NUMERIC, Types.REAL, Types.DOUBLE -> listOf<Double?>()
313315
Types.BOOLEAN -> listOf<Boolean?>()
314316
Types.DATE, Types.TIMESTAMP -> listOf<LocalDate?>()
315317
Types.TIME -> listOf<LocalTime?>()
@@ -319,15 +321,15 @@ fun DataFrame.Companion.fromResultSet(rs: ResultSet): DataFrame {
319321
}
320322

321323
// see https://stackoverflow.com/questions/21956042/mapping-a-jdbc-resultset-to-an-object
322-
while (rs.next()) {
324+
while(rs.next()) {
323325
// val row = mapOf<String, Any?>().toMutableMap()
324-
for (colIndex in 1..numColumns) {
325-
val any: Any? = when (colTypes[colIndex - 1]) {
326+
for(colIndex in 1..numColumns) {
327+
val any: Any? = when(colTypes[colIndex - 1]) {
326328
Types.INTEGER, Types.SMALLINT -> rs.getInt(colIndex)
327-
Types.BIGINT -> rs.getLong(colIndex)
328-
Types.DECIMAL, Types.FLOAT, Types.NUMERIC,Types.REAL,Types.DOUBLE -> rs.getDouble(colIndex)
329+
Types.BIGINT -> rs.getLong(colIndex)
330+
Types.DECIMAL, Types.FLOAT, Types.NUMERIC, Types.REAL, Types.DOUBLE -> rs.getDouble(colIndex)
329331
Types.BOOLEAN -> rs.getBoolean(colIndex)
330-
Types.DATE, -> rs.getDate(colIndex).toLocalDate()
332+
Types.DATE -> rs.getDate(colIndex).toLocalDate()
331333
Types.TIMESTAMP -> rs.getTimestamp(colIndex).toLocalDateTime()
332334
Types.TIME -> rs.getTime(colIndex).toLocalTime()
333335
Types.CHAR, Types.LONGVARCHAR, Types.VARCHAR, Types.NVARCHAR -> rs.getString(colIndex)

0 commit comments

Comments
 (0)