Skip to content

Instantly share code, notes, and snippets.

View jarsen's full-sized avatar

Jason Larsen jarsen

View GitHub Profile
@jarsen
jarsen / ScoreKeeper.swift
Last active September 6, 2019 02:59
example of copy constructor with default values for immutability
struct Scorekeeper {
let runningScore: Int
let climbingScore: Int
let bikingScore: Int
let swimmingScore: Int
init(runningScore: Int = 0, climbingScore: Int = 0, bikingScore: Int = 0, swimmingScore: Int = 0) {
self.runningScore = runningScore
self.climbingScore = climbingScore
self.bikingScore = bikingScore
defmodule MarkovChain do
def create_chain(source, ngram_length) do
source
|> Stream.chunk_every(ngram_length + 1, 1)
|> Stream.map(fn chunk -> Enum.split(chunk, ngram_length) end)
|> Stream.scan(%{}, fn {ngram, word}, acc ->
Map.update(acc, ngram, word, fn val -> val ++ word end)
end)
end
#!/usr/local/bin/ruby
# crawler.rb
# by: Jason Larsen
# a generic web crawler that allows the user to do whatever they want by passing blocks
# @version 0.7
# 14 Dec 2009
# 0.6 things seem to be working well
# 0.7 modified so that URL's being added to the queue truncate fragments,
# this should save a lot of work
@jarsen
jarsen / NSArrayMagic.m
Last active July 24, 2018 09:22
How to do lots of cool things with NSArray. Inspired by NSHipster and WWDC 2013 Session 228 - "Hidden Gems in Cocoa and Cocoa Touch"
NSArray *albums = @[[Album albumWithName:@"Random Access Memories" price:9.99f],
[Album albumWithName:@"Clarity" price:6.99f],
[Album albumWithName:@"Weekend in America" price:7.99f],
[Album albumWithName:@"Weekend in America" price:7.90f],
[Album albumWithName:@"Bangarang EP" price:2.99f]];
// Reversing an Array
__unused NSArray *reversed = albums.reverseObjectEnumerator.allObjects;
// PREDICATES
@jarsen
jarsen / JaSONObject.swift
Last active November 10, 2017 14:49
JaSON Playground
import Foundation
//
// MARK: - JSONError Type
//
public enum JSONError: ErrorType, CustomStringConvertible {
case KeyNotFound(key: String)
case NullValue(key: String)
case TypeMismatch(expected: Any, actual: Any)

Keybase proof

I hereby claim:

  • I am jarsen on github.
  • I am jarsen (https://keybase.io/jarsen) on keybase.
  • I have a public key ASA-ZqyUHPk5dr-lESy6TrMX4qO5hFqdGOKjBBqrP7qaqQo

To claim this, I am signing this object:

@jarsen
jarsen / Debuggable.swift
Last active August 3, 2017 21:16
conform to this protocol and get a debugPrint for free
protocol Debuggable {
var debug: Bool { get set }
func debugPrint(message: String)
}
extension Debuggable {
func debugPrint(message: String) {
if debug {
print(message)
}
//: Playground - noun: a place where people can play
import UIKit
enum JSONError : ErrorType {
case NoValueForKey(String)
case TypeMismatch
}
public class JSONObject {
@jarsen
jarsen / JaSON.swift
Last active November 10, 2016 00:14
JSON Value Extraction in Swift. Blog post here http://jasonlarsen.me/2015/10/16/no-magic-json-pt3.html
import Foundation
//
// MARK: - JSONError Type
//
public enum JSONError: ErrorType, CustomStringConvertible {
case KeyNotFound(key: JSONKeyType)
case NullValue(key: JSONKeyType)
case TypeMismatch(expected: Any, actual: Any)
@jarsen
jarsen / regression.swift
Created October 10, 2014 20:24
Univariate Linear Regression
import Cocoa
class CSVDoubleSequence: SequenceType {
typealias GeneratorType = IndexingGenerator<Array<Double>>
let path: String
let values: [Double]
init(path: String) {
self.path = path