Skip to content

Instantly share code, notes, and snippets.

View mitac-lien's full-sized avatar
:octocat:
[object Object]

Chun Hao Lien mitac-lien

:octocat:
[object Object]
View GitHub Profile
@mitac-lien
mitac-lien / cartesianProduct.ts
Created March 16, 2021 09:35 — forked from ssippe/cartesianProduct.ts
Typescript Cartesian Product
const f = (a: any[], b: any[]): any[] =>
[].concat(...a.map(a2 => b.map(b2 => [].concat(a2, b2))));
export const cartesianProduct = (a: any[], b: any[], ...c: any[]) => {
if (!b || b.length === 0) {
return a;
}
const [b2, ...c2] = c;
const fab = f(a, b);
return cartesianProduct(fab, b2, c2);
@mitac-lien
mitac-lien / JsonElement_Safe.kt
Created December 29, 2020 02:15 — forked from StratusHunter/JsonElement_Safe.kt
I was having trouble with safely parsing Gson objects without loads of try catch statements or a single try catch which would cause the whole object parsing to fail if a single field was incorrect. I also wanted to replicate org.json.JSONObject opt style functions to return null instead of throwing an exception so I can use Kotlin's null handlin…
import com.google.gson.*
import java.math.BigDecimal
import java.math.BigInteger
/**
* Created by Terence Baker on 13/02/2018.
*/
val JsonElement.optString: String?
get() = safeConversion { asString }