Skip to content

Instantly share code, notes, and snippets.

View jarsen's full-sized avatar

Jason Larsen jarsen

View GitHub Profile
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

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 / 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)
protocol StoryboardInstantiable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
}
extension StoryboardInstantiable {
static func instantiateFromStoryboard() -> Self {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
guard let vc = storyboard.instantiateViewControllerWithIdentifier(storyboardIdentifier) as? Self else {
fatalError("Instantiated view controller does not match type")
import UIKit
enum MyError: ErrorType {
case Oooggh(String)
}
func errorProne() throws {
throw MyError.Oooggh("nope")
}
@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)
// MARK: - Keys
import Foundation
public protocol JSONKeyType {
var JSONKey: String { get }
}
extension String: JSONKeyType {
public var JSONKey: String {
@jarsen
jarsen / KeyboardNotifications.swift
Created July 16, 2015 17:14
keyboard notifications
func keyboardWillShow(notification: NSNotification) {
print("Showing keyboard")
guard let userInfo = notification.userInfo,
rectValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
rawCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else {
return
}
let keyboardFrame = rectValue.CGRectValue()
@jarsen
jarsen / attemptMap.swift
Created July 13, 2015 16:59
attemptMap for Reactive Cocoa that deals with `throws`
func attemptMap<T, U>(operation: T throws -> U) -> ReactiveCocoa.Signal<T, NSError> -> ReactiveCocoa.Signal<U, NSError> {
return { signal in
return Signal { observer in
signal.observe(next: { value in
do {
sendNext(observer, try operation(value))
}
catch {
sendError(observer, error as NSError)
@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)
}