Skip to content

Instantly share code, notes, and snippets.

View nickmain's full-sized avatar

Nick Main nickmain

View GitHub Profile
@nickmain
nickmain / swipl.rkt
Created August 22, 2011 05:07
Calling SWI-Prolog from Racket via FFI
#lang racket
(require ffi/unsafe)
(require ffi/cvector)
(define swipl-lib (ffi-lib "/opt/local/lib/swipl-5.11.24/lib/i386-darwin10.8.0/libswipl"))
(define [swipl-func name type] (get-ffi-obj name swipl-lib type))
(define fid_t _pointer) ;;handle to a foreign frame
@nickmain
nickmain / gist:2048771
Created March 16, 2012 06:22
Receiving mouse events in Blub Prolog
sprite( X, Y ) :-
sprite(S),
S.x <- X, S.y <- Y,
paint(S,0x8888ff),
spawn(( repeat,
receive(S,mouseMove,E),
S.x <- E.stageX - 50,
S.y <- E.stageY - 30 )).
paint(Sprite,Color) :-
@nickmain
nickmain / wktest.rkt
Created March 11, 2013 19:24
Embedding WebKit using Racket Objective-C FFI
#lang racket/gui
(require framework)
(require ffi/unsafe)
(require ffi/unsafe/objc)
(ffi-lib "/System/Library/Frameworks/WebKit.framework/WebKit")
(import-class WebView)
(import-class NSURLRequest)
(import-class NSURL)
(import-class NSString)
@nickmain
nickmain / 8queens.html
Created May 15, 2013 19:04
8 queens problem, handwritten JS and Scheme with the intention of eventually being generated by a Prolog compiler written in Racket
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function makeContext() { return { choices:[], trail:[], halted:false }; }
// Push a new choice point onto the stack
function pushChoice( ctx, choiceThunk ) {
ctx.choices.unshift( { thunk:choiceThunk, trailIndex:ctx.trail.length } );
}
@nickmain
nickmain / gist:7269478
Created November 1, 2013 18:14
How can a meaningful unit test be written for this CLIPS rule ? The rule is declarative - it states the preconditions that must exist and is explicit about what the post-conditions are (the asserted facts). How can a unit test add value to this and not be merely a test that CLIPS itself actually works ?
(defrule make-group
(make-graphic ?sheet ?id)
(dict-entry ?id Class "Group")
(not (dict-entry ?id isSubgraph ?))
(dict-entry ?id Graphics ?array)
(array ?array $?graphics)
=>
(assert (group ?id))
(foreach ?graphic $?graphics
(assert (make-graphic ?sheet ?graphic))
@nickmain
nickmain / Testing optionals
Created June 14, 2014 22:36
Using pattern matching on tuples of Optionals
// Playground - noun: a place where people can play
import Cocoa
func checkOptionals(a:Dictionary<String,Int>,key1:String,key2:String) {
switch (a[key1],a[key2]) {
case (.Some(_),.Some(_)): println("both")
case (.Some(let a),.None): println("first " + String(a))
case (.None,.Some(let b)): println("second " + String(b))
default: println("neither")
#lang racket
(define (run-length n)
(let ([run-char #f]
[run-len 0])
(λ(c)
(if (equal? c run-char)
(set! run-len (+ run-len 1))
(begin (set! run-len 1)
(set! run-char c)))

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF

Instructions

To capture the video (filesize: 19MB), using the free "QuickTime Player" application:

@nickmain
nickmain / MiniHTTPServer.swift
Created April 11, 2016 03:28
Minimal Swift HTTP server for unit testing
//: Playground - noun: a place where people can play
import Foundation
class ServerSocket {
private var sockAddrLen = socklen_t(sizeof(sockaddr_in))
private var serverSocket = socket(AF_INET, SOCK_STREAM, 0)
init?(_ port: UInt16) {
var option: UInt32 = 1

1. Clone your fork:

git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream