Skip to content

Instantly share code, notes, and snippets.

SUPERCILEX Individual Contributor License Agreement

In order to clarify the intellectual property license granted with Contributions from any person or entity, SUPERCILEX Inc. ("SUPERCILEX") must have a Contributor License Agreement ("CLA") on file that has been signed by each Contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of SUPERCILEX; it does not change your rights to use your own Contributions for any other purpose.

You accept and agree to the following terms and conditions for Your present and future Contributions submitted to SUPERCILEX. Except for the license granted herein to SUPERCILEX and recipients of software distributed by SUPERCILEX, You reserve all right, title, and interest in and to Your Contributions.

  1. Definitions.

"You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with SUPERCILEX. For legal entities, the entity

@SUPERCILEX
SUPERCILEX / pyTriples.java
Created October 27, 2016 04:48
Pythagorean Triples: Think Backwards
// If you have ever tried computing all the possible pythagorean triples (a^2 + b^2 = c^2) without duplicates,
// you might have tried creating 3 nested loops with the first one being a, the second b, and the third c.
// This method kind of works, however, you end up with duplicates and unbelievable slow just getting into the hundreds (for c).
// A more efficient way to solve the problem would be to start with c being the first loop
// and the other two variables in the second and third loop.
// Such a method might look like so:
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
val hadModel = ViewModelProviders.of(this).has(MyViewModel::class.java)
val model = ViewModelProviders.of(this).get(MyViewModel::class.java)
if (!hadModel) {
if (savedInstanceState != null) {
model.init(savedInstanceState.getMyStuff())
} else {
model.init(arguments.getMyStuff())
}
}
{
"team-indices": {
"uid1": {
"teamKey1": 2521, // We sort our teams in ascending order by team number
"teamKey2": { ... },
"teamKey3": { ... }
},
"uid2": { ... },
"uid3": { ... }
},
{
"teams": { // Collection
"teamId1": { // Document
"name": "SERT",
"number": 2521,
"owners": { // Object within document
"uid1": 2521, // We sort our teams in ascending order by team number
"uid2": { ... },
"uid3": { ... }
},
FirebaseFirestore.getInstance().collection("teams").add(team)
fun Team.addScout() {
val scoutRef: DocumentReference = FirebaseFirestore.getInstance()
.collection("teams")
.document(id /* Field in Team */)
.collection("scouts")
.document() // Creating a document ref with a truly random id
scoutRef.set(Scout(scoutRef.id, templateId))
firestoreBatch {
val metricsRef = scoutRef.collection("metrics")
/** Deletes all scouts for a team, including their `metrics` subcollection. */
fun Team.deleteAllScouts() {
ref.collection("scouts").delete() // Kotlin's syntactic sugar
.addOnSuccessListener { snapshots: List<DocumentSnapshot> ->
for (snapshot in snapshots) deleteScout(snapshot.id)
}
}
fun Team.deleteScout(id: String) {
val scoutDoc = ref.collection("scouts").document(id)
FirebaseFirestore.getInstance()
.collection("teams")
.addSnapshotListener { snapshot: QuerySnapshot?, e: FirebaseFirestoreException? ->
if (e != null) {
FirebaseCrash.report(e)
return@addSnapshotListener
}
snapshot!! // The parameters follow a XOR pattern so this is perfectly safe
// Do stuff
// Please don't actually use this method, it's just a nice example of how painful the listener registration API is.
// I wrote it with a case of YAGNI before I really understood the Firestore APIs—you should
// be using `Query#get()` if you need the most up-to-date copy of your documents.
fun Query.getFromServer(): Task<QuerySnapshot> = TaskCompletionSource<QuerySnapshot>().apply {
val listener = object : EventListener<QuerySnapshot> {
lateinit var registration: ListenerRegistration
override fun onEvent(snapshot: QuerySnapshot?, e: FirebaseFirestoreException?) {
if (e == null) {
if (!snapshot!!.metadata.isFromCache) {