Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / playground.rs
Last active October 7, 2020 06:11 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait Truthy: Sized {
fn is_truthy(&self) -> bool;
fn as_option(self) -> Option<Self> {
if self.is_truthy() {
Some(self)
} else {
None
}
}
fn or(self, default: Self) -> Self {
@Lucretiel
Lucretiel / playground.rs
Created June 22, 2019 01:48 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait Truthy: Sized {
fn is_truthy(&self) -> bool;
fn as_option(self) -> Option<Self> {
if self.is_truthy() {
Some(self)
} else {
None
}
}
}
@Lucretiel
Lucretiel / useKeyedCallback.tsx
Last active May 18, 2019 23:39
React hook for creating callback functions in a loop to pass to individual components
import {memoize} from "lodash"
/**
* Helper for passing a callback to each component in a loop. Given a callback
* with the signature (key, ..args), returns a new factory with the signature
* (key) => (...args). When called with a key, returns a wrapper function taking
* (...args) wich calls the original callback. This wrapper function is guaranteed
* to be the same for any given key.
*
* The callback must have NO dependencies that might change between renders. It
@Lucretiel
Lucretiel / add_trap.bash
Last active April 6, 2022 02:53
Trap a command in bash without removing the previous trap command
#!/bin/bash
# This function adds an additional command to the trap stack. Each command
# added in this way will be called in reverse order when the signal is received.
#
# This function works with a simple concatenation with the currently existing
# trap function. Popping something from the trap stack without running it is
# left as an exercise to the reader; it requires at least a parser capable of
# matching { } correctly.
#
@Lucretiel
Lucretiel / advent_template.c
Last active December 5, 2018 16:49
A template for advent of code problems. Write your solution in the `solve` function; the template handles all the i/o
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/**
* Write a message to stderr, followed by a newline, then exit the program.
* This helper function is provided because C doesn't have throwable exceptions
* or any similarly ergonomic error handling.
*
* Arguments to this function are similar to `fprintf`; see online documentation
@Lucretiel
Lucretiel / spread_fixtures.py
Last active June 22, 2018 04:01
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
@Lucretiel
Lucretiel / spread_fixtures.py
Created June 22, 2018 03:58
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
@Lucretiel
Lucretiel / spread_fixtures.py
Created June 22, 2018 03:58
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
def get_rows(selected_table):
for row in selected_table.css('tr'):
cells = row.css('td').extract()
yield tuple(cells)
def find_bge_table(response):
for table_element in response.css('table'):
table = list(get_rows(table_element))
if any(row[0] == "BGE" for row in table):
@Lucretiel
Lucretiel / async.js
Last active October 11, 2017 04:50
Sample code showing the use of async functions in Javascript
// Create a promise that is resolved after `timeout` ms
const promiseTimer = timeout => new Promise(resolve => setTimeout(() => resolve(null), timeout))
async function counter(max) {
for(let i = 0; i < max; i++) {
console.log("Counting:", i)
// This is the important part. "await" allows the function to appear to block;
// other asynchronous work can be done here. This is basically the equivelent
// of a .then on promiseTimer