This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Collection<T>.combinations(n: Int): List<List<T>> { | |
return when { | |
n < 0 -> throw Error("combinations size cannot be negative") | |
n > size -> emptyList() | |
n == 1 -> map { mutableListOf(it) } | |
else -> foldIndexed<T, List<List<T>>>(mutableListOf()) { index, all, first -> | |
all + drop(index + 1) | |
.combinations(n - 1) | |
.map { it + first } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> lazyFast(operation: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) { | |
operation() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FullscreenOverlayService extends Service { | |
private View overlay; | |
private WindowManager windowManager; | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.ListPopupWindow; | |
import android.widget.PopupWindow; | |
import android.widget.Spinner; | |
public static void avoidSpinnerDropdownFocus(Spinner spinner) { | |
try { | |
Field listPopupField = Spinner.class.getDeclaredField("mPopup"); | |
listPopupField.setAccessible(true); | |
Object listPopup = listPopupField.get(spinner); | |
if (listPopup instanceof ListPopupWindow) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListDataSource<T>(private val items: List<T>) : PositionalDataSource<T>() { | |
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) { | |
callback.onResult(items, 0, items.size) | |
} | |
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) { | |
val start = params.startPosition | |
val end = params.startPosition + params.loadSize | |
callback.onResult(items.subList(start, end)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
func windowed(size: Int, step: Int = 1, partialWindows: Bool = false) -> [[Element]] { | |
let count = self.count | |
var index = 0 | |
var result: [[Element]] = [] | |
while (index < count) { | |
let windowSize = Swift.min(size, count - index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ObservableType { | |
func isEmpty() -> Single<Bool> { | |
return self | |
.take(1) | |
.map { _ in false } | |
.ifEmpty(switchTo: Observable.just(true)) | |
.asSingle() | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension PrimitiveSequenceType where Self: ObservableConvertibleType, Self.TraitType == SingleTrait { | |
func flatMapCompletable(_ selector: @escaping (E) -> Completable) -> Completable { | |
return self | |
.asObservable() | |
.flatMap { e -> Observable<Never> in | |
selector(e).asObservable() | |
} | |
.asCompletable() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func associate<K, V>(_ transform: (Element) -> (K, V)) -> [K: V] { | |
return self.reduce(into: [:]) { (dict, element) in | |
let (key, value) = transform(element) | |
dict[key] = value | |
} | |
} | |
func associateBy<K>(_ keySelector: (Element) -> K) -> [K: Element] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func sumBy(selector: (Element) -> Int) -> Int { | |
return self.reduce(into: 0) { (sum, element) in | |
sum += selector(element) | |
} | |
} | |
} |