Skip to content

Instantly share code, notes, and snippets.

View Takhion's full-sized avatar

Eugenio Marletti Takhion

View GitHub Profile
@Takhion
Takhion / 1_property.kt
Created September 16, 2018 16:23
Kotlin property delegate utils
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
private typealias GetValue<This, R> = (thisRef: This, property: KProperty<*>) -> R
private typealias SetValue<This, R> = (thisRef: This, property: KProperty<*>, value: R) -> Unit
inline fun <This, R> property(
crossinline getValue: GetValue<This, R>
) =
@Takhion
Takhion / MultiReceiver.kt
Created July 8, 2018 15:52
Multiple receivers in Kotlin
val sample: (X, Y, Z) -> Int = multiReceiver { { { x + y + z } } }
fun <A, B, C, R> multiReceiver(f: A.() -> B.() -> C.() -> R) = { a: A, b: B, c: C -> f(a)(b)(c) }
class X(val x: Int)
class Y(val y: Int)
class Z(val z: Int)
@Takhion
Takhion / RxFilterIsInstance.kt
Last active June 29, 2020 17:04 — forked from rock3r/RxFilterIsInstance.kt
Port Kotlin's awesome filterIsInstance() to RxJava — one call to filter by type and map-cast to that type
package me.seebrock3r.extensions.reactivex
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Flowable<*>.filterIsInstance(): Flowable<R> =
filter { it is R }
@Takhion
Takhion / ExhaustiveWhen.kt
Last active July 28, 2021 19:19
Exhaustive `when` mapping in Kotlin
@file:Suppress("PackageDirectoryMismatch") // root package looks great when importing, ie. `import exhaustive`
/**
* Allows requiring an exhaustive mapping in a `when` block when used with the [rangeTo] operator.
* @sample [exhaustive_when_example]
*/
@Suppress("ClassName") // lowercase because it should look like a keyword
object exhaustive {
@Takhion
Takhion / facebook-photo-download.go
Last active March 24, 2018 17:03 — forked from thebitguru/facebook-photo-download.go
A simple go program to download all the high resolution pictures from your facebook albums.
package main
/*
* A simple go program to download all the high resolution pictures from your facebook albums.
*
* To run this:
* 1. Go to https://developers.facebook.com/tools/explorer/?method=GET&path=me&version=v2.8
* 2. Get an Access Token: Get Token > Get User Access Token > Check "user_photos"
* 3. Paste in the app.
*/
@Takhion
Takhion / gorun.go
Created March 24, 2018 17:00 — forked from mattn/gorun.go
//
// gorun - Script-like runner for Go source files.
//
// https://wiki.ubuntu.com/gorun
//
// Copyright (c) 2011 Canonical Ltd.
//
// Written by Gustavo Niemeyer <gustavo.niemeyer@canonical.com>
//
package main
@Takhion
Takhion / SharedPreferencesPropertyDelegate.kt
Created October 13, 2017 07:58
Shared Preferences Kotlin property delegate with injected provider
import android.app.Activity
import kotlin.reflect.KProperty
interface SharedPreferencesWriter //TODO
interface SharedPreferencesWriterProvider {
val sharedPreferencesWriter: SharedPreferencesWriter
fun <T> prefs() = SharedPreferencesPropertyDelegate<T>()
}
@Takhion
Takhion / demo.dart
Last active August 11, 2022 11:09
[Flutter] TextSelectionLayout (see https://stackoverflow.com/a/46244263/1306903)
// animated GIF: https://i.stack.imgur.com/mHOIc.gif
// see https://stackoverflow.com/a/46244263/1306903
import 'package:flutter/material.dart';
import 'ink_text_selection_layout.dart';
class Demo extends StatelessWidget {
@override
Widget build(context) => inkTextSelectionLayout(
import rx.Observable
import rx.subjects.BehaviorSubject
import SingletonObservable.ItemWrapper
/**
* Represents the current subscription status (either 'unsubscribed' or 'subscribed') of a [SingletonObservable].
* The index starts at [SingletonObservable.firstSubscriptionIndex] as 'unsubscribed', and every subsequent value alternates between the two states.
* Use [SingletonObservable.isSubscribed] to calculate the status for a specific [SubscriptionIndex].
*
* Note that a subscription index is constant for the lifetime of a stream, which is completed by the first time either one of
fun test() {
val item1 = Item(GameObject())
val item2 = Item(GameObject())
assert(ItemStack(setOf(item1, item2)) is ItemStack)
assert(ItemStack(setOf(item1)) is Item)
assert(ItemStack(emptySet()) is Empty)
}
class ItemStack private constructor(val items: Set<Item>) : Elem() {
companion object {