This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GLView.h | |
// | |
// Created by Dave Kennedy on 11/09/2016. | |
// | |
// | |
#import <Cocoa/Cocoa.h> | |
#import <GLKit/GLKit.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def max (a, b): | |
c = a-b | |
k = (c >> 31) & 1 | |
return a - k * c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Swap two numbers without an intermediate variable. Still takes as many statement as with one though… | |
Consider a = 5, b = 10 | |
a = b0101 | |
b = b1010 | |
a = a ^ b -> b0101 ^ b1010 -> a = b1111 (15) | |
b = a ^ b -> b1111 ^ b1010 -> b = b0101 (5) | |
a = a ^ b -> b1111 ^ b0101 -> a = b1010 (10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sumProbabilities[U] (options: Seq[(Int, () => U)]): Int = { | |
options.map {case (p, f) => p}.sum | |
} | |
def roughly [U] (chance: Int) (f: => U) = chance -> (() => f) | |
def chancesOf [U] (options: (Int, () => U)*): () => Option[U] = { | |
type Chance = PartialFunction[Int,Option[U]] | |
val fallback: Chance = { | |
case x => None | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define LEFT(x) (2*(x)+1) | |
#define RIGHT(x) (2*(x)+2) | |
#define PARENT(x) (((x)-1)/2) | |
void swap (int* a, int i, int j) { | |
int t = a[i]; | |
a[i] = a[j]; | |
a[j] = t; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGContextRef fromImage(NSImage* image) | |
{ | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
NSSize size = [image size]; | |
CGContextRef contextRef = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); | |
NSGraphicsContext *graphicsContext = [NSGraphicsContext graphicsContextWithCGContext:contextRef flipped:NO]; | |
NSGraphicsContext* currentContext = [NSGraphicsContext currentContext]; | |
[NSGraphicsContext setCurrentContext:graphicsContext]; | |
[image drawInRect:NSMakeRect(0, 0, size.width, size.height)]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
using namespace std; | |
// From http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range | |
long random_at_most(long max) { | |
unsigned long | |
// max <= RAND_MAX < ULONG_MAX, so this is okay. | |
num_bins = (unsigned long) max + 1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
import scala.util.Random | |
def shuffle (values: List[Int]): List[Int] = { | |
val r = new Random() | |
@tailrec | |
def shuffle0 (values: List[Int], accum: List[Int] = List.empty): List[Int] = values match { | |
case List(x) => x :: accum | |
case h :: t => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def from (i: Int): Stream[Int] = i #:: from(i+1) | |
val nats = from(0) | |
val fb = nats map { | |
n => | |
if (n % 15 == 0) "fizzbuzz" | |
else if (n % 3 == 0) "fizz" | |
else if (n % 5 == 0) "buzz" | |
else n.toString | |
} | |
fb.take(20) foreach println |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.davepkennedy | |
import scala.annotation.tailrec | |
object ThreeSum { | |
def parseInt (s: String): Option[Int] = try { | |
Some (java.lang.Integer.parseInt(s)) | |
} catch { | |
case e: NumberFormatException => None | |
} |