+
Staff Engineer @ GrubHub
CKUG Co-Organizer
ktlint-gradle Maintainer
@wakingrufus wakingrufus
@wakingrufus@bigshoulders.city
data class ImmutableDataValue(val stringData: String)
data class ChatMessage(
val user: String
val timestamp: Instant
val message: String)
fun newMessage(message: String) : ChatMessage {
return ChatMessage(
user = System.getProperty("user.name"),
timestamp = Instant.now(),
message = message)
}
data class ChatMessage(
val user: String
val timestamp: Instant
val message: String)
fun newMessage(
message: String,
user: String,
timestamp: Instant) : ChatMessage {
return ChatMessage(
user = user,
timestamp = timestamp,
message = message)
}
fun addNewMessageSideEffect(
messages: MutableList<ChatMessage>,
newMessage: ChatMessage) {
messages.add(newMessage)
// Modifies input
}
val externalMessages: List<ChatMessage> = ArrayList()
fun addNewMessageSideEffect2(newMessage: ChatMessage) {
externalMessages.add(newMessage)
// Modifies something outside of scope
}
fun addNewMessage(
messages: List<ChatMessage>,
newMessage: ChatMessage) : List<ChatMessage> {
return listOf(*messages.toTypedArray(), newMessage)
}
class BigDecimalUtil {
private BigDecimalUtil() { }
static boolean bigDecimalEquals(BigDecimal one, BigDecimal two) {
return one.compareTo(two) == 0;
}
}
fun bigDecimalEquals(one: BigDecimal, two: BigDecimal) : Boolean {
return one.compareTo(two) == 0
}
fun bigDecimalEquals(one: BigDecimal, two: BigDecimal) : Boolean
= one.compareTo(two) == 0
val bigDecimalEquals: (Int, Int) -> Boolean
= { one, two -> one.compareTo(two) == 0 }
static <T> boolean contains(
Collection<T> collection,
Predicate<T> predicate) {
for(T i: collection){
if(predicate.test(i)){
return true;
}
}
return false;
}
contains(
Arrays.asList("1", "2", "10"),
(test) -> test.length() > 1
); // true
@FunctionalInterface
public interface Predicate<T> {
boolean test(T var1);
}
fun <T> boolean contains(
collection: Collection<T>,
predicate: (T) -> Boolean) {
for(T i: collection){
if(predicate(i)){
return true;
}
}
return false;
}
contains(Arrays.asList("1", "2", "10")) {
it.length() > 1
} // true
fun newMessage(
message: String,
user: String,
timestamp: () -> Instant = { Instant.now() }
) : ChatMessage {
return ChatMessage(
user = user,
timestamp = timestamp(),
message = message)
}
newMessage("message", "user") // production code
// test code
newMessage("message", "user") {
Instant.EPOCH
}
fun <T> T?.orElse(fallback: () -> T): T {
return if (this == null) {
fallback()
} else {
this
}
}
val nullableString: String? = null
val nonNullString : String = nullableString.orElse { "" }
var s : String?
s = null // OK
var s : String
s = null // Compile Error
sealed class Thing {
class Thing1(val thing1: String): Thing()
class Thing2(val thing2: Int) : Thing()
}
val thing: Thing
when(thing){
is Thing1 -> thing.thing1 // String
is Thing2 -> thing.thing2 // Int
}
final var opt = Optional.ofNullable(...);
opt.isPresent(); // true or false
opt.or(fallback);
opt.map(String::length).orElse(0);
val opt: String? = ...
opt != null
opt ?: fallback
opt?.length ?: 0
sealed class Either<A, B> {
class Left<A, B>(val value: A) : Either<A, B>()
class Right<A, B>(val value: B) : Either<A, B>()
}
val e : Either<String, Int>
when(e){
is Left -> e.value.length > 2
is Right -> e.value > 2
}
sealed class Try<A> {
class Success<A>(val value: A) : Try<A>()
class Failure<A>(val ex: Exception) : Try<A>()
}
fun <A> doTry(work: () -> A): Try<A> {
return try {
Try.Success(work())
} catch (e: Exception) {
Try.Failure(e)
}
}
val result = doTry { "" }
when (result) {
is Try.Failure -> result.ex
is Try.Success -> result.value
}
Credit: Scott Wlaschin
fun <A, B> Try<A>.andThen(next: (A) -> B): Try<B> {
return when (this) {
is Try.Success -> doTry { next(this.value) }
is Try.Failure -> Try.Failure<B>(this.exception)
}
}
val result = doTry { "" }
.andThen { it.length }
.andThen { it + 2 }
.andThen { it < 0 }
when (result) {
is Try.Failure -> result.exception
is Try.Success -> result.value
}
fun myFunct1(s: String) : Try<Int> = doTry { it.length }
fun <A, B> Try<A>.flatMap(next: (A) -> Try<B>): Try<B> {
return when (this) {
is Try.Success -> next(this.value)
is Try.Failure -> Try.Failure<B>(this.exception)
}
}
val result = doTry { "" }
.flatMap { myFunc1(it) }
when (result) {
is Try.Failure -> result.exception
is Try.Success -> result.value
}
@wakingrufus wakingrufus
@wakingrufus@bigshoulders.city