Skip to content

Instantly share code, notes, and snippets.

// 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) {
@gaearon
gaearon / slim-redux.js
Last active March 25, 2024 19:12
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
@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)
@mcmurrym
mcmurrym / MirrorDebugDecscription.swift
Last active December 24, 2019 05:59
A default protocol implementation for CustomDebugStringConvertible that uses Mirror for introspection
public extension CustomDebugStringConvertible {
var debugDescription: String {
return debugDescription()
}
func debugDescription(_ indentationLevel: Int = 0, includeType: Bool = true) -> String {
let indentString = (0..<indentationLevel).reduce("") { tabs, _ in tabs + "\t" }
var s: String
@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.
@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
@lattner
lattner / TaskConcurrencyManifesto.md
Last active March 26, 2024 00:33
Swift Concurrency Manifesto
@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
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active March 11, 2024 22:57
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse