Skip to content

Instantly share code, notes, and snippets.

View sliskiCode's full-sized avatar
:octocat:
Work, work

Piotr Ślesarew sliskiCode

:octocat:
Work, work
View GitHub Profile
@sliskiCode
sliskiCode / Person.kt
Last active December 19, 2016 14:30
Living (Android) without Kotlin #4
fun foo(persons: MutableList<Person>) {
persons.filter { it.age >= 21 }
.filter { it.name.startsWith("P") }
.map { it.name }
.sorted()
.forEach(::println)
}
data class Person(val name: String, val age: Int)
@sliskiCode
sliskiCode / Person.java
Last active December 19, 2016 14:32
Living (Android) without Kotlin #3
import lombok.Data;
@Data class Person {
final String name;
}
@sliskiCode
sliskiCode / Person.kt
Last active December 25, 2016 17:09
Living (Android) without Kotlin #2
val (riddle): String = Person("Peter").component1()
println(riddle) // prints Peter
@sliskiCode
sliskiCode / Person.kt
Last active December 16, 2016 15:57
Living (Android) without Kotlin #1
data class Person(val name: String)
val (riddle) = Person("Peter")
println(riddle)
@sliskiCode
sliskiCode / Delegates.kt
Last active November 24, 2016 15:33
7 things you probably don’t know about Kotlin #10
var price: Double by Delegates.vetoable(0.0) { prop, old, new ->
validate(new)
}
fun validate(price: Double) : Boolean {
// Validation checks
}
@sliskiCode
sliskiCode / Compose.kt
Last active November 25, 2016 14:36
7 things you probably don’t know about Kotlin #9
val prices = listOf(21.8, 232.5, 231.3)
val taxedDiscountedRounded = compose(::taxed, ::discounted, ::rounded)
prices.map(taxedDiscountedRounded)
fun <A, B> compose(f: (A) -> A,
g: (A) -> A,
h: (A) -> B): (A) -> B = { x -> h(g(f(x))) }
fun taxed(value: Double): Double = value * 1.4
fun discounted(value: Double): Double = value * 0.9
@sliskiCode
sliskiCode / Compose.kt
Last active November 24, 2016 14:30
7 things you probably don’t know about Kotlin #8
val prices = listOf(21.8, 232.5, 231.3)
prices.map(::taxed)
.map(::discounted)
.map(::rounded)
fun taxed(value: Double): Double = value * 1.4
fun discounted(value: Double): Double = value * 0.9
fun rounded(value: Double): Double = Math.round(value).toDouble()
@sliskiCode
sliskiCode / Main.kt
Created November 23, 2016 15:00
7 things you probably don’t know about Kotlin #7
// Main.java
public static void main(String[] args) {
String name = null;
Nullcheck.ifNull(name, new Function1<Object, Unit>() {
@Override
public Unit invoke(Object o) {
return null;
}
});
@sliskiCode
sliskiCode / Main.kt
Last active November 23, 2016 14:39
7 things you probably don’t know about Kotlin #6
// Main.java
public static void main(String[] args) {
String name = null;
AnyKt.ifNull(name, new Function1<Object, Unit>() {
@Override
public Unit invoke(Object o) {
return null;
}
});
@sliskiCode
sliskiCode / CustomButton.kt
Last active December 5, 2016 08:58
5 things you probably don’t know about Kotlin #5
val button = CustomButton(context)
button.visibility = CustomButton.Visibility.VISIBLE
button.foregroundGravity = CustomButton.ForegroundGravity.LEFT
class CustomButton(context: Context?) : View(context) {
companion object Visibility {
val VISIBLE = 1
val INVISIBLE = 2
}