@@ -5,9 +5,27 @@ import java.sql.ResultSet
55import kotlin.random.Random
66
77
8+ /* *
9+ * Adds two numbers of type `Number` and returns their sum as a `Double`.
10+ * This function is generic and works with any subtype of `Number`,
11+ * allowing for operations on integers, floats, and other number types.
12+ *
13+ * @param a First number to be added, of type `T`, which is a subtype of `Number`.
14+ * @param b Second number to be added, of type `T`, which is a subtype of `Number`.
15+ * @return The sum of `a` and `b` converted to a `Double`.
16+ */
17+
818fun <T : Number > aPlusB (a : T , b : T ): Double = a.toDouble() + b.toDouble()
919
1020
21+ /* *
22+ * Executes a SQL query on the provided database connection and returns the results as a list of rows,
23+ * where each row is represented as a list of column values.
24+ *
25+ * @param db The database connection on which the SQL query will be executed.
26+ * @param query The SQL query string to be executed.
27+ * @return A list of rows, where each row is a list containing the column values from the query's result set.
28+ */
1129fun sqlite (db : Connection , query : String ): List <List <Any ?>> {
1230 db.createStatement().use { statement ->
1331 statement.executeQuery(query).use { resultSet ->
@@ -27,6 +45,16 @@ fun sqlite(db: Connection, query: String): List<List<Any?>> {
2745}
2846
2947
48+ /* *
49+ * Compares two items using a specified key mapping function and returns an integer
50+ * indicating their relative order based on the mapped keys.
51+ *
52+ * @param keyMap A function that maps an item of type T to a key of type R, which is comparable.
53+ * @param item1 The first item to be compared.
54+ * @param item2 The second item to be compared.
55+ * @return An integer: -1 if the key of item1 is less than the key of item2, 1 if the key of item1
56+ * is greater than the key of item2, or 0 if they are equal.
57+ */
3058fun <T , R : Comparable <R >> compare (keyMap : (T ) -> R , item1 : T , item2 : T ): Int {
3159 return when {
3260 keyMap(item1) < keyMap(item2) -> - 1
@@ -36,6 +64,13 @@ fun <T, R : Comparable<R>> compare(keyMap: (T) -> R, item1: T, item2: T): Int {
3664}
3765
3866
67+ /* *
68+ * Generates a random string of alphabets with the specified length. The characters in the string
69+ * are randomly chosen from both lowercase and uppercase English alphabets.
70+ *
71+ * @param length The length of the random string to be generated.
72+ * @return A string consisting of randomly selected alphabets of the given length.
73+ */
3974fun randomAlphabets (length : Int ): String {
4075 val charPool = (' a' .. ' z' ) + (' A' .. ' Z' )
4176 return (1 .. length)
0 commit comments