Skip to content

Instantly share code, notes, and snippets.

Chris Chares ChrisChares

Block or report user

Report or block ChrisChares

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View RecursiveBinarySearch.swift
func binarySearchRecursive<T: Comparable, E: RandomAccessCollection>(_ xs: E, target: T, start _start: E.Index? = nil, end _end: E.Index? = nil) -> E.Index? where E.Element == T {
// Base cases
guard !xs.isEmpty else { return nil; }
guard xs.count > 1 else {
return xs[xs.startIndex] == target ? xs.startIndex : nil
// Start with two indexes, optionally supplied
let startIndex = _start ?? xs.startIndex;
let endIndex = _end ?? xs.endIndex;
View IterativeBinarySearch.swift
func binarySearchIterative<T: Comparable, E: RandomAccessCollection>(_ xs: E, target: T) -> E.Index? where E.Element == T {
// Start with two indexes, one at the beginning
var startIndex = xs.startIndex;
// And one at the end
var endIndex = xs.index(before: xs.endIndex);
// We are done when are our indexes converge
while startIndex < endIndex {
// Find the midpoint between our current range
let midPoint = xs.index(startIndex, offsetBy: xs.distance(from: startIndex, to: endIndex) / 2);
let midValue = xs[midPoint];
ChrisChares /
Last active Jul 9, 2019
async/await with ES6 Generators & Promises

async/await with ES6 Generators & Promises

This vanilla ES6 function async allows code to yield (i.e. await) the asynchronous result of any Promise within. The usage is almost identical to ES7's async/await keywords.

async/await control flow is promising because it allows the programmer to reason linearly about complex asynchronous code. It also has the benefit of unifying traditionally disparate synchronous and asynchronous error handling code into one try/catch block.

This is expository code for the purpose of learning ES6. It is not 100% robust. If you want to use this style of code in the real world you might want to explore a well-tested library like co, task.js or use async/await with Babel. Also take a look at the official async/await draft section on desugaring.


  • node.js - 4.3.2+ (maybe earlier with
View Swift Operators.swift
infix operator %= {
associativity right
precedence 90
infix operator >= {
associativity none
precedence 130
ChrisChares / Runtime Language Localization
Last active Sep 16, 2019
Runtime language localization in Objective C
View Runtime Language Localization
//example of setting run time languages in obj-c
//note that this won't redraw the layout on language change, you have to do that manually
//based on this stack overflow answer:
#import <Foundation/Foundation.h>
#define RUNTIME_LANGUAGE_STORAGE @"runtimeLanguageStorage"
#define RunTimeLanguageEnglish @"en"
#define RunTimeLanguageSpanish @"es"
View GruntFile.js
shell: {
vars: {
command: 'source etc/vars'
dockerosx: {
command: 'docker-osx start'
fig: {
command: 'fig up -d'
#!/usr/bin/env bash
#Install NPM Globals
sudo npm install -g nodemon
sudo npm install -g bower
sudo npm install -g grunt-cli
sudo npm install -g mocha
#Get the Submodules
git submodule init
You can’t perform that action at this time.