Skip to content

Instantly share code, notes, and snippets.

View asher-dev's full-sized avatar

Asher asher-dev

  • Montreal, Canada
View GitHub Profile
@asher-dev
asher-dev / moon_journey.py
Created December 8, 2018 02:36
HackerRank Solution: Journey to the Moon
#!/bin/python3
# https://www.hackerrank.com/challenges/journey-to-the-moon/problem
import math
import os
import random
import re
import sys
from itertools import combinations
@asher-dev
asher-dev / bfs_shortest_path.py
Last active March 22, 2019 18:23
HackerRank Solution: BFS Shortest Path
#!/bin/python3
# https://www.hackerrank.com/challenges/bfsshortreach/problem
import math
import os
import random
import re
import sys
from itertools import chain
@asher-dev
asher-dev / struct.py
Last active January 25, 2019 02:04
Something approximating a python struct
#!/usr/bin/env python3
"""
[Created: March 2018]
Oh hello, I'm here to destroy everything you love about Python.
This is kind of ridiculous, but I wanted to create an easy way to
define struct-like classes in Python and it seemed like a good way
to learn some more about the python data model.
It's more of a syntax hack than anything else, but it was fun to
make.
@asher-dev
asher-dev / simple_argument_parser.py
Created March 6, 2018 09:14
Simple argument parser
#!/usr/bin/python3
# Requires Python 3.7+ or CPython 3.6 for ordered dict
# Lightweight type-checking argument parser requiring minimal boilerplate
# Just a coding exercise for myself, but if you like it please use it!
# Todo:
# - Boolean flags and grouping (e.g. "-o -a", "-xAd")
# - Required and optional arguments (e.g. "--mode quiet -n 2")
import sys
from collections import namedtuple
@asher-dev
asher-dev / promises_primer.md
Last active December 7, 2018 20:25
Promises in ES6, for asynchronous sequences

A Primer on Promises in ES6

(Post was originally written as a response to a question about how to resolve "callback hell" when asynchronous calls need to be executed sequentially.)

A popular system for managing sequential asynchronous operations, called "Promises", was integrated into ES6 (the latest release version of ECMAScript/Javascript). It's definitely a good idea to make sure you understand basic callbacks before diving into the Promises API, but it can really streamline asynchronous sequences once your callbacks start to get hellish.

It's based on an abstract data type called a "thenable". The basic premise is pretty simple -- a "promise" contains an operation that will be executed asynchronously, and has a .then() method which can be used to specify an operation to follow once the promise is fulfilled. You can create a new promise like this:

// Promise.new() creates a new promise.  It accepts a function as an argument.