kotlin-fun

所属分类:Kotlin编程
开发工具:Java
文件大小:74KB
下载次数:0
上传日期:2016-10-19 21:02:15
上 传 者sh-1993
说明:  玩Kotlin并实现有趣的东西
(Playing around with Kotlin and implementing interesting stuff)

文件列表:
build.gradle (817, 2016-10-20)
gradle (0, 2016-10-20)
gradle\wrapper (0, 2016-10-20)
gradle\wrapper\gradle-wrapper.jar (53636, 2016-10-20)
gradle\wrapper\gradle-wrapper.properties (230, 2016-10-20)
gradlew (4971, 2016-10-20)
gradlew.bat (2314, 2016-10-20)
settings.gradle (32, 2016-10-20)
src (0, 2016-10-20)
src\main (0, 2016-10-20)
src\main\java (0, 2016-10-20)
src\main\java\com (0, 2016-10-20)
src\main\java\com\github (0, 2016-10-20)
src\main\java\com\github\h0tk3y (0, 2016-10-20)
src\main\java\com\github\h0tk3y\kotlinFun (0, 2016-10-20)
src\main\java\com\github\h0tk3y\kotlinFun\util (0, 2016-10-20)
src\main\java\com\github\h0tk3y\kotlinFun\util\WeakIdentityHashMap.java (32591, 2016-10-20)
src\main\kotlin (0, 2016-10-20)
src\main\kotlin\com (0, 2016-10-20)
src\main\kotlin\com\github (0, 2016-10-20)
src\main\kotlin\com\github\h0tk3y (0, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun (0, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\ComparatorContext.kt (707, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\Events.kt (2139, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\FieldProperty.kt (4895, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\LexComparator.kt (1152, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\SelfReference.kt (835, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\SequenceChaining.kt (1163, 2016-10-20)
src\main\kotlin\com\github\h0tk3y\kotlinFun\VarWithObservableSetter.kt (1071, 2016-10-20)
src\test (0, 2016-10-20)
src\test\kotlin (0, 2016-10-20)
src\test\kotlin\com (0, 2016-10-20)
src\test\kotlin\com\github (0, 2016-10-20)
src\test\kotlin\com\github\h0tk3y (0, 2016-10-20)
src\test\kotlin\com\github\h0tk3y\kotlinFun (0, 2016-10-20)
src\test\kotlin\com\github\h0tk3y\kotlinFun\EventTest.kt (954, 2016-10-20)
src\test\kotlin\com\github\h0tk3y\kotlinFun\FieldPropertyTest.kt (2057, 2016-10-20)
... ...

# kotlin-fun You know, writing code in Kotlin is a lot of `fun`. Here you may find some useful stuff written while playing around. Contributions are welcome. ## Usage You can use the project as a Gradle, Maven or SBT dependency: [![](https://jitpack.io/v/h0tk3y/kotlin-fun.svg)](https://jitpack.io/#h0tk3y/kotlin-fun) ## Self-reference Kotlin has no ability to reference a variable which is not initialized inside its constructor. But sometimes it is necessary, e.g. for callbacks. Here is a workaround with the following usage: val c: MyClass = selfReference { MyClass(someParams) { println(self.someField); } } Here, `self` is a *magic* reference to a value which is not constructed yet. ## Sequence chaining * `modifyPrefix` takes the original sequence and applies an operator to its prefix, leaving the tail unchanged, if any; * `chain` transforms the sequence by chaining the results of the operators, each called on what's left of the sequence after the previous one; * `lazyChain` is the same but concatenation is done lazily and no sequence objects are created until their items item are requested; * operator for concatenating a sequence with lambda that provides a sequence. ### Examples val seq = (1..5).asSequence() val prefixModified = seq.modifyPrefix { drop(1).take(2).map { -it } } // -2, -3, 4, 5 val chained = seq.chain( { sequenceOf(-1, 0) } { take(2).map { it + 100 } } { map { it * 100 } } ) // -1, 0, 101, 102, 300, 400, 500 fun primes(): Sequence { fun primesFilter(from: Sequence): Sequence = from.iterator().let { val current = it.next() sequenceOf(current) + { primesFilter(it.asSequence().filter { it % current != 0 }) } } return primesFilter((2..Int.MAX_VALUE).asSequence()) } ## Field property delegate Provides property delegates which behave as if there was a backing field. Useful for extension properties. Use `[Synchronized][Nullable]FieldProperty` for properties with different nullability and thread-safety. Default initializer for nullable version produces `null`, for not-null -- throws `IllegalStateException`. var MyClass.tag: String by FieldProperty { it.name } fun main(args: Array) { val c = MyClass("some name") println(c.tag) // some name c.tag = "some tag" println(c.tag) // some tag } Can be used to create inner mapping distinct for instances of enclosing class: val enclosingMyClassSharedTag = FieldProperty { it.name.reversed() } class Enclosing { var MyClass.innerTag: String by FieldProperty { it.name } var MyClass.sharedTag: String by enclosingMyClassSharedTag // ... } ## Lexicographical comparator Given a `Comparator`, creates a `Comparator>`, that compares iterables lexicographically: val natural = naturalOrder() val comparator = natural.lexicographically() comparator.compare(listOf(1, 2, 3), listOf(1, 2, 3, 4)) // -1 comparator.compare(listOf(1, 2, 4), listOf(1, 2, 3, 4)) // 1 ## Comparator context Allows you to use comparison operators `<`, `>`, `<=`, `>=` when you have a comparator for the corresponding type. val compareBySizes = compareBy> { it.size } compareBySize.withOperators { println(listOf(1) < listOf(1, 2)) // true println(listOf(1, 2, 3) >= listOf(3, 2, 1)) // true } Cannot be used for `Comparator>` because of the operators being extension functions. ## Var with observable setter Provides a property delegate with Swift-style `didSet` and `willSet` observers, and a type-safe builder for it, that allows you to add any one of `didSet` and `willSet` or both: var x: Int by varWithObservableSetter(5).willSet { println("before") }.didSet { println("after") } var y: String by varWithObservableSetter("abc").didSet { println("after") } ## Events C#-like events with handlers subscription. Supports adding a fire-once handler. Handler functions can receive zero to three arguments. val e = event0() e.addHandler { println("triggered!") } e.trigger() // triggered! val tick = event2() val f = { code, tag -> print("$code $tag " } e.addHandler(f) e.handleOnce { code, tag -> println(code + tag.length) } e.trigger(1, "abc") // 1 abc 4 e.trigger(2, "def") // 2 def e.removeHandler(f) e.trigger(3, "xyz") // To avoid exposing `trigger()` function to 3rd party code, use `.asHandlersCollector()` private val e = event1() val event: HandlersCollector<(String) -> Unit> get() = e.asHandlersCollector()

近期下载者

相关文件


收藏者