Skip to content

Instantly share code, notes, and snippets.

@advantis
advantis / gist:b96adefb75cc2a0c685a
Created July 6, 2014 10:42
Hybrid Luhn algorithm
func isLuhnValid(number: String) -> Bool {
let asciiOffset: UInt8 = 48
let digits = Array(number.utf8).reverse().map{$0 - asciiOffset}
let convert: (UInt8) -> (UInt8) = {
let n = $0 * 2
return n > 9 ? n - 9 : n
}
var sum: UInt8 = 0
@advantis
advantis / gist:bb1d3e71e1ec169dcb3c
Last active August 29, 2015 14:03
Functional Luhn algorithm
func isLuhnValid(number: String) -> Bool {
let asciiOffset: UInt8 = 48
let convert: (UInt8) -> (UInt8) = {
let n = $0 * 2
return n > 9 ? n - 9 : n
}
let digits = Array(number.utf8).reverse().map{$0 - asciiOffset}
let indices = Array(0..digits.count)
let evens = indices.filter{$0 & 1 == 0}.map{digits[$0]}
let odds = indices.filter{$0 & 1 == 1}.map{digits[$0]}.map(convert)
@advantis
advantis / gist:27e6e88f98f2728fa5a9
Created June 30, 2014 13:31
Swift null coalescing operator
operator infix || {}
@infix func ||<T>(lhs: T?, rhs: T) -> T {
return lhs ? lhs! : rhs
}
-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
--
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
@advantis
advantis / ADVTabViewController.h
Created May 27, 2014 12:58
Generic UITabBarController replacement
//
// Copyright © 2014 Yuri Kotov
//
#import <UIKit/UIKit.h>
@interface ADVTabViewController : UIViewController
@property (nonatomic) NSUInteger selectedIndex;
@property (nonatomic, copy) NSArray *viewControllers;
#ifndef NS_DESIGNATED_INITIALIZER
#if __has_attribute(objc_designated_initializer)
#define NS_DESIGNATED_INITIALIZER __attribute((objc_designated_initializer))
#else
#define NS_DESIGNATED_INITIALIZER
#endif
#endif
@advantis
advantis / ADVDiscreteSlider.h
Last active August 29, 2015 14:00
Discrete slider
//
// Copyright © 2014 Yuri Kotov
//
#import <UIKit/UIKit.h>
@interface ADVDiscreteSlider : UISlider
@property (nonatomic) float unitValue;
@advantis
advantis / NSObject+ADVDescription.h
Created February 7, 2014 13:40
Automagically generated object description
//
// Copyright © 2014 Yuri Kotov
//
#import <Foundation/Foundation.h>
@interface NSObject (ADVDescription)
- (NSString *) autoDescription;
@advantis
advantis / NSManagedObject+ADVCopying.h
Created November 25, 2013 14:34
NSManagedObject category for creating deep copy in another context
//
// Copyright © 2013 Yuri Kotov
//
#import <CoreData/CoreData.h>
@interface NSManagedObject (ADVCopying)
- (instancetype) adv_copyInContext:(NSManagedObjectContext *)context;
@advantis
advantis / NSData+ADVHexadecimalRepresentation.h
Created November 25, 2013 10:22
NSData category for generating hexadecimal string representation
//
// Copyright © 2012 Yuri Kotov
//
#import <Foundation/Foundation.h>
@interface NSData (ADVHexadecimalRepresentation)
- (NSString *) hexadecimalRepresentation;