Skip to content

Instantly share code, notes, and snippets.

View pr1001's full-sized avatar

Peter Robinett pr1001

View GitHub Profile
@pr1001
pr1001 / objc.h
Last active February 26, 2016 15:40
Objective C Property and Protocol Problems in Swift
@protocol MyProtocol
- (void)myValue;
@end
@interface MyClass
@property myValue;
@pr1001
pr1001 / README.md
Created February 5, 2015 05:07
Programmatically calling segues without implementing `prepareForSegue:sender:`

UIViewController+SegueBlocks

This is a category which adds methods on UIViewController to provide a handler at the spot that you programmatically perform a segue. This avoids a mess of if, else if, else statements in prepareForSegue:sender:. In fact, you don't even need to implement the method at all if you don't need do anything before the segues called from actions in your storyboard.

Usage

NSNumber *blogPostID = @1;
[self performSegueWithIdentifier:@"BlogPost" handler:^(BlogPostVC *blogPostVC) {
    blogPostVC.blogPostID = blogPostID;

}];

@pr1001
pr1001 / composer.json
Created February 22, 2012 00:05
Composer autoloading issue
{
"name": "bubblefoundry/Request",
"type": "library",
"description": "Request is a simple webapp framework that focusing on routing HTTP requests and returing responses.",
"keywords": ["http", "request", "response", "route"],
"authors": [{
"name": "Peter Robinett",
"email": "peter@bubblefoundry.com",
"homepage": "http://www.bubblefoundry.com"
}],
@pr1001
pr1001 / gist:1664232
Created January 23, 2012 16:55
How to get Enumeration values back when using Scala parser combinators.
// ISO 3166-1 alpha-2 two letter country codes from http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
object Country extends Enumeration {
val Andorra = Value("ad") // Andorra
val UAE = Value("ae") // United Arab Emirates
val Afghanistan = Value("af") // Afghanistan
// ...
}
trait CountryParser extends JavaTokenParsers {
// would love to get rid of the silly regex
@pr1001
pr1001 / README.md
Created November 20, 2011 15:29
Immutable Javascript object properties

Wish you had some immutability in Javascript? Now you can!

var t = {a: 1, b: 2}.immutable();
console.log(t.a, t.b);
try {
  t.a = 3; // ->  Uncaught Error: a is immutable and cannot be modified.
} catch (e) {
  console.log(e);
}

var t2 = t.copy({a: 3});

@pr1001
pr1001 / Nat.scala
Created November 14, 2011 00:33
Some experiments with natural numbers in Scala _without_ type class wizardry. As long at you use Nat() you are guaranteed a non-negative natural number.
object Nat {
object Zero extends Nat(0)
class Nat(val number: Int) {
def to(n: Nat): Option[IndexedSeq[Nat]] = if(number <= n.number) Some((number to n.number).flatMap(apply(_))) else None
def +(n: Nat): Nat = new Nat(number + n.number)
def +(n: Int): Option[Nat] = this.+(apply(n))
def +(n: Option[Nat]): Option[Nat] = n.map(this + _)
@pr1001
pr1001 / Conditional.scala
Created November 12, 2011 13:34
Ruby-style trailing conditionals in Scala
class Conditional[T](left: => T) {
def iff(right: => Boolean): Option[T] = if (right) Some(left) else None
def unless(right: => Boolean): Option[T] = if (!right) Some(left) else None
}
implicit def any2Unless[T](left: => T) = new Conditional(left)
@pr1001
pr1001 / gist:1351495
Created November 9, 2011 13:54
I need to tell the Dutch government how often I'm out of the country when I renew my permit. Luckily ScalaTime makes that easy to calculate.
import org.joda.time._
import org.scala_tools.time.Imports._
// do +1 day to be inclusive on start and end days
val intervals = List (
// One Day
new DateMidnight(2011, 1, 19) to (new DateMidnight(2011, 1, 19) + 1.day),
// Several Days
new DateMidnight(2011, 2, 5) to (new DateMidnight(2011, 2, 13) + 1.day)
)
@pr1001
pr1001 / gist:1331842
Created November 1, 2011 20:44
ivy.xml
<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="com.miogiro" module="util_2.9.1" revision="0.1" status="release" publication="20111101213155">
<description>
util
</description>
</info>
<configurations>
<conf name="compile" visibility="public" description=""/>
<conf name="runtime" visibility="public" description="" extends="compile"/>
@pr1001
pr1001 / gist:1331815
Created November 1, 2011 20:31
util.sbt
name := "util"
organization := "com.miogiro"
version := "0.1"
scalaVersion := "2.9.1"
crossScalaVersions := Seq("2.8.1", "2.9.1")