Skip to content

Instantly share code, notes, and snippets.

View sliskiCode's full-sized avatar
🌴
On vacation

Piotr Ślesarew sliskiCode

🌴
On vacation
View GitHub Profile
@sliskiCode
sliskiCode / Sugar.kt
Created December 5, 2017 20:16
6 magic sugars that can make your Kotlin codebase happier #12
val receiver: String = "Fructos"
val block: String.() -> Unit = {
println(toUpperCase())
println(toLowerCase())
println(capitalize())
}
val result: Unit = with<String, Unit>(receiver, block)
@sliskiCode
sliskiCode / Sugar.kt
Last active December 2, 2017 21:26
6 magic sugars that can make your Kotlin codebase happier #11
fun access(contract: Contract,
employee: Employee) = when (contract to employee) {
PROBATION to SENIOR_ENGINEER,
PROBATION to REGULAR_ENGINEER -> NotGranted(AssertionError("Access not allowed on probation contract."))
PERMANENT to SENIOR_ENGINEER,
PERMANENT to REGULAR_ENGINEER,
PERMANENT to JUNIOR_ENGINEER,
CONTRACTOR to SENIOR_ENGINEER -> Granted(DateTime(1))
CONTRACTOR to REGULAR_ENGINEER,
PROBATION to JUNIOR_ENGINEER,
@sliskiCode
sliskiCode / Sugar.kt
Last active December 2, 2017 08:33
6 magic sugars that can make your Kotlin codebase happier #10
fun access(contract: Contract,
employee: Employee) = when (Pair(contract, employee)) {
Pair(PROBATION, SENIOR_ENGINEER),
Pair(PROBATION, REGULAR_ENGINEER),
Pair(PROBATION, JUNIOR_ENGINEER) -> NotGranted(AssertionError("Access not allowed on probation contract."))
Pair(PERMANENT, SENIOR_ENGINEER),
Pair(PERMANENT, REGULAR_ENGINEER),
Pair(PERMANENT, JUNIOR_ENGINEER),
Pair(CONTRACTOR, SENIOR_ENGINEER) -> Granted(DateTime(1))
Pair(CONTRACTOR, REGULAR_ENGINEER),
@sliskiCode
sliskiCode / Sugar.kt
Last active December 2, 2017 08:32
6 magic sugars that can make your Kotlin codebase happier #9
fun access(employee: Employee,
contract: Contract): SafariBookAccess {
return when (employee) {
SENIOR_ENGINEER -> when (contract) {
PROBATION -> NotGranted(AssertionError("Access not allowed on probation contract."))
PERMANENT -> Granted(DateTime())
CONTRACTOR -> Granted(DateTime())
}
REGULAR_ENGINEER -> when (contract) {
PROBATION -> NotGranted(AssertionError("Access not allowed on probation contract."))
@sliskiCode
sliskiCode / Sugar.kt
Last active December 2, 2017 08:33
6 magic sugars that can make your Kotlin codebase happier #8
fun access(employee: Employee,
contract: Contract): SafariBookAccess
@sliskiCode
sliskiCode / Sugar.kt
Last active November 27, 2017 10:02
6 magic sugars that can make your Kotlin codebase happier #7
sealed class SafariBookAccess
data class Granted(val expirationDate: DateTime) : SafariBookAccess()
data class NotGranted(val error: AssertionError) : SafariBookAccess()
data class Blocked(val message: String) : SafariBookAccess()
@sliskiCode
sliskiCode / Sugar.kt
Last active December 10, 2017 11:06
6 magic sugars that can make your Kotlin codebase happier #6
enum class Employee {
DEV_LEAD,
SENIOR_ENGINEER,
REGULAR_ENGINEER,
JUNIOR_ENGINEER
}
enum class Contract {
PROBATION,
PERMANENT,
@sliskiCode
sliskiCode / Sugar.java
Last active November 27, 2017 08:59
6 magic sugars that can make your Kotlin codebase happier #5
public final void sugar(@NotNull Response response) {
Intrinsics.checkParameterIsNotNull(response, "response");
String var3;
if (response instanceof Success) {
var3 = ((Success)response).getBody();
System.out.println(var3);
} else if (response instanceof Error) {
var3 = "" + ((Error)response).getCode() + ' ' + ((Error)response).getMessage();
System.out.println(var3);
@sliskiCode
sliskiCode / Sugar.kt
Last active November 27, 2017 08:59
6 magic sugars that can make your Kotlin codebase happier #4
fun sugar(response: Response) = when (response) {
is Success -> println(response.body)
is Error -> println("${response.code} ${response.message}")
Timeout -> println(response.javaClass.simpleName)
}
@sliskiCode
sliskiCode / Sugar.kt
Last active November 27, 2017 08:59
6 magic sugars that can make your Kotlin codebase happier #3
fun sugar(response: Response) = when (response) {
is Success -> ...
is Error -> ...
Timeout -> ...
}