Skip to content

Instantly share code, notes, and snippets.

View macu's full-sized avatar

Matt Cudmore macu

  • Halifax, NS
View GitHub Profile
@macu
macu / timeout.swift
Last active February 22, 2022 08:03
Timeout utility in Swift, with the ability to cancel a deferred callback.
import Foundation
/// Timeout wrapps a callback deferral that may be cancelled.
///
/// Usage:
/// Timeout(1.0) { println("1 second has passed.") }
///
class Timeout: NSObject
{
private var timer: NSTimer?
@macu
macu / if-var-scope-demo.go
Created January 12, 2015 16:40
I hadn't realized you could access variables declared in if statements beyond the first block following the expression. This enables lots of code cleanup :)
package main
import "fmt"
func fab() (int, error) {
return 0, nil
}
func main() {
if i, err := fab(); err != nil {
@macu
macu / star–and-moon.txt
Last active September 1, 2022 01:49
Star and Moon ASCII Text Art
★°..    . ☾°☆ .*●¸.   ★ °:. .•○°★ . * .     .
 ° .●.    °☾°☆ ¸.●.  ★  ★°☾☆¸.¸ ★ :. .•○°★ . * .
 .  ¸.  ° ¸.*●¸.    °☾° ¸.●¸.  ★ °:. .•° . * :. . ¸.
●¸   ★  ★☾°★.    . °☆ .●¸.   ★ °. •○°★ .
       * . ☾° ¸.*●¸    °☾°☆ .*¸.   ★ 
★°..    . ☾°☆ .*●¸.   ★ °:. .•○°★ . * .    . ° .
●.    °☾°☆ ¸.●.  ★  ★°☾☆¸.¸ ★ :. .•○°★ . * . .
 .  ¸.  ° ¸.*●¸.  °☾° ¸.●¸.  ★ °:. .•°
@macu
macu / HexDump.swift
Created November 14, 2014 19:03
Hex dump NSData to String
// Thanks http://www.swiftsoda.com/swift-coding/get-bytes-from-nsdata/
func hexDump(data: NSData) -> String {
var len = data.length
var s = NSMutableString(capacity: len*2)
var byteArray = [UInt8](count: len, repeatedValue: 0x0)
data.getBytes(&byteArray, length:len)
for v in byteArray {
s.appendFormat("%02x", v)
}
return s
@macu
macu / IntDigits.swift
Created November 13, 2014 13:43
Method in Swift for extracting the digits from an integer. (Negative integers not supported.)
extension Int {
/// Returns the digits of the number in the given base.
/// The array of digits is ordered from most to least significant.
func digits(base: Int = 10) -> [Int] {
if self < base {
return [self]
}
var n = self
var d: [Int] = []
class AtomicBoolean {
private var val: Byte = 0
/// Sets the value, and returns the previous value.
/// The test/set is an atomic operation.
func testAndSet(value: Bool) -> Bool {
if value {
return OSAtomicTestAndSet(0, &val)
} else {
@macu
macu / RWSync.swift
Last active August 29, 2015 14:05
Simple utility for executing synchronous code in concurrent read/write blocks. Thanks to "Mastering Grand Central Dispatch" from WWDC 2011: https://developer.apple.com/videos/wwdc/2011/?id=210
/// RWSync provides methods for executing read/write blocks through the Grand Central Dispatch.
/// Multiple reads may execute concurrently, but a write will block all other executions in the queue.
class RWSync {
private let queue: dispatch_queue_t
/// Instantiates RWSync with a new concurrent queue.
init() {
let queueName = "rwsync.\(mach_absolute_time())"
self.queue = dispatch_queue_create(queueName, DISPATCH_QUEUE_CONCURRENT)
@macu
macu / pluralize.js
Created August 11, 2014 19:19
Ember pluralize helper
import { Handlebars } from 'ember';
// Thanks for the inspiration: https://coderwall.com/p/ryo_3w
Handlebars.registerBoundHelper('pluralize', function(number, options) {
var phrase = options.hash.phrase || '{|s}';
return phrase.replace(/\{(.*?)\|(.*?)\}/, function(match, singular, plural) {
return number == 1 ? singular : plural;
});
});
@macu
macu / ember-model-clone.js
Last active August 29, 2015 14:03
Reopen the Model class, to allow cloning records with overrides.
import Ember from 'ember';
import DS from 'ember-data';
/**
* Returns the model class name, dasherized, which can be used to find
* and create new models of the same type through the store.
*/
export function getClassName(model) {
// typeKey introduced in Ember Data 1.0.0.beta.3
var typeKey = model.constructor.typeKey;
@macu
macu / ember-generate-properties.html
Created June 13, 2014 17:36
Example of dynamically generating properties on objects, demonstrating that the bindings work as expected.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>EmberJS demo</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ember.js/1.5.1/ember.min.js"></script>