Skip to content

Instantly share code, notes, and snippets.

@jarsen
jarsen / json.swift
Last active August 29, 2015 14:13
Parsing JSON Playground with Result type. Inspired by Haskell, Swiftz, and http://robots.thoughtbot.com/efficient-json-in-swift-with-functional-concepts-and-generics
// Playground - noun: a place where people can play
import Foundation
infix operator >>- { associativity left precedence 150 } // Bind
infix operator <^> { associativity left } // Functor's fmap (usually <$>)
infix operator <*> { associativity left } // Applicative's apply
public typealias JSON = AnyObject
public typealias JSONObject = Dictionary<String, JSON>
// Playground - noun: a place where people can play
import UIKit
import CoreGraphics
public enum Result<T> {
case Success(@autoclosure () -> T)
case Failure(String)
init(_ value:T) {
@rob-brown
rob-brown / RBNetworkActivityIndicatorManager.h
Created July 31, 2011 05:51
A threadsafe network activity indicator manager.
//
// RBNetworkActivityIndicatorManager.h
//
// Copyright (c) 2011 Robert Brown
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@rob-brown
rob-brown / RBSimpleSingleton.h
Created July 31, 2011 02:28
A singleton that can be safely subclassed to reduce code duplication.
//
// RBSimpleSingleton.h
//
// Copyright (c) 2011 Robert Brown
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@meh
meh / lol.exs
Created December 16, 2013 17:29
defmodule Fun do
def arity(fun) do
case :erlang.fun_info(fun, :arity) do
{ :arity, arity } ->
arity
end
end
def adapt!(fun, 0) do
fn -> fun.([]) end
@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)
@entone
entone / get_ip4_address.ex
Last active October 3, 2017 20:07
iterate over local network interfaces and find first non local ip4v address
def get_ipv4_address() do
:inet.getifaddrs()
|> elem(1)
|> Enum.find(fn {_interface, attr} ->
case attr |> Keyword.get_values(:addr) do
[] -> false
list -> list |> Enum.find(fn(addr) ->
case addr do
nil -> false
{127, 0, 0, 1} -> false
@rob-brown
rob-brown / Actor.swift
Last active March 8, 2018 14:15
Elixir-inspired concurrency primitives
//
// Actor.Swift
//
// Copyright (c) 2017 Robert Brown
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@0xced
0xced / NSData+CommonDigest.h
Created May 23, 2011 09:00
NSData+CommonDigest: The most elegant NSData category for cryptographic hash functions
/*
Licensed under the MIT License
Copyright (c) 2011 Cédric Luthi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@danielberkompas
danielberkompas / scheduler.ex
Created October 26, 2016 17:59
A simple mix task scheduler for Elixir apps
defmodule MyApp.Scheduler do
@moduledoc """
Schedules a Mix task to be run at a given interval in milliseconds.
## Options
- `:task`: The name of the Mix task to run.
- `:args`: A list of arguments to pass to the Mix task's `run/1` function.
- `:interval`: The time interval in millisconds to rerun the task.