@@ -5,9 +5,26 @@ import java.sql.ResultSet
55import kotlin.random.Random
66
77
8+ /* *
9+ * Computes the sum of two numbers and returns the result as a Double.
10+ * This function is generic and can accept arguments of any type
11+ * that inherits from Number.
12+ *
13+ * @param a The first number to add; must be of type Number.
14+ * @param b The second number to add; must be of type Number.
15+ * @return The sum of 'a' and 'b' as a Double.
16+ */
817fun <T : Number > aPlusB (a : T , b : T ): Double = a.toDouble() + b.toDouble()
918
1019
20+ /* *
21+ * Executes a given SQL query on a provided database connection and returns the result as a list of rows,
22+ * where each row is a list containing the column values for that row.
23+ *
24+ * @param db The database connection to be used for executing the query.
25+ * @param query The SQL query string to be executed.
26+ * @return A list of lists, where each inner list represents a row retrieved from the query execution.
27+ */
1128fun sqlite (db : Connection , query : String ): List <List <Any ?>> {
1229 db.createStatement().use { statement ->
1330 statement.executeQuery(query).use { resultSet ->
@@ -27,6 +44,19 @@ fun sqlite(db: Connection, query: String): List<List<Any?>> {
2744}
2845
2946
47+ /* *
48+ * Compares two items based on a derived key that is comparable.
49+ * The function takes a key-mapping function and two items of the same type,
50+ * applies the key-mapping function to both items, and returns an integer based on their comparison.
51+ * If the key for the first item is less than the second, it returns -1.
52+ * If the key for the first item is greater than the second, it returns 1.
53+ * If both keys are equal, it returns 0.
54+ *
55+ * @param keyMap A function that takes an item of type T and returns a comparable key of type R.
56+ * @param item1 The first item to be compared.
57+ * @param item2 The second item to be compared.
58+ * @return An integer indicating the order of the items; -1 if item1 < item2, 1 if item1 > item2, and 0 if they are equal.
59+ */
3060fun <T , R : Comparable <R >> compare (keyMap : (T ) -> R , item1 : T , item2 : T ): Int {
3161 return when {
3262 keyMap(item1) < keyMap(item2) -> - 1
@@ -36,6 +66,13 @@ fun <T, R : Comparable<R>> compare(keyMap: (T) -> R, item1: T, item2: T): Int {
3666}
3767
3868
69+ /* *
70+ * Generates a random string of alphabets with the specified length.
71+ * The string includes both uppercase and lowercase alphabets.
72+ *
73+ * @param length The desired length of the generated string.
74+ * @return A string consisting of random alphabets of the specified length.
75+ */
3976fun randomAlphabets (length : Int ): String {
4077 val charPool = (' a' .. ' z' ) + (' A' .. ' Z' )
4178 return (1 .. length)
0 commit comments