Skip to content

Instantly share code, notes, and snippets.

View adam-arold's full-sized avatar
💭
Exploring the n'th dimension.

Adam Arold adam-arold

💭
Exploring the n'th dimension.
View GitHub Profile
@adam-arold
adam-arold / KotlinUserWithNulls.kt
Created September 11, 2017 16:31
Kotlin user with nulls
data class KotlinUserWithNulls(val firstName: String?,
// String? means that it is either a String object or a null
val lastName: String?,
val addresses: List<Address> = listOf()) {
data class Address(val city: String?)
companion object {
fun fetchFirstCity(user: KotlinUserWithNulls?): String? {
user?.addresses?.forEach { it.city?.let { return it } }
@adam-arold
adam-arold / KotlinUserWithoutNulls.kt
Created September 11, 2017 16:32
Kotlin user without nulls
data class KotlinUserWithoutNulls(val firstName: String,
// this parameter can't be null
val lastName: String,
val addresses: List<Address> = listOf()) {
data class Address(val city: String)
companion object {
fun fetchFirstCity(user: KotlinUserWithNulls) =
user.addresses.first().city
@adam-arold
adam-arold / JavaUser.java
Created September 11, 2017 16:33
Java user
public class JavaUser {
// ...
private final String firstName;
private final String lastName;
private final List<Address> addresses;
public Address getFirstAddress() {
Address firstAddress = addresses.get(0);
@adam-arold
adam-arold / KotlinUser.kt
Created September 11, 2017 16:34
Kotlin user
data class KotlinUser(val firstName: String,
val lastName: String,
val addresses: List<Address> = listOf()) {
data class Address(val city: String)
/**
* This is the same as in `JavaUser`.
*/
fun getFirstAddressNoInference(): Address {
@adam-arold
adam-arold / KotlinUser.kt
Created September 11, 2017 16:34
getFirstAddressInferred
/**
* Here the type of `firstAddress` is inferred from the context.
*/
fun getFirstAddressInferred(): Address {
val firstAddress = addresses.first()
return firstAddress
}
@adam-arold
adam-arold / KotlinUser.kt
Created September 11, 2017 16:35
getFirstAddress
/**
* Here the return type is inferred. Note that
* if a method consists of only one statement
* you can omit the curly braces.
*/
fun getFirstAddress() = addresses.first()
@adam-arold
adam-arold / JavaLineLoader.java
Created September 11, 2017 16:35
JavaLineLoader
public class JavaLineLoader {
public List<String> loadLines(String path) {
List<String> lines = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(path))) {
String line;
while((line = br.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
@adam-arold
adam-arold / KotlinLineLoader.kt
Created September 11, 2017 16:36
KotlinLineLoader
class KotlinLineLoader {
fun loadLines(path: String) = File(path).readLines()
}
@adam-arold
adam-arold / JavaFilterOperation.java
Created September 11, 2017 16:36
JavaFilterOperation
public class JavaFilterOperation {
private List<String> items;
@FunctionalInterface
interface FilterOperation {
Boolean filter(String element);
}
private List<String> filterBy(FilterOperation fn) {
@adam-arold
adam-arold / KotlinFilterOperation.kt
Created September 11, 2017 16:37
KotlinFilterOperation
class KotlinFilterOperation {
private val items = listOf<String>()
fun filterBy(fn: (String) -> Boolean) = items.filter(fn)
fun doFilter() {
filterBy(String::isNotEmpty)
// note the exension function `isNotEmpty` added to `String`!
}