Skip to content

Instantly share code, notes, and snippets.

View pitiphong-p's full-sized avatar

Pitiphong Phongpattranont pitiphong-p

View GitHub Profile

Keybase proof

I hereby claim:

  • I am pitiphong-p on github.
  • I am pitiphong_p (https://keybase.io/pitiphong_p) on keybase.
  • I have a public key ASCYHTyMo48sHejIeUTR9ynIBNuobg7RegfPzZi9dzBmaQo

To claim this, I am signing this object:

@pitiphong-p
pitiphong-p / PTPKeyPathForSelectors.m
Created February 7, 2019 05:03
Creating a KeyPath from Selectors
inline NSString * PTPKeyPathForSelectors(SEL selector, ...) {
if (!selector) {
return nil;
}
NSMutableArray *selectors = [NSMutableArray array];
va_list args;
va_start(args, selector);
SEL arg = selector;
do {
@pitiphong-p
pitiphong-p / Grouping.swift
Created August 14, 2016 18:09
A simple method for grouping elements with given key calculating closure in Swift 3
extension Array {
func group<Key: Hashable>(by: (Element) -> Key) -> [Key: [Element]] {
var values = [Key: [Element]]()
let keys = self.map(by)
let pairs = zip(keys, self)
pairs.forEach { (key, value) in
var valuesOfKeys = values[key] ?? []
valuesOfKeys.append(value)
values[key] = valuesOfKeys
import Foundation
// See: https://developer.apple.com/swift/blog/?id=15
struct Error: ErrorType {
var _domain: String {return "com.sadun"}
var _code: Int {return 0}
}
enum MyError : ErrorType {
@pitiphong-p
pitiphong-p / gist:7447416
Last active December 28, 2015 05:09
NSIndexSet category to get the nth index in the index set. (0 based position)
@interface NSIndexSet (NthPosition)
- (NSUInteger)indexAtIndex_ppb:(NSUInteger)position;
@end
@implementation NSIndexSet (NthPosition)
- (NSUInteger)indexAtIndex_ppb:(NSUInteger)position {
__block NSUInteger index = NSNotFound;
if (position < self.count) {
__block NSUInteger count = 0;
[self enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {