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 / 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 / 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 / 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 / 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 / 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 / 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.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 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 / gradle.groovy
Last active December 16, 2016 17:29
Living (Android) without Kotlin #5
defaultConfig {
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
@sliskiCode
sliskiCode / Person.java
Last active December 19, 2016 14:30
Living (Android) without Kotlin #6
import lombok.Data;
import com.annimon.stream.Stream;
void foo(List<Person> persons) {
Stream.of(persons)
.filter(it -> it.getAge() >= 21)
.filter(it -> it.getName().startsWith("P"))
.map(Person::getName)
.sorted()
.forEach(System.out::println);