Skip to content

Instantly share code, notes, and snippets.

View aballano's full-sized avatar
🏠
Working from home

Alberto Ballano aballano

🏠
Working from home
View GitHub Profile
@aballano
aballano / require_dependency.py
Created April 15, 2017 00:40
Functions to install required dependencies for a python script.
#!/usr/bin/env python
import subprocess
import sys
def run_command(command):
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip()
sealed class Option<out A> {
object None : Option<Nothing>()
data class Some<out A>(val value: A) : Option<A>()
inline fun <B> map(f: (A) -> B): Option<B> = when (this) {
is None -> this
is Some -> Some(f(value))
}
}
// Considerably inefficient implementation for test purposes
fun fib(k: Int): Long = when (k) {
0 -> 1
1 -> 1
else -> fib(k - 1) + fib(k - 2)
}
@aballano
aballano / GenericExtensions.kt
Created March 12, 2017 16:57
Kotlin ternary operator
// Source: http://stackoverflow.com/a/39687177/616119
infix fun <T> Boolean.then(param: T): T? = if (this) param else null
// Usage:
println(condition then "true" ?: "false")
@Override public Observable showDeleteFailedWithRetry() {
Snackbar snackbar = Snackbar.make(rootView, "Can't delete chat", Snackbar.LENGTH_LONG);
Observable<Integer> observable = RxSnackbar.actionClicked(snackbar, "Retry"));
Observable<Integer> dismissObservable = RxSnackbar.dismisses(snackbar)
.filter(event -> event != Snackbar.Callback.DISMISS_EVENT_ACTION)
.flatMap(ignored -> Observable.error(new Throwable()));
snackbar.show();
return observable.mergeWith(dismissObservable);
}
@aballano
aballano / remove_gradle_wrapper.sh
Created November 10, 2016 10:49
Removes all gradle wrapper folders except for the specified one.
cd ~/.gradle/wrapper/dists/
find . ! -name 'gradle-2.14.1-all' -type d -depth 1 -exec rm -f -r {} +
public void onDeleteChatAt(int id) {
interactor.deleteChat(id)
.retryWhen(errors -> errors.flatMap(error -> view.showDeleteFailedWithRetry()))
.subscribe(() -> view.removeChat(id), logError())
}
public void onDeleteChatAt(int id) {
interactor.deleteChat(id)
.retryWhen(errors -> view.showDeleteFailedWithRetry())
.subscribe(() -> view.removeChat(id), logError())
}
@aballano
aballano / View.java
Last active November 18, 2016 10:18
@Override public Observable showDeleteFailedWithRetry() {
Snackbar snackbar = Snackbar.make(rootView, "Can't delete chat", Snackbar.LENGTH_LONG);
Observable<Integer> observable = RxSnackbar.actionClicked(snackbar, "Retry"));
snackbar.show();
return observable;
}
public void onDeleteChatAt(int id) {
interactor.deleteChat(id)
.subscribe(() -> view.removeChat(id), logError())
}