I'm working with this higher-order compose function:

```
fun <T, U, V> higherCompose(): ((U) -> V) -> ((T) -> U) -> (T) -> V =
{ f ->
{ g ->
{ x -> f(g(x)) }
}
}
```

And the application:

```
val cos = higherCompose<Double, Double, Double>()()
{ x: Double -> Math.sin(x)}() { x : Double -> Math.PI/2 - x }
```

I do understand what we're trying to achieve here mathematically. But what I'm struggling with is notation and the programming meaning:

What exactly is

```
higherCompose<Double>()
```

And why do we apply so strangely values to it:

```
1. () ()
2. { function1} { function 1}
```

Here's how this notation is parsed and what the function calls are:

This is the initial no-argument call to

`higherCompose`

that returns a value of exactly the`higherCompose`

's return type,`((U) -> V) -> ((T) -> U) -> (T) -> V`

, with the three type parameters substituted with`Double`

.These parentheses are simply used to resolve parsing ambiguity. Kotlin allows calling a function with a lambda in either of the forms:

`f({ })`

,`f() { }`

,`f { }`

. If you remove the parentheses, the lambda marked as (3) will be treated as an argument to the first call. One way to solve this ambiguity is to add`()`

to tell the compiler that it's the (1)'s return value that is invoked and not`higherCompose`

Therefore, this lambda is passed as the

`((U) -> V)`

argument to the`higherCompose`

's result type, producing`((T) -> U) -> (T) -> V`

.These parentheses are added because Kotlin does not allow calling a return value of a call with a lambda outside parentheses (

`f() { }`

) directly with another lambda:`f() { } { }`

is prohibited, but adding another pair of parentheses between the lambdas solves this:`f() { }() { }`

means an invocation of the`f`

's result with a lambda.This lambda is passed as the

`((T) -> U)`

argument to the (3)'s result type, and the result of this call is`(T) -> V`

You can get a much less cryptic version of this expression if you pass all the lambdas inside parentheses (the

`f({ })`

form instead of`f() { }`

or`f { }`

):See: Lambda Expressions and Anonymous Functions in the language reference.