Automatically restart nodejs when your server.js file changes

Developing your NodeJS app and tired of manually restarting node when your server.js changes? Nodemon is a nice npm package that offers a solution.

Simply install nodemon by executing

npm install -g nodemon

Then instead of using node to run your script, use nodemon:

nodemon server.js

That’s it!

There is another (more DIY) solution that uses the inotify-tools Linux package and also works well.
First install inotify-tools:

sudo apt-get install inotify-tools

Then use this command

while :; do pkill -f "node server.js xibxor"; node server.js xibxor& inotifywait -e close_write ./; done

Anytime a file in the current directory changes, node will be restarted!

Cheers!



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

NodeJS colorized text

Been working on a tool in nodejs and needed a way to easily read the massive debug information that was shooting out.
Bash supports colorized text so I made a random colored string function. Check it out:

function strRandomColor(s){
	return '\x1B[' +
		Math.floor(91 + Math.random() * 6) +
		"m" +
		s +
		'\x1B[39m';
}

Usage:

console.log(strRandomColor("This text will be a random color!"));


▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

GLKView auto-rotating and you’re getting a EXC_BAD_ACCESS in glrGetPrivateInteger ?

0x31b5c75a:  bl     0x31b67e0c                ; sgxReadTextureData
0x31b5c75e:  b      0x31b5c7f0                ; glrGetPrivateInteger + 368
0x31b5c760:  ldr    r0, [r5, #24]
0x31b5c762:  cbz    r0, 0x31b5c744            ; glrGetPrivateInteger + 196
0x31b5c764:  ldr    r0, [r0, #4]
0x31b5c766:  ldr    r0, [r0]
0x31b5c768:  cbz    r0, 0x31b5c744            ; glrGetPrivateInteger + 196

You are probably using a CIContext like me, to do rendering with the EAGLContext.

The fix is to dispose of the current CIContext, and create a new one. You’ll also need to do this when the application resumes.

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    ciContext = [CIContext contextWithEAGLContext:glkView.context options:@{kCIContextWorkingColorSpace: [NSNull null]}];
}

- (void) applicationDidBecomeActive {
    ciContext = [CIContext contextWithEAGLContext:glkView.context options:@{kCIContextWorkingColorSpace: [NSNull null]}];
}


▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

Writing a fairly complex iOS app and getting this error?

dyld: Library not loaded: /usr/local/lib/lib*****.*.dylib
  Referenced from: /var/mobile/Applications/B289CFC8-853B-426B-BAC9-E5ADDFF2E151/App.app/App
  Reason: image not found

The app store/iOS platform does not allow loading of dylibs from an iOS app.
Even bundling the dylib will not help here.

Xcode searches for dylibs and uses them with a higher priority than it would use a static library file (lib.a)

Solution: Run a

rm `find ./ -name *.dylib`

over your third-party libs and recompile. Because the dylibs are now gone, your app will be statically linked to the lib.a files instead.



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

Non-Coalescing OR operator for Objective C

In a previous post of mine, I talked about how javascript and some other languages have an OR operator that doesn’t cast to a boolean.
The non-coalescing operator is extremely useful for setting default text. For example in JS:

textField.text = username.value || fullName.value || "No Name Entered";

In C#, the operator is ??, the Null Coalescing Operator
In PHP and C (only certain extensions though) the operator is ?:, the shorthand ternary operator. This was introduced into PHP in version 5.3.
I am not sure when GCC got the extension.

To get similar behavior to ECMAScript, one could do, int value = x ?: y ?: z ?: a ?: b ?: c;
This only works with arithmetic types in C, and strings/numbers which fall under primitives in PHP.
We still need a solution if we want to do something similar for objects/strings in C.

A reader informed me that my previous implementation does not short-circuit like the native operator would.

I am all about performant code, and I failed to provide that. I apologize to anyone who might be using that implementation. Short-circuiting is a huge performance gain and should be a part of an elegant functional OR macro.

So I went back to the drawing board. I looked into recursive variadic macros and recursive variadic C++ templates. I needed to be able to take any number of arguments, as well as varied types. The technique listed here for compile-time Factorial, Template metaprogramming, looked interesting. I tried to replicate the compile-time struct{} declaration but it turns out that a template’s parameters cannot take run-time values, only compile-time consts. The only way to receive non-const values would be in the function part of the template, which would kill short-circuiting.

After ruling out templates as a fruitful approach, I looked back into macros. Macros can’t recurse in C. Even if they have a fixed point, they still can’t recurse, C doesn’t support it. Instead you have to define MACRO_1, MACRO_2, … MACRO_N, in order to do pseudo-recursion. Quite ugly as you can see here: http://stackoverflow.com/a/5048661

Boost provides a nice way around this. With BOOST_PP_SEQ_FOLD, we can pass an initial state as an argument and use a sequence to layer over that initial state until the sequence is consumed.

By using BOOST_PP_SEQ_FOLD_RIGHT, we can make OR(a,b,c) turn into (isEmpty(a) ? (isEmpty(b) ? c : b) : a)
The BOOST_PP_SEQ_FOLD_RIGHT hands us the step, state, and current sequence element. The macro we pass to BOOST_PP_SEQ_FOLD_RIGHT to run over our argument sequence is:

#define __OR(s, state, obj) (isEmpty(obj) ? state : obj)

We fold from the right instead of the left, because the right-most argument shouldn’t be checked for empty, and it needs to be the deepest-nested.
With an isEmpty function overloaded for the many different primtive/object types, and a generic one for the id type, we get the code below:

#define BOOST_PP_VARIADICS 1
#include <boost/preprocessor/seq.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
 
#define __OR(s, state, obj) (isEmpty(obj) ? state : obj)
#define _OR(seq) BOOST_PP_SEQ_FOLD_RIGHT(__OR, BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(seq)),seq), BOOST_PP_SEQ_POP_BACK(seq))
#define OR(...) _OR(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
 
 
 
 
/* 
 We define a slew of overloaded isEmpty functions for cases where we know the type at compile-time
 If the type cannot be deduced at compile time, the slower isEmpty(id) is used
 You should cast your id types if you know them for better performance
 */
 
extern inline BOOL isEmpty(const char*         s) { return   !strlen(s); }
extern inline BOOL isEmpty(const long          x) { return           !x; }
extern inline BOOL isEmpty(const int           x) { return           !x; }
extern inline BOOL isEmpty(const short         x) { return           !x; }
extern inline BOOL isEmpty(const char          x) { return           !x; }
extern inline BOOL isEmpty(const double        x) { return           !x; }
extern inline BOOL isEmpty(const float         x) { return           !x; }
extern inline BOOL isEmpty(const NSNumber*     o) { return !o.boolValue; }
extern inline BOOL isEmpty(const NSString*     o) { return    !o.length; }
extern inline BOOL isEmpty(const NSData*       o) { return    !o.length; }
extern inline BOOL isEmpty(const NSSet*        o) { return     !o.count; }
extern inline BOOL isEmpty(const NSArray*      o) { return     !o.count; }
extern inline BOOL isEmpty(const NSDictionary* o) { return     !o.count; }
extern inline BOOL isEmpty(const NSNull*       o) { return          YES; }
extern inline BOOL isEmpty(const id            o) {
    return o == nil
    ||     o == [NSNull null]
    ||   ([o respondsToSelector:@selector(boolValue)]
          && ![o boolValue])
    ||   ([o respondsToSelector:@selector(length)]
          && ![o length])
    ||   ([o respondsToSelector:@selector(count)]
          && ![o count]);
}

Here’s how the macro is used:

NSLog(@"%@", OR(nil,@"", @0, @{@"foo": @"bar"}, @[]));
// outputs {foo = bar;}

NSLog(@"%@", OR(nil,@"", @0, @{}, @[]));
// outputs ()

NSLog(@"%@", OR(nil,@"", @0, @[], @"", @{}));
// outputs {}

NSLog(@"%@", OR(nil,@"", @[], @"", nil));
// outputs (null)

NSLog(@"%@", OR(nil,@"", @[], @"", [NSNull null]));
// outputs <null>

NSLog(@"%@", OR(nil,[NSNull null], @[], @"foo", @"", @"bar"));
// outputs "foo"

NSLog(@"%i", OR(0, 7, 9, 2));
// outputs 7

NSLog(@"%f", (float) OR(0, 0.0, 9, 2));
// outputs 9.0

int x = 10;
NSLog(@"%f", (float) OR(false, x, 5, 2));
// outputs 10.0

NSLog(@"%s", OR("", "foo", "bar"));
// outputs foo

As usual all the code above is public domain and free to use.

Boost is required for the macro.

Download XXOR.h here



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

Asynchronous Block Operations

NSBlockOperation’s blockOperationWithBlock method is immensely powerful. It allows us to create a synchronous operation from a block. However, it does not give us the ability to create an asynchronous operation.

If we need an asynchronous operation for a small task, it might be tempting to subclass NSOperation. Usually though, subclassing NSOperation is overkill.

I cooked up a class called AsyncBlockOperation. It’s very similar to NSBlockOperation with one caveat; the block you pass to it receives one argument, a reference to the AsyncBlockOperation for the block. When your block is done with its task, it must call [op complete]

Here’s an example of its use:

NSOperation* asyncOp = [AsyncBlockOperation blockOperationWithBlock:^(AsyncBlockOperation* op) {
    NSLog(@"starting op");
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (op.isCancelled){
            return [op complete];
        }
        
        NSLog(@"2 seconds have passed");
        NSLog(@"calling [op complete]");
        [op complete];
    });
    NSLog(@"op block returns but op isn't finished");
}];
asyncOp.completionBlock = ^{
    NSLog(@"completion block called!");
};
[asyncOp start];

The output will be:

2013-04-29 22:51:57.417 Experiments[5795:11603] starting op
2013-04-29 22:51:57.419 Experiments[5795:11603] op block returns but op isn't finished
2013-04-29 22:51:59.420 Experiments[5795:11603] 2 seconds have passed
2013-04-29 22:51:59.421 Experiments[5795:11603] calling [op complete]
2013-04-29 22:51:59.422 Experiments[5795:12e03] completion block called!

Get AsyncBlockOperation on Github



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

Functional OR in PHP and Obj C

Javascript’s OR operator, ||, is extremely convenient. It returns the first true value, otherwise the last false value.

That is, (3 || 0 || 1) would evaluate to 3.
And (0 || 0 || null) would evaluate to null

An OR operator that doesn’t coalesce its return value into a boolean is extremely useful for things like default text.
You might want to do something like this: UsernameField.value = (username || "No Username");

Javascript can get away with the dynamic behavior of the OR operator quite easily because it has the concept of object falseness for some objects like empty strings. In stronger-typed languages, object references are true if not pointing to nil.
In strongly typed compiled languages like C, the OR operator evaluates to a boolean because otherwise it would have to be overloaded and account for a ton of different types.

One would then wonder why a language like PHP, dynamically typed, with the concept of object falseness, fails to have this *extremely* convenient behavior?

I didn’t find one on the web, so I’ve implemented a non-coalescing OR for PHP. The name ‘OR’ is reserved in PHP, T_LOGICAL_OR, so I use _or instead. It’s a variadic function, meaning it takes a variable number of args.

function _or(){
  for ($i = 0, $n = func_num_args(); $i < $n; ++$i){
    $arg = func_get_arg($i);
    if ($arg) return $arg;
   

  return $arg;
}

PHP has a function called func_get_args() which returns an array of the arguments. I’m not sure if the internal storage of the arguments is an array, or there is some transmutation overhead in pulling them all out at once and doing a foreach.
I just used what is safer performance-wise, func_get_arg, fetching each one seperately.
If you’d like to do some performance benchmarks on func_get_arg vs func_get_args, that’d be awesome! Please post your findings in the comments. I am all about performance!

In newer versions of PHP > 5.3, PHP has the ?: operator
(“” ?: “2nd” ?: “3rd”) would evaluate to “2nd”
(“1st” ?: “2nd” ?: “3rd”) would evaluate to “1st”


On to Objective C…

Objective C is a bit harder to implement for. Everything is an Object that responds to messages, well except for primitives, structs, and pure data (we can ignore those.) It can be done with a little elbow grease.
Variadics, methods that take an indeterminate number of arguments, have to have their arguments nil terminated in the C family of languages. I suppose you can say PHP wins here..but hell, this might be the only time PHP wins so celebrate sparingly.
Nil-termination is problematic for us because we may want to pass a nil reference to our OR function and have it consider it ‘empty’. Instead, we will make our or function take a numArgs parameter, and create a macro to make it easy to call.

First we write an isEmpty function. By checking for nil references, count of 0, or length of 0, we should cover almost all objects that follow standard naming convention.

static inline BOOL isEmpty(id thing) {
    return thing == nil
    || thing == [NSNull null]
    || ([thing respondsToSelector:@selector(length)]
        && [thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [thing count] == 0);
}

Next we write our fairly simple variadic or function:

static inline id or(int numArgs, ...)
{
    id obj;
    va_list args;
    
    for (va_start(args, numArgs); numArgs; --numArgs)
        if (!isEmpty(obj = va_arg(args, id))) break;
    
    va_end(args);
    
    return obj;
}

To make it easier to call our ‘or’ function, without having to pass the argument count, we can make a macro.

#define __VA_NARGS__(...)  (sizeof((id[]){__VA_ARGS__})/sizeof(id))

#define OR(...) or(__VA_NARGS__(__VA_ARGS__), __VA_ARGS__)

__VA_NARGS__ is just a clever macro to get the argument count. We call `or` with the number of arguments, followed by the arguments. In C/ObjC there is no built-in macro for getting argument count at compile-time. __VA_NARGS__ is a clever savior that simply divides the size of a compile-time temporary id[] array by sizeof(id) to calculate the argument count.

Example usage of the functional or:

NSLog(@"%@", OR(nil,@"", @{@"foo": @"bar"}, @[]));
// outputs {foo = bar;}

NSLog(@"%@", OR(nil,@"", @{}, @[]));
// outputs []

NSLog(@"%@", OR(nil,@"", @[], @"", @{}));
// outputs {}

NSLog(@"%@", OR(nil,@"", @[], @"", nil));
// outputs (null)

NSLog(@"%@", OR(nil,@"", @[], @"", [NSNull null]));
// outputs <null>

NSLog(@"%@", OR(nil,[NSNull null], @[], @"", @"", @"woot"));
// outputs "woot"

Functions like this are plenty fast when using the ‘inline’ function modifier, so feel free to use ‘OR’ even where performance is necessary.

As with all code I post, the above functions are public domain.

Download FunctionalOr from Github

EDIT (5/5/2013) Please see my newer, more efficient code for a functional OR macro in Objective C: Non-Coalescing OR operator for Objective C



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

Version-Specific Cache Files for an iOS / Mac App

Recently when making an app, I ran into a small problem. I needed to cache a large computation on the first run of my app, ie. dynamically generated texture data, but I wanted the ability to have the texture change in newer versions of the app when I adjust the algorithm.
The code to generate the texture data should only execute on the first launch of the app, and then get cached to a file. Upon user-update of the app, the cache should be cleared. I wasn’t sure if there was an easy way to do this. Fortunately, there is a very elegant way.

Your .app bundle will be replaced with every new update (unless you use some kind of .diff updates, I certainly don’t, and the app store doesn’t.)
Hence, your cached data that you store within it, will also be cleared and generated again on next app launch.

Here is the code I used to cache a file to the .app bundle on iOS:

NSString* texturePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/texture.dat"];
NSData* textureData = [[NSData alloc] initWithContentsOfFile:texturePath];
if (!textureData){
    // generate texture data here
    [textureData writeToFile:texturePath atomically:YES];
}
// do something with textureData

Have fun!



▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

When to use Block Enumeration

Block enumeration is slighty slower than using a for loop.

// slightly slower than for loop
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  // equivalent to using 'break' in a for loop
  if (..condition..) *stop = YES;
  //..code..
}];

// slightly faster than block enumeration
for (id val in myArray){
  if (..condition..) break;
  //..code..
}

Why use block enumeration at all then?
The block can be stored and re-used for later enumeration, so that’s one benefit.

void(^myEnumerationBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger idx, BOOL *stop) {
  //..code..
};

[myArray enumerateObjectsUsingBlock:myEnumerationBlock];

Another benefit is concurrent enumeration.

[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  //..code..
}];


▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

The power of NSPredicate

In Objective C, we can use NSPredicate to filter an array in advanced ways.
A simple use is filtering an array.

Below we get all strings from an array with a length greater 3 characters.


NSString* myStrings = [@[string1, string2, string3] filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"length > 3"]
];

If we just had a single string to check, we can use evaluateWithObject.
Let’s suppose we want to verify that the input the user entered is only letters and at least 3 characters long.

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^(?i)[a-z]{3,}$'"];
[predicate evaluateWithObject:@"abc123"]; // returns FALSE
[predicate evaluateWithObject:@"ab"]; // returns FALSE
[predicate evaluateWithObject:@"abcdef"]; // returns TRUE


▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓