Skip to content

Instantly share code, notes, and snippets.

View ZakTaccardi's full-sized avatar

Zak Taccardi ZakTaccardi

View GitHub Profile
@ZakTaccardi
ZakTaccardi / Result.kt
Created December 18, 2017 17:09
Idea for better sealed class JVM interop for kotlin compiler
// generates callback and method to handle that callback for better Java interop
annotation class JvmCallback
@JvmCallback
sealed class Result {
data class Success(val successId: String) : Result()
data class Error(val exception: Exception) : Result()
// would be automatically generated because of JvmCallback
@file:Suppress("unused", "FunctionName", "IllegalIdentifier")
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
/**
* The best way to launch yourself an activity. Your implementation should enable the following api:
@ZakTaccardi
ZakTaccardi / ObservingState.kt
Created July 6, 2017 16:59
Exposing Information: Observing Events vs State (in Rx)
class UserRepository{
// note: hot observables that do not replay missed events
fun logins(): Observable<User>
fun logouts(): Observable<Unit>
}
/**
* An abstract user with two implementations:
* "LoggedIn" and "Logged out"
*/
package com.example.taccardi.zak;
import android.support.annotation.NonNull;
import io.reactivex.Observable;
/**
* Use this class to lock (prevent charges to it) or unlock (allow charges) a card.
*/
interface CardLockRepository {
@ZakTaccardi
ZakTaccardi / FizzBuzzTest.java
Created April 17, 2017 20:39
A fizz buzz that uses algebraic data types
import org.junit.Test;
import java.util.function.Consumer;
/**
* Note: no actual assertions are done here.
*/
public class FizzBuzzTest {
static void defaultPrint(BaseFizzBuzz base) {
@ZakTaccardi
ZakTaccardi / BetterApi.kt
Created April 6, 2017 20:17
API Design — Handling exceptions
private class BetterApi {
fun login(credentials: Credentials): LoginResponse {
//login...
TODO()
}
class LoginResponse private constructor(
val user: User?,
class TemporalCoupling {
fun incorrectUsageOfBadApi() {
val badApi: BadApi = BadApi()
badApi.username = "Zak"
badApi.password = "temporalCouplingIsBad_123"
badApi.login() //throws an NullPointerException
}
@ZakTaccardi
ZakTaccardi / DealCardsUi.kt
Last active March 29, 2017 22:18
DealCardsUi
/**
* The user interface for dealing cards.
*
* @see DealCardsActivity
*/
interface DealCardsUi : StateRenderer<DealCardsUi.State> {
val state: State
override fun render(state: State)
@ZakTaccardi
ZakTaccardi / KotlinExtensionTest.kt
Created February 20, 2017 16:45
Making tests more readable with Kotlin’s extension functions
@Test
fun testClear() {
clickNumber(1)
clickNumber(2)
clickNumber(3)
state.assertValue(123)
longPressClear()
state.assertValue(0)
}
@ZakTaccardi
ZakTaccardi / Crossfader.kt
Last active April 11, 2017 19:35
Animate between multiple views, allowing only one view to be visible at a time.
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.view.View
import android.view.ViewGroup
/**
* Animates a view between its states.
*
* Currently only supports crossfading.