@@ -5,9 +5,27 @@ import java.sql.ResultSet
5
5
import kotlin.random.Random
6
6
7
7
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
+
8
18
fun <T : Number > aPlusB (a : T , b : T ): Double = a.toDouble() + b.toDouble()
9
19
10
20
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
+ */
11
29
fun sqlite (db : Connection , query : String ): List <List <Any ?>> {
12
30
db.createStatement().use { statement ->
13
31
statement.executeQuery(query).use { resultSet ->
@@ -27,6 +45,16 @@ fun sqlite(db: Connection, query: String): List<List<Any?>> {
27
45
}
28
46
29
47
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
+ */
30
58
fun <T , R : Comparable <R >> compare (keyMap : (T ) -> R , item1 : T , item2 : T ): Int {
31
59
return when {
32
60
keyMap(item1) < keyMap(item2) -> - 1
@@ -36,6 +64,13 @@ fun <T, R : Comparable<R>> compare(keyMap: (T) -> R, item1: T, item2: T): Int {
36
64
}
37
65
38
66
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
+ */
39
74
fun randomAlphabets (length : Int ): String {
40
75
val charPool = (' a' .. ' z' ) + (' A' .. ' Z' )
41
76
return (1 .. length)
0 commit comments