Skip to content

Instantly share code, notes, and snippets.

@tLewisII
tLewisII / TokenizerTest
Created September 15, 2013 12:40
A test of the execution time difference between Foundation and CoreFoundation for string tokenizing and counting.
///Using Foundation.
-(void)getWordsFromString:(NSString *)string withFrequency:(NSUInteger)frequency completion:(void(^)(NSArray *array))block {
NSDate *date = [NSDate date];
__block NSTimeInterval interval = 0.0;
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:1000];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if(!temp[substring]) {
temp[substring] = @1;
}
else {
@tLewisII
tLewisII / TJLGridTransitions.m
Created October 5, 2013 03:56
Grid ViewController Transitions
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return .7;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
CGRect finalFrame = [transitionContext finalFrameForViewController:toController];
UIView *containerView = [transitionContext containerView];
@tLewisII
tLewisII / Fibonacci.hs
Last active December 25, 2015 18:09
Fibonacci sequence generator in Haskell. Given an upper bounds, will return a list of all numbers in the sequence that are <= the upper bounds.
module Main where
loop :: Integer -> [Integer] -> Integer -> Integer -> [Integer]
loop upper list cur prev
| (cur == upper) = list ++ [cur]
| (cur > upper) = list
| otherwise = loop upper (list ++ [cur]) (cur + prev) cur
fibSeq :: Integer -> [Integer]
fibSeq 0 = [0]
fibSeq 1 = [0,1]
fibSeq n = loop n [0] 1 0
@tLewisII
tLewisII / Recursive.m
Last active December 25, 2015 22:39
Recursion in Objective-C
- (BOOL)isPowerOfTwo:(NSUInteger)number {
if(number == 0) return NO;
if(number == 1 || number == 2) return YES;
return [self findPower:number accumulator:2];
}
- (BOOL)findPower:(NSUInteger)n accumulator:(NSUInteger)accumulator {
if(accumulator == n) return YES;
if(accumulator > n) return NO;
return [self findPower:n accumulator:(2 * accumulator)];
@tLewisII
tLewisII / TJLAutoLayoutMacros.h
Created January 14, 2014 03:34
Auto layout macros, for shortening auto layout code.
#define NSConstraintMake(item, itemAttribute, relation, otherItem, otherAttribute, multiplier, constant) \
[NSLayoutConstraint constraintWithItem:item attribute:itemAttribute relatedBy:relatation toItem:otherItem attribute:otherAttribute multiplier:multiplier constant:constant] \
#define kLeading NSLayoutAttributeLeading
#define kTrailing NSLayoutAttributeTrailing
#define kTop NSLayoutAttributeTop
#define kBottom NSLayoutAttributeBottom
#define kCenterX NSLayoutAttributeCenterX
#define kCenterY NSLayoutAttributeCenterY
#define kLeft NSLayoutAttributeLeft
@tLewisII
tLewisII / NSPredicate+CHAdditions.m
Created January 17, 2014 22:34
static NSPredicate + NSString causing crash.
@implementation NSPredicate (CHAdditions)
+ (NSPredicate *)sharedValidEmailPredicate {
static NSPredicate *_sharedPredicate = nil;
static NSString *_emailRegEx = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_emailRegEx =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@tLewisII
tLewisII / TJLPhotoManger.m
Created January 27, 2014 20:12
Saving many photos to the camera roll.
- (void)saveImages:(NSArray *)images toGroup:(ALAssetsGroup *)group next:(void (^)(NSData *imageData))next error:(void (^)(NSError *))errorBlock completion:(void (^)())block {
dispatch_queue_t queue = dispatch_queue_create("com.photoShare.saveToCameraRoll", NULL);
[images enumerateObjectsUsingBlock:^(PPSAsset *obj, NSUInteger idx, BOOL *stop) {
dispatch_async(queue, ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[self.library writeImageDataToSavedPhotosAlbum:obj.fullImageData metadata:obj.metaData completionBlock:^(NSURL *assetURL, NSError *error) {
if(error) {
errorBlock(error);
}
(doseq [xml-content (:content xml)]
(spit file-name (:a-key xml-content))
(spit file-name (:another-key xml-content) :append true)
) ;;on last pass I need to spit an end marker
@tLewisII
tLewisII / TJLProgressView.m
Created March 16, 2014 00:06
Rotating UIProgressView
- (void)showVerticallyOnLeftSideOfView:(UIView *)parent havingNavigationBar:(BOOL)havingNavBar {
[parent addSubview:self];
CGFloat widthOffset = havingNavBar ? 64 : 0;
CGFloat x = CGRectGetWidth(parent.bounds) - (CGRectGetHeight(parent.bounds));
self.frame = (CGRect){
.origin.x = x,
.origin.y = CGRectGetMaxY(parent.bounds),
.size.width = CGRectGetHeight(parent.bounds) - widthOffset,
.size.height = 0
@tLewisII
tLewisII / FunctionComp.swift
Created June 11, 2014 12:56
Function composition in Swift
operator infix .. {
associativity right
}
func ..<A, B, C>(f: B -> C, g: A -> B) -> A -> C {
return { (a: A) -> C in
return f(g(a))
}
}