Skip to content

Instantly share code, notes, and snippets.

@ChrisChares
ChrisChares / AsyncAwaitGenerator.md
Last active September 30, 2022 13:26
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.

Compatibility

  • node.js - 4.3.2+ (maybe earlier with
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];
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;
infix operator %= {
associativity right
precedence 90
assignment
}
infix operator >= {
associativity none
precedence 130
}
@ChrisChares
ChrisChares / Runtime Language Localization
Last active September 16, 2019 23:29
Runtime language localization in Objective C
//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: http://stackoverflow.com/a/23395903/1226668
#import <Foundation/Foundation.h>
#define RUNTIME_LANGUAGE_STORAGE @"runtimeLanguageStorage"
#define RunTimeLanguageEnglish @"en"
#define RunTimeLanguageSpanish @"es"
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