Skip to content

Instantly share code, notes, and snippets.

View felixchez's full-sized avatar

felixchez felixchez

View GitHub Profile
@felixchez
felixchez / gist:aa00f9a208c912a0beb105125482abda
Created May 7, 2020 01:58
TypeScript: Unit Conversion Class
/**
* Convert from units to another units e.g. from square feet to acres
* return original value if conversion units are not supported
* Usage:
* const convertedValue = convert(100).from('acres').to('square feet');
* expected:
* 4356000
*/
class Converter {
private toUnits: string;
@felixchez
felixchez / gist:8132a7db4431c9d2b1a139703339cfee
Created December 15, 2018 02:50
kotlin_benchmark_inline_function
inline fun benchmark(block: () -> Unit): Unit {
val startTime = System.nanoTime()
block()
val endTime = System.nanoTime()
Log.i("Time take to execute in ${endTime - startTime)")
}
===
fun onClickHandler() {
// onClickListener flows that handling Network Request, Database Persistence and UI Rendering
val startTime = System.nanoTime()
// onClickListener flows that handling Network Request, Database Persistence and UI Rendering
val endTime = System.nanoTime()
Log.i("Time take to execute in ${endTime - startTime)")
@felixchez
felixchez / gist:7a3154985b5321ff019b03c6b7c3e205
Created December 12, 2018 19:32
kotlin_notnullable_function_callback
fun doAsyncOperation(val onCallback: (String) -> Unit?) {
// do lengthy computation operation
onCallback("result")
}
===
doAsyncOperation { _ -> }
doAsyncOperation {result -> Log.v(result)}
@felixchez
felixchez / gist:422b992c63546362089836d787076038
Created December 12, 2018 19:21
kotlin_nullable_function_callback
fun doAsyncOperation(val onCallback: ((String) -> Unit)? = null) {
// do lengthy computation operation
onCallback?.invoke("result")
}
===
doAsyncOperation()
doAsyncOperation {result -> Log.v(result)}
val dataTypeA = arrayListOf(1, 2, 3)
Observable.fromIterable(dataTypeA)
.map {element -> when (element) {
1 -> "one"
2 -> "two"
3 -> "three"
else -> ""
}}
.toList().toObservable()