@@ -20,6 +20,8 @@ fun main() {
20
20
21
21
/* *
22
22
* FUNCTIONS
23
+ * Functions in kotlin are 'first class objects', ie the can be assigned to variables, passed as params
24
+ * to other functions, and returned as a result from higher order functions.
23
25
*/
24
26
fun functions () {
25
27
println (" \n >>>>> Some functions" )
@@ -34,6 +36,7 @@ fun functions() {
34
36
14
35
37
)// note four is using default, declard in order without names
36
38
paramsWithDefaultValues(two = false ) // named params do not have to be in order
39
+ doSomethngWithLambdas()
37
40
}
38
41
39
42
// functions are first class and do not need to be part of a class
@@ -50,15 +53,35 @@ fun addAndPrint(a: Int, b: Int) = println(" fun with expression body: printing
50
53
51
54
52
55
fun addAndPrintBody (a : Int , b : Int ) {
53
- println (" fun with block body: printing from a body with no return: ${a + b} " )
56
+ println (" \n fun with block body: printing from a body with no return: ${a + b} " )
54
57
return Unit // not required, can be ignored
55
58
}
56
59
57
60
fun paramsWithDefaultValues (one : String = "blah", two : Boolean = false, three : Int = 0,
58
61
fourNullable : String? = null){ // one, two, three are not nullable. four is
59
- println (" fun params with default values: one: $one two:$two three: $three , fourNullable: $fourNullable " )
62
+ println (" \n fun params with default values: one: $one two:$two three: $three , fourNullable: $fourNullable " )
60
63
}
61
64
65
+ /* *
66
+ * Lambdas
67
+ * Similar to the way java does them for the most part
68
+ */
69
+ // Lambda Expressions
70
+ // return type is inferred, ie we don't explicitly say 'Int'
71
+ val sumTypeInferred = {x: Int , y: Int -> x + y}
72
+ val aResult = sumTypeInferred(3 , 4 )
73
+
74
+ // Explicit type declaration
75
+ // Format: (input params) -> return type = implementation
76
+ val printNumber : (Int ) -> Unit = {num -> println (" explioct type declared for lambda num: $num " )}
77
+ // the following doesn't work even tho I found lots of examples with it
78
+ // val printNumberErrors : Int -> Unit = {num -> println(" explioct type declared for lambda num: $num")}
79
+
80
+ fun doSomethngWithLambdas () {
81
+ println (" \n >>>>> do a couple of lambda things" )
82
+ println (" invoke a lambdaExpression 3 + 4 = ${sumTypeInferred(3 , 4 )} " )// need ${} since params, even tho it is a 'property'
83
+ printNumber(14 )
84
+ }
62
85
/* *
63
86
* VARIABLES
64
87
*/
0 commit comments