Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
STAY HOME

Keita Kajiwara kakajika

🏠
STAY HOME
View GitHub Profile
@kakajika
kakajika / combinations.kt
Last active Sep 22, 2018
List all combinations of picking n elements.
View combinations.kt
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 }
}
@kakajika
kakajika / lazyFast.kt
Created Sep 20, 2018
Non-atomic access extension for lazy.
View lazyFast.kt
fun <T> lazyFast(operation: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) {
operation()
}
@kakajika
kakajika / FullscreenOverlayService.java
Last active Oct 12, 2021
Android Service implementation with fullscreen overlay window (over status bar & navigation bar)
View FullscreenOverlayService.java
class FullscreenOverlayService extends Service {
private View overlay;
private WindowManager windowManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@kakajika
kakajika / AvoidSpinnerDropdownFocus.java
Last active Aug 30, 2021
Avoid taking window focus by Android Spinner's Dropdown to keep setSystemUiVisibility flags (such as Immersive Mode).
View AvoidSpinnerDropdownFocus.java
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) {
@kakajika
kakajika / ListDataSource.kt
Created Jun 14, 2018
Android Architecture Components DataSource implementation to supply simple List.
View ListDataSource.kt
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))
}
}
@kakajika
kakajika / Array+windowed.swift
Last active Mar 27, 2018
Ported from Kotlin-stdlib
View Array+windowed.swift
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)
@kakajika
kakajika / ObservableType+isEmpty.swift
Last active Feb 27, 2018
RxSwift.ObservableType::isEmpty()
View ObservableType+isEmpty.swift
extension ObservableType {
func isEmpty() -> Single<Bool> {
return self
.take(1)
.map { _ in false }
.ifEmpty(switchTo: Observable.just(true))
.asSingle()
}
@kakajika
kakajika / flatMapCompletable.swift
Created Feb 16, 2018
Single::flatMapCompletable in RxSwift
View flatMapCompletable.swift
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()
}
@kakajika
kakajika / Sequence+associate.swift
Last active Jan 31, 2018
A port of Kotlin-stdlib's associate method to Swift 4.
View Sequence+associate.swift
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] {
View Sequence+sumBy.swift
extension Sequence {
func sumBy(selector: (Element) -> Int) -> Int {
return self.reduce(into: 0) { (sum, element) in
sum += selector(element)
}
}
}