This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var price: Double by Delegates.vetoable(0.0) { prop, old, new -> | |
| validate(new) | |
| } | |
| fun validate(price: Double) : Boolean { | |
| // Validation checks | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class Person(val name: String) | |
| val (riddle) = Person("Peter") | |
| println(riddle) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val (riddle): String = Person("Peter").component1() | |
| println(riddle) // prints Peter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import lombok.Data; | |
| @Data class Person { | |
| final String name; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defaultConfig { | |
| jackOptions { | |
| enabled true | |
| } | |
| } | |
| compileOptions { | |
| sourceCompatibility JavaVersion.VERSION_1_8 | |
| targetCompatibility JavaVersion.VERSION_1_8 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |