Skip to content

Instantly share code, notes, and snippets.

View k06a's full-sized avatar
🚀
DeFi dreamer

Anton Bukov k06a

🚀
DeFi dreamer
View GitHub Profile
@interface NSIndexPath (SharedIndexPath)
@end
@implementation NSIndexPath (SharedIndexPath)
+ (instancetype)sharedIndexPathForRow:(NSInteger)row inSection:(NSInteger)section
{
static NSIndexPath *arr[10][10] = {nil};
if (arr[row][section] == nil)
arr[row][section] = [NSIndexPath indexPathForRow:row inSection:section];
return arr[row][section];
}
func bind1<T1,V>(f: (T1)->V, t1: T1) -> ()->V
{
return { () -> V in f(t1) }
}
func bind1<T1,T2,V>(f: (T1,T2)->V, t1: T1) -> (T2)->V
{
return { t2 -> V in f(t1,t2) }
}
prefix func !<T>(f: (T)->Bool) -> (T)->Bool
{
return { !f($0) }
}
func &&<T>(f1: (T)->Bool, f2: (T)->Bool) -> (T)->Bool
{
return { f1($0) && f2($0) }
}
@k06a
k06a / SwiftHaskellElem
Created August 30, 2014 23:01
Swift implementation of Haskell elem function
func elem<T: Equatable>(item: @autoclosure ()->T) -> (@autoclosure ()->[T]) -> Bool {
return { (arr: @autoclosure ()->[T]) in find(arr(), item()) != nil }
}
let found = elem (2) ([1,2,3]) // Hope to avoid braces in future
Before iOS 8, you do this:
```
[backgroundViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
[backgroundViewController presentViewController:_myMoreAppsViewController animated:NO completion:nil];
```
in iOS 8, you have to do this:
```
backgroundViewController.providesPresentationContextTransitionStyle = YES;
backgroundController.definesPresentationContext = YES;
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
@k06a
k06a / gist:7daf00b7c72e3db15adc
Created February 1, 2015 01:02
Prime numbers integral by Eratosthenes
int64_t nn = 100000000;
vector<int> ip(nn,1);
size_t pos = 2;
ip[0] = 0;
ip[1] = 0;
while ((pos = find(ip.begin()+pos, ip.end(), 1) - ip.begin()) < nn) {
for (size_t j = pos*pos; j < ip.size(); j+=pos)
ip[j] = 0;
pos += 1 + (pos>2);
}
#import <UIKit/UIKit.h>
@interface TintedButton : UIButton
@end
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
@implementation NSControl (Subclasses)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
method_exchangeImplementations(class_getInstanceMethod([self class], @selector(initWithCoder:)),
@k06a
k06a / add.cpp
Created October 14, 2015 16:34
Add two decimal number stored in strings
string add(const string & s1, const string & s2)
{
int flag = 0;
string ret(std::max(s1.size(),s2.size())+1, '0');
for (int i = 0; i < ret.size(); i++) {
int v = (i < s1.size() ? s1[s1.size()-1-i]-'0' : 0)
+ (i < s2.size() ? s2[s2.size()-1-i]-'0' : 0) + flag;
flag = v/10;
v -= flag*10;
ret[ret.size()-1-i] = v+'0';
@k06a
k06a / RefString.cpp
Last active October 16, 2015 07:17
std::string substring reference without copying
struct RefString
{
RefString(const string & s, int i, int l) : s(s), i(i), l(l) {}
const char & operator [] (int x) const {
return s[i+x];
}
size_t length() const {
return l;