Skip to content

Commit ce9e9dc

Browse files
Merge pull request Kotlin#47 from p7nov/functional-revised
docs: revised Functional
2 parents 246290b + 7033243 commit ce9e9dc

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

examples/04_functional/01_Higher-Order Functions.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Higher-Order Functions
22

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.
44

5-
### Taking a function as parameter
5+
### Taking Functions as Parameters
66

77
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
88

@@ -22,15 +22,13 @@ fun main() {
2222

2323
</div>
2424

25-
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?
3330

31+
### Returning Functions
3432

3533
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
3634

@@ -49,8 +47,8 @@ fun main() {
4947

5048
</div>
5149

52-
1. Declare a higher-order function that returns a function.
53-
2. Return a function matching the signature.
54-
3. Invoke `operation` to get the result assigned to a variable.
55-
4. Invoke the function.
50+
1. Declares a higher-order function that returns a function.
51+
2. Declares a function matching the signature.
52+
3. Invokes `operation` to get the result assigned to a variable. Here `func` becomes `square` which is returned by `operation`.
53+
4. Invokes `func`. The `square` function is actually executed.
5654

examples/04_functional/02_Lambdas.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lambda Functions
22

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.
44

55
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
66

@@ -34,8 +34,8 @@ fun main() {
3434
</div>
3535

3636
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.
3939
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 (`::`) .

examples/04_functional/03_extensionFunctions.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Extension Functions and Properties
22

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.
4+
35
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
46

57
```kotlin
@@ -17,23 +19,22 @@ fun main() {
1719

1820
val order = Order(listOf(Item("Bread", 25.0F), Item("Wine", 29.0F), Item("Water", 12.0F)))
1921

20-
println("Max priced item name: ${order.maxPricedItemName()}")
22+
println("Max priced item name: ${order.maxPricedItemName()}") // 4
2123
println("Max priced item value: ${order.maxPricedItemValue()}")
22-
println("Items: ${order.commaDelimitedItemNames}")
24+
println("Items: ${order.commaDelimitedItemNames}") // 5
2325

2426
}
2527
```
2628

2729
</div>
2830

29-
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`.
3536

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:
3738

3839
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
3940

0 commit comments

Comments
 (0)