You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/04_functional/01_Higher-Order Functions.md
+12-14Lines changed: 12 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Higher-Order Functions
2
2
3
-
A higher-order function is a function that takes another function as parameter and/or returns a function.
3
+
A [*higher-order function*](https://kotlinlang.org/docs/reference/lambdas.html) is a function that takes another function as parameter and/or returns a function.
1. Declare a higher-order function that takes as parameter two integers, x and y. In addition it takes another function as parameter, named `operation`
26
-
that itself takes two parameters of type integer and returns an integer.
27
-
2. Invoke the function passing in the arguments supplied.
28
-
3. Declare a function that matches the same signature.
29
-
4. Invoke the higher-order function passing in as the function argument, `::sum` which is how we reference a function by name in Kotlin.
30
-
5. Invoke the higher-order function passing in as function argument a lambda function. Looks more clear, doesn't it?
31
-
32
-
### Returning a function as parameter
25
+
1. Declares a higher-order function. It takes two integer parameters, `x` and `y`. Additionally, it takes another function `operation` as a parameter. The `operation` parameters and return type are also defined in the declaration.
26
+
2. The higher order function returns the result of `operation` invocation with the supplied agruments.
27
+
3. Declares a function that matches the `operation`signature.
28
+
4. Invokes the higher-order function passing in two integer values and the function argument `::sum`. `::` is the notation that references a function by name in Kotlin.
29
+
5. Invokes the higher-order function passing in a lambda as a function argument. Looks clearer, doesn't it?
Copy file name to clipboardExpand all lines: examples/04_functional/02_Lambdas.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Lambda Functions
2
2
3
-
Lambda functions ("lambdas") are a simple way to create functions ad-hoc. Lambdas can be denoted very concisely in many cases thanks to type inference and the implicit `it` variable.
3
+
[*Lambda functions*](https://kotlinlang.org/docs/reference/lambdas.html) ("lambdas") are a simple way to create functions ad-hoc. Lambdas can be denoted very concisely in many cases thanks to type inference and the implicit `it` variable.
1. A lambda in all its glory, with explicit types everywhere. The lambda is the part in curly braces, which is assigned to a variable of type `(String) -> String` (a function type).
37
-
2. Type inference inside lambda: infers type of lambda parameter from type of the variable it's assigned to.
38
-
3. Type inference outside lambda: infers type of the variable based on the type of the lambda parameter and return value.
37
+
2. Type inference inside lambda: the type of the lambda parameter is inferred from the type of the variable it's assigned to.
38
+
3. Type inference outside lambda: the type of the variable is inferred from the type of the lambda parameter and return value.
39
39
4. You cannot do both together, the compiler has no chance to infer the type that way.
40
-
5. For lambdas with a single parameter, you don't have to explicitly name it. Instead, you can use the implicit `it` variable. This is especially useful whenever the type of `it` can be inferred (which is often the case).
41
-
6. If your lambda just consists of a single function call, you may prefer function pointers.
40
+
5. For lambdas with a single parameter, you don't have to explicitly name it. Instead, you can use the implicit `it` variable. This is especially useful when the type of `it` can be inferred (which is often the case).
41
+
6. If your lambda consists of a single function call, you may use function pointers (`::`) .
Copy file name to clipboardExpand all lines: examples/04_functional/03_extensionFunctions.md
+10-9Lines changed: 10 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Extension Functions and Properties
2
2
3
+
Kotlin lets you add new members to any class with the [extensions](https://kotlinlang.org/docs/reference/extensions.html) mechanism. Namely, there are two types of extensions: extension functions and extension properties. They look a lot like normal functions and properties but with one important difference: you need to specify the type that you extend.
1. We define simple models of `Item` and `Order`. `Order` can contain arbitrary `Collection` of `Item` s
30
-
2. We define extension functions for `Order` type. Later we call these functions directly on the instance of that type.
31
-
3. We can also define extension properties.
32
-
33
-
Read more about signature of [extensions](https://kotlinlang.org/docs/reference/extensions.html). Signature
34
-
is very much like standard function or property signature, with addition that we need to specify type we're attaching function or property to.
31
+
1. Defines simple models of `Item` and `Order`. `Order` can contain a collection of `Item` objects.
32
+
2. Adds extension functions for the `Order` type.
33
+
3. Adds an extension property for the `Order` type.
34
+
4. Calls extension functions directly on an instance of `Order`.
35
+
5. Accesses the extension property on an instance of `Order`.
35
36
36
-
It is even possible to execute extensions on `null` references. In their implementation we can check for `null`reference and based on that we can do any arbitrary logic. Example:
37
+
It is even possible to execute extensions on `null` references. In an extension function, you can check the object for `null` and use the result in your code:
0 commit comments