Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / BigFactorial.java
Created May 1, 2014 10:53
Factorial with tail-call optimization in Java 8
// Code from “Functional Programming in Java, Chapter 7”.
import java.util.function.Function;
import java.util.stream.Stream;
import java.math.BigInteger;
@FunctionalInterface
interface TailCall<T> {
// Maybe a functional category on NSArray will help.
@interface NSArray(Extension)
-(NSArray*) map:(id(^)(id object))block;
@end
@implementation NSArray(Extension)
-(NSArray*) map:(id(^)(id object))block {
NSParameterAssert(block != NULL);
NSMutableArray *results = [[NSMutableArray alloc] initWithCapacity:[self count]];
@janodev
janodev / gist:4683470
Created January 31, 2013 15:06
14589365
#import <Foundation/Foundation.h>
typedef NS_ENUM(unsigned char, MyTreeVisitingOrder) {
MyTreeOrderDepthFirst,
MyTreeOrderValueFirst
};
#define Tree NSObject<MyTree>
@protocol MyTree
@janodev
janodev / Animal.h
Created February 16, 2013 02:27
Ways to declare Objective-C variables. See a commented version at http://stackoverflow.com/a/14906215/412916
@interface Animal : NSObject
{
NSObject *iProtected;
@package
NSObject *iPackage;
@private
NSObject *iPrivate;
@protected
NSObject *iProtected2; // default access. Only visible to subclasses.
@public
#import <Foundation/Foundation.h>
// clang -framework Foundation main.m -o main && ./main
int main(int argc, char *argv[]) {
@autoreleasepool {
NSUInteger (^__block factorial)(NSUInteger n);
NSUInteger (^__block __weak weakFactorial)(NSUInteger n);
weakFactorial = factorial = ^ NSUInteger (NSUInteger n) {
return n==0 ? 1 : n * weakFactorial(n-1);
};
@janodev
janodev / gist:5292003
Last active December 15, 2015 16:49 — forked from iamleeg/gist:5290797
This creates an Objective-C object in the stack. ARC code.
#import <Foundation/Foundation.h>
#include <stdlib.h>
#include <objc/runtime.h>
@interface A : NSObject
@property (assign) int meaning;
@end
@implementation A
@janodev
janodev / gist:5332644
Last active December 15, 2015 22:19
Dynamic GCD accessors with resolveInstanceMethod: and dispatch_barrier_async.
#include <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Hi : NSObject
@end
@implementation Hi {
NSString *something;
}
#!/bin/bash
# This script builds the iOS and Mac openSSL libraries
# It's expanded from https://github.com/st3fan/ios-openssl
set -xe
# Setup paths to stuff we need
OPENSSL_VERSION="1.0.1e"
@janodev
janodev / NSTimer+(BlockSupport).m
Last active December 17, 2015 07:48
NSTimer with safe release. Credits to Effective Objective-C 2.0.
#import <Foundation/Foundation.h>
@interface NSTimer (BlockSupport)
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats;
@end
@janodev
janodev / Person.h
Last active December 17, 2015 17:59
Different ways to execute a block property. This runs in CodeRunner. Replace one of the #ifdef X below with #ifdef YES, and comment person = nil;.
#import <Foundation/Foundation.h>
#undef X
typedef void (^salute_t)();
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) salute_t salute;