Skip to content

Instantly share code, notes, and snippets.

View nbrandaleone's full-sized avatar

Nick Brandaleone nbrandaleone

View GitHub Profile
@nbrandaleone
nbrandaleone / swift-networking1.swift
Last active August 29, 2015 14:21
Swift Networking. Examples of NSURLSession and NSData(getContentsofURL:)
//: Playground - Swift networking examples
import Cocoa
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely() // this allows async requests to finish in the Playground
// Example of NSURLSession. This is the preferred way of creating web/socket conenctions for Apple programs
var request = NSMutableURLRequest(URL: NSURL(string: "http://www.google.com")!)
@nbrandaleone
nbrandaleone / fibonacci.swift
Last active August 29, 2015 14:21
Swift Fibonacci
let N = 30
var fib = [Int](count: N, repeatedValue: 1)
for i in 2..<N {
fib[i] = fib[i-1] + fib[i-2]
}
for number in fib {
println(number)
}
@nbrandaleone
nbrandaleone / GCD1.swift
Last active January 24, 2016 20:09
Swift background threads
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some task
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
}
# Create wrapper to execute blocks in bacground
func dispatch_to_main_queue(block: dispatch_block_t?) {
@nbrandaleone
nbrandaleone / max_array.swift
Created June 1, 2015 22:12
Finding the max value in an array (using Swift)
let nums = [1, 6, 3, 9, 4, 6];
let numMax = nums.reduce(Int.min) { max($0, $1) }
minElement(numbers) // 1
maxElement(numbers) // 9
@nbrandaleone
nbrandaleone / elixirLife.ex
Created June 18, 2015 18:09
Conway Game of Life, working in a text terminal screen. Solved using Elixir.
defmodule Life do
@docmodule """
This program simulates John Conway's "Game of Life"
on a small board of size 8x8 cells. The default board executes
a "glider" that runs from the top left to the bottom right of the board.
The board does not wrap around to the top/bottom or sides.
The primary data structure is a list of lists (8x8), which contains
the board representation. Coords are [row, col]: [0,0] ... [0,7]
... ...
@nbrandaleone
nbrandaleone / elixirSudoku.ex
Created June 18, 2015 18:16
Solves a fully constrained sudoku board. Each cell is a process that talks to other cells using mailboxes and channels.
defmodule SudokuSolver do
def cell(scheduler) do
send scheduler, { :ready, self }
cell_loop(scheduler, [], ["1","2","3","4","5","6","7","8","9"])
end
defp cell_loop(scheduler, loc, val) do
receive do
{ :loc, location } ->
@nbrandaleone
nbrandaleone / mandelbrot.clj
Last active August 29, 2015 14:23
Generate the Mandelbrot set using Clojure (black and white)
;;
;; Source code originally from "Clojure Programming" (2012) by Chas Emerick, Brian Carper and Christophe Grand
;;
(ns mandelbrot.core
(:import java.awt.image.BufferedImage
(java.awt Color RenderingHints)))
(defn- escape
"Returns an integer indicating how many iterations were required
before the value of z (using the components `a` and `b`) could
@nbrandaleone
nbrandaleone / primes.clj
Created June 18, 2015 18:38
A lazy sequence of prime numbers. Written in Clojure.
(ns primes.core)
;; Taken from clojures.contrib.lazy-seqs
; primes cannot be written efficiently as a function, because
; it needs to look back on the whole sequence. Contrast with
; fibs and power-of-2 which only need a fixed buffer of 1 or 2
; previous values.
;; primes - based on the \"naive\" implemention described in [1] plus a
; small \"wheel\" which eliminates multiples of 2, 3, 5, and
; 7 from consideration by incrementing past them. Also inspired
@nbrandaleone
nbrandaleone / clojureLife.clj
Last active August 29, 2015 14:23
Conway Game of Life. Written in Clojure.
;;
;; Source code originally from "Clojure Programming" (2012) by Chas Emerick, Brian Carper and Christophe Grand
;;
(ns gol.core
(:gen-class))
(defn neighbors
"find our cell neighbors"
[[x y]]
(for [dx [-1 0 1] dy [-1 0 1] :when (not= 0 dx dy)]
@nbrandaleone
nbrandaleone / SwiftLife.swift
Created June 18, 2015 19:02
Conway Game of Life, written in Swift (in a functional manner).
//
// ViewController.swift
// SwiftLife
//
// Created by Nick Brandaleone on 6/1/15.
// Copyright (c) 2015 Nick Brandaleone. All rights reserved.
//
import UIKit