Skip to content

Instantly share code, notes, and snippets.

@tapi
tapi / KVO.js
Created February 24, 2012 01:36
Quick and dirty Javascript KVO
/* Extends the Class Object Written by Resig. http://ejohn.org/blog/simple-javascript-inheritance/ */
Class.prototype.addObserver = function(/** String */ key, /** Object */ observer, /** Function */ method)
{
if (key !== null && key !== undefined &&
observer !== null && observer !== undefined &&
method !== null && method !== undefined)
{
eval("this._" + key + " = this." + key);
this.__defineGetter__(key, function()
@tapi
tapi / error extension
Created March 27, 2015 18:31
Wouldn't this work instead of a nil default?
extension NSError{
//...
class func
myParseError(description:String?="Parse failed.") -> NSError {
let info = [NSLocalizedDescriptionKey: description]
return self(
domain:myDomain,
code:MyErrorCodes.ParseError.rawValue,
userInfo:info)
@tapi
tapi / GroupBy.swift
Created February 6, 2015 14:24
A generic groupby function that should work similarily to ruby's
/**
Group all items in a collection according to the value returned by a block.
:param: collection The collection whose items you want to group.
:param: groupBlock A block that returns a key for that value to be grouped by.
:returns: Returns a dictionary whose keys are the values returned by the groupBlock.
*/
func groupBy<V, K : protocol<Hashable, Equatable>, C : _ExtensibleCollectionType where C.Generator.Element == V>(collection: C, groupBlock: (V) -> K) -> [K: [V]] {
typealias ValueGroup = [V]
### Keybase proof
I hereby claim:
* I am tapi on github.
* I am tapi (https://keybase.io/tapi) on keybase.
* I have a public key whose fingerprint is B8A2 FE8D A75F 3FD8 9114 77D5 E7E6 6A04 5D6C 1382
To claim this, I am signing this object:
@tapi
tapi / gist:9236422
Created February 26, 2014 19:17
This is the funkiest assignment syntax I have ever seen used in Objective-C
_imageView = ({
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.scrollView addSubview:imageView];
imageView;
});