Skip to content

Instantly share code, notes, and snippets.

Avatar

Sylvain HOCQ SylvainHocq

  • PARIS
View GitHub Profile
View If you want to read a file in kotlin with RX
fun getReadLinesObservable(ctx: Context, fileName: String): Observable<String> {
return Observable
.create { emitter:Emitter<String> ->
val input: BufferedReader
try {
val open = ctx.assets.open(fileName)
input = BufferedReader(InputStreamReader(open))
var line = input.readLine()
while (line != null) {
emitter.onNext(line)
View compose rx transformer
private fun composeTransformer(vararg transformers: Transformer<Any, Any>): Transformer<Any, Any> {
return Transformer { t ->
var resultObservable = t
transformers.forEach {
resultObservable = resultObservable.compose(it)
}
resultObservable
}
}
View rx pair with previous
fun <TSource> Observable<TSource>.pairWithPrevious()
: Observable<Pair<TSource?, TSource?>> {
return this
.map {
Pair(null as TSource?, it)
}
.scan { acc: Pair<TSource?, TSource?>, current: Pair<TSource?, TSource?> ->
Pair(acc.second, current.second)
}
}
View Kotlin null check for multiple nullable var’s.kt
inline fun <A, B, R> ifNotNull(a: A?, b: B?, code: (A, B) -> R) {
if (a != null && b != null) {
code(a, b)
}
}
inline fun <A, B, C, R> ifNotNull(a: A?, b: B?, c: C?, code: (A, B, C) -> R) {
if (a != null && b != null && c != null) {
code(a, b, c)
}
@SylvainHocq
SylvainHocq / OkHttp3Stack.java
Created Sep 25, 2016 — forked from alashow/OkHttp3Stack.java
An OkHttp backed HttpStack for Volley (okhttp3 version)
View OkHttp3Stack.java
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Circle Internet Financial
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
You can’t perform that action at this time.