Skip to content

Instantly share code, notes, and snippets.

Jason Larsen jarsen

Block or report user

Report or block jarsen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View markov_chain.ex
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
View keybase.md

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 Nov 10, 2017
JaSON Playground
View JaSONObject.swift
import Foundation
//
// MARK: - JSONError Type
//
public enum JSONError: ErrorType, CustomStringConvertible {
case KeyNotFound(key: String)
case NullValue(key: String)
case TypeMismatch(expected: Any, actual: Any)
View StoryboardInstantiable.swift
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")
View where.swift
import UIKit
enum MyError: ErrorType {
case Oooggh(String)
}
func errorProne() throws {
throw MyError.Oooggh("nope")
}
@jarsen
jarsen / JaSON.swift
Last active Nov 10, 2016
JSON Value Extraction in Swift. Blog post here http://jasonlarsen.me/2015/10/16/no-magic-json-pt3.html
View JaSON.swift
import Foundation
//
// MARK: - JSONError Type
//
public enum JSONError: ErrorType, CustomStringConvertible {
case KeyNotFound(key: JSONKeyType)
case NullValue(key: JSONKeyType)
case TypeMismatch(expected: Any, actual: Any)
View magiclessJarSONv3.swift
// MARK: - Keys
import Foundation
public protocol JSONKeyType {
var JSONKey: String { get }
}
extension String: JSONKeyType {
public var JSONKey: String {
View KeyboardNotifications.swift
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 Jul 13, 2015
attemptMap for Reactive Cocoa that deals with `throws`
View attemptMap.swift
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 Aug 3, 2017
conform to this protocol and get a debugPrint for free
View Debuggable.swift
protocol Debuggable {
var debug: Bool { get set }
func debugPrint(message: String)
}
extension Debuggable {
func debugPrint(message: String) {
if debug {
print(message)
}
You can’t perform that action at this time.