- , ,
. lock()
,
, , lock
:
fun <T> lock(lock: Lock, body: () -> T): T{
lock.lock()
try{
return body()
}
finally {
lock.unlock()
}
}
. body
: () -> T
, ,
, T
.
try
, lock
, lock()
.
lock(), ( function references
fun toBeSynchronized() = sharedResource.operation()
val result = lock (lock, ::toBeSynchronized)
, -:
val result = lock(lock, { sharedResource.operation() })
->
( )->
Kotlin , , , - , :
lock (lock) {
sharedResource.operation()
}
map()
:
fun <T, R> List<T>.map(transform: (T) -> R): List<R> {
val result = arrayListOf<R>()
for (item in this)
result.add(transform(item))
return result
}
:
val doubled = ints.map { it -> it * 2 }
, , .
it
: ,
( ->
). it
:
ints.map { it * 2 }
LINQ :
strings.filter { it.lenght == 5 }.sortBy { it }.map { it.toUpperCase() }
- " "(. "functional literal"), , , . :
max(strings, { a, b -> a.length < b.length })
max
- , . , , functional literal.
fun compare(a: String, b: String): Boolean = a.length < b.length
, , ( ) . , max
:
fun <T> max(collection: Collection<T>, less: (T, T) -> Boolean): T? {
var max: T? = null
for (it in collection)
if (max == null || less(max, it))
max = it
return max
}
'less' (T, T) -> Boolean
, , T
'Boolean':'true', , .
, 4, less
: T
.
, , , .
val compare: (x: T, y: T) -> Int = ...
-, literals of function types, :
val sum = { x: Int, y: Int -> x + y }
- {...}
, (), ->
. Unit
, ( ) .
, , , :
val sum: (Int, Int) -> Int = { x, y -> x + y }
, - . Kotlin , , it
:
ints.filter { it > 0 } // '(it: Int) -> Boolean'
, qualified return .
ints.filter {
val shouldFilter = it > 0
shouldFilter
}
ints.filter {
val shouldFilter = it > 0
return@filter shouldFilter
}
, , - , . . callSuffix
-, , . , , . , , :
fun(x: Int, y: Int): Int = x + y
, , . ( ), :
fun(x: Int, y: Int): Int {
return x + y
}
, . , , .
ints.filter(fun(item) = item > 0)
: - ( Unit
) , .
, {...}
. , , -.
- return
(non-local returns. return
, (@
), , fun
. , return
- , -. , return
, , ,, .
- (, object expression) , , . Java, , :
var sum = 0
ints.filter { it > 0 }.forEach {
sum += it
}
print(sum)
Kotlin -. - . ()[extensions.html], - . - Type-safe Groovy-style builders.
sum : Int.(other: Int) -> Int
, , -:
1.sum(2)
. , .
val sum = fun Int.(other: Int): Int = this + other
- , .
class HTML {
fun body() { ... }
}
fun html(init: HTML.() -> Unit): HTML {
val html = HTML() // -
html.init() //
return html
}
html { //
body() // -
}
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |