Skip to content

Instantly share code, notes, and snippets.

View joelburton's full-sized avatar

Joel Burton joelburton

View GitHub Profile
@joelburton
joelburton / shop.ts
Created February 24, 2024 00:09
Generics in TS
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
### WAYS TO LOOP OVER TWO THINGS AT ONCE IN PYTHON:
cats = ["fluffy", "puffy", "muffy"]
dogs = ["fido", "rover", "bowser"]
# jinja-specific way, won't work in regular python:
#
# {% for cat in cats %}
@joelburton
joelburton / leveret.kt
Last active September 20, 2021 00:06
Leveret Lunch in Functional Kotlin
data class Cell(val y: Int, val x: Int) {
fun neighbors() = listOf(Cell(y, x - 1), Cell(y - 1, x), Cell(y, x + 1), Cell(y + 1, x)) // WNES
}
data class Garden(val cells: List<List<Int>>) {
val nRows = cells.size
val nCols = cells[0].size
fun findCenterCells() = listOf(
Cell((nRows - 1) / 2, (nCols - 1) / 2), // NW
@joelburton
joelburton / delegation.kt
Last active September 18, 2021 22:49
OMG Delegation in Kotlin
/** Single abstract-method interface for a way to fly. */
fun interface FlyBehavior {
fun fly()
}
// implementations of the single-method interface
val FlyWithWings = FlyBehavior { println("I'm flying!") }
val FlyNoWay = FlyBehavior { println("No way") }
val FlyRocketPowered = FlyBehavior { println("Rocket!!!!!!") }
@joelburton
joelburton / 8.kt
Last active September 16, 2021 10:35
import java.io.File
/** Single instruction for state machine. */
data class Instruction(val opcode: String, val operand: Int)
/** Marker exception raised during state machine runs. */
class Answer(val result: Int) : Throwable()
val lines = File("8.txt")
.readLines()
class Course(val name: String, val start: Short, val end: Short)
/** For list of courses, print best schedule for a single classroom.
*
* The best schedule for a classroom is the one that has the most classes
* scheduled for it, with no classes overlapping with another class.
*/
fun bestSchedule(classes: MutableList<Course>): List<String> {
val schedule = mutableListOf<String>()
import java.util.*
/** Graph node for undirected graphs.
*
* @property value the value of the node.
* @property adjacent a set of the adjacent nodes for this one.
**/
class Node(
private val value: Any,
@joelburton
joelburton / flask-sqla-step-by-step.rst
Last active July 15, 2021 15:26
Setting up Flask/SQLAlchemy Step by Step

Setting up Flask <-> SQLAlchemy Step-by-Step

  1. Check app.py for postgresql:///sample_db <— the name of your new database is the thing after the 3 slashes. Use this in step 2.
  2. In terminal (and maybe not necessarily in the dir of your file, just anywhere?), type: createdb sample_db

    NOTE: (Yes, it doesn't matter what directory you're in when you run createdb.

@joelburton
joelburton / r17-study-hall-breakpoints-oo.md
Created August 10, 2020 19:24
Study Hall covering breakpoints and OO.
@joelburton
joelburton / pojo.py
Created July 31, 2020 18:12
For JS programmers who like to use obj.key = val
class POJO(dict):
def __getattr__(self, attr):
return self[attr]
def __setattr__(self, attr, val):
try:
super().__getattr__(self, attr)
super().__setattr__(self, attr, val)
except AttributeError:
self[attr] = val