Skip to content

Instantly share code, notes, and snippets.

View christianscott's full-sized avatar

Christian Scott christianscott

View GitHub Profile
@christianscott
christianscott / Grid.jsx
Last active May 5, 2017 02:09
Virtualised grid using ReactJS and RxJS
/*
* Virtualised grid using ReactJS and RxJS.
*
* Squares in the grid have the class `visible` only when they are inside the viewport.
*
* When the container has not been scrolled for 50ms, Grid.invalidate() is
* called and the visibility of each of the squares is checked using getBoudingClientRect().
*
* It is necessary to debounce the stream, otherwise this component will be horribly slow.
*
@christianscott
christianscott / rockPaperScissors.hs
Created May 2, 2017 03:16
Rock paper scissors in Haskell
module RPS where
import Data.Char (isSpace, toUpper)
import Data.Maybe (isNothing)
import Data.Text (strip)
data Move = Rock | Paper | Scissors
deriving (Show, Eq, Enum)
instance Ord Move where
@christianscott
christianscott / linkedlist.c
Created May 5, 2017 01:24
Merge sorting linked lists
#include "linkedlist.h"
/*
* Create a new LinkedList node
*/
node *create(int data, node *next)
{
node *new_node = (node*)malloc(sizeof(node));
if (new_node == NULL) {
@christianscott
christianscott / match.js
Last active May 12, 2017 09:22
Python-style destructured for loops in ES6 javascript
// Write a new zip as we don't have one in js
const zip = (first, second) => {
return [...first].map((x, i) => [x, second[i]])
}
const match = (first, second) => {
const seen = new Map()
const zipped = zip(first, second)
for (let [x, y] of zipped) {
@christianscott
christianscott / postie.py
Created May 30, 2017 02:47
Postie - stack based postfix calculator with variables
from collections import deque
class Postie:
def __init__(self):
self.__identifiers = dict()
def run(self):
"Run the calculator"
self.__display_intro()
@christianscott
christianscott / four_letters_in_common.py
Created July 25, 2017 23:17
Summer of Tech Hackfest - Code Challenge (Four letters in common)
def get_substrings(word, k):
"""Returns a generator containing all substrings in word of length k"""
for i in range(len(word) - k + 1):
yield word[i:i+k]
def k_letters_in_common(source, words, k):
"""Given a source word and a sequence of words, find all words in the
sequence that share a substring of length k with the source word.
Returns a list of these words (internally, a list is an array in python).
@christianscott
christianscott / todays_note.fish
Created October 18, 2017 04:28
Daily note-taking with a simple fish shell function
function todays_note
set todays_note_dir (date "+$HOME/Dropbox/daily_notes/%Y/%b/")
set todays_filename (date '+%A-%d.md')
if not test -d $todays_note_dir
mkdir -p $todays_note_dir
end
pushd $todays_note_dir; and nvim $todays_filename; and popd
end
@christianscott
christianscott / pipe.py
Last active October 25, 2017 01:23
Pipe in python
def pipe(*funcs):
return lambda initial: functools.reduce(apply, funcs, initial)
def apply(arg, func):
return func(arg)
if __name__ == '__main__':
pipe(uppercase, reverse, cipher)("hello, world!") # as an example
@christianscott
christianscott / prototype.py
Last active November 7, 2017 08:17
Poor man's prototypal inheritance in Python
class Object:
def __init__(self, *parents):
self.parents = [parent() for parent in parents]
def __getattr__(self, name):
for parent in self.parents:
if hasattr(parent, name):
return getattr(parent, name)
raise AttributeError(f"'Object' object has no attribute '{name}'")
@christianscott
christianscott / animate.js
Last active November 11, 2017 01:11
Smooth progress bar animation using requestAnimationFrame
const makeLoader = (progressEl, updateProgressCount, initialProgress=0) => {
let progressCount = initialProgress
const load = () => {
progressCount = updateProgressCount(progressCount)
progressEl.value = progressCount
requestAnimationFrame(load) // don't need to use setTimeout - will only be called ~60 times/second
}
return load
}