Skip to content

Instantly share code, notes, and snippets.

@ijoshsmith
ijoshsmith / zip.hs
Created April 22, 2015 03:54
Haskell zip function
-- Zips together two lists in Haskell.
zip' :: [a] -> [b] -> [(a, b)]
zip' xs [] = []
zip' [] ys = []
zip' (x:xs) (y:ys) = (x, y) : zip' xs ys
-- Example usages:
-- zip' "ABCDE" [1,2,3] …yields… [('A',1),('B',2),('C',3)]
-- zip' "AB" [1,2,3,4,5] …yields… [('A',1),('B',2)]
-- zip' "ABC" [] …yields… []
@ijoshsmith
ijoshsmith / encode_list.exs
Created February 20, 2015 01:41
Encode a list using Elixir
# I wrote this after watching Dave Thomas review his implementation
# in this presentation: https://www.youtube.com/watch?v=5hDVftaPQwY
defmodule MyList do
def encode(list), do: _encode(list, [])
defp _encode([ a, a | tail], acc), do: _encode([ {a, 2} | tail], acc)
defp _encode([{a, n}, a | tail], acc), do: _encode([{a, n+1} | tail], acc)
defp _encode([{a, n}, b | tail], acc), do: _encode([ b | tail], [{a, n} | acc])
@ijoshsmith
ijoshsmith / break_dollar.exs
Created February 20, 2015 00:33
Count ways to break a dollar using Elixir
defmodule Money do
@silver_dollar 100
@half_dollar 50
@quarter 25
@dime 10
@nickel 5
@penny 1
def all_coins, do: [@silver_dollar, @half_dollar, @quarter, @dime, @nickel, @penny]
@ijoshsmith
ijoshsmith / fizzbuzz.exs
Created February 14, 2015 20:54
Elixir solution to the FizzBuzz test
# Elixir (v1.0.3) solution to the FizzBuzz test, defined as:
#
# Write a program that prints the numbers from 1 to 100. But for multiples of three print
# "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers
# which are multiples of both three and five print "FizzBuzz".
# Source: http://c2.com/cgi/wiki?FizzBuzzTest
#
# Inspired by programming exercises in the book 'Programming Elixir' by Dave Thomas.
defmodule FizzBuzz do
@ijoshsmith
ijoshsmith / childviewcontrollers.m
Created January 14, 2015 16:56
Adding and removing child view controllers
// These methods assume that `self` references a UIViewController instance.
- (void)loadChildViewController:(UIViewController *)viewController
{
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
- (void)unloadChildViewController:(UIViewController *)viewController
@ijoshsmith
ijoshsmith / main.swift
Last active August 29, 2015 14:04
Simplifying NSJSONSerialization in Swift
import Foundation
/** Outputs of the JSONObjectWithData function. */
enum JSONObjectWithDataResult
{
case Success(AnyObject)
case Failure(NSError)
}
/**
@ijoshsmith
ijoshsmith / main.swift
Last active August 29, 2015 14:04
Nil-coalescing Operator in Swift
//
// UPDATE: This gist was rendered obsolete as of
// Xcode 6 Beta 5, which introduced the nil
// coalescing operator in Swift proper (??).
//
/* Nil-coalescing operator */
infix operator !! {}
func !! <T> (
value: T?,
@ijoshsmith
ijoshsmith / marshalMadness.swift
Created July 9, 2014 18:17
Having some fun with the marshal operator in Swift
import UIKit
// The marshal operator is reviewed in this blog post…
// http://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/
// Don't ever do something this ridiculous in a real app!
func printMarshalOperator()
{
{p("M")}
@ijoshsmith
ijoshsmith / arrayToDict.swift
Last active November 1, 2019 05:08
Create Swift Dictionary from Array
/**
Creates a dictionary with an optional
entry for every element in an array.
*/
func toDictionary<E, K, V>(
array: [E],
transformer: (element: E) -> (key: K, value: V)?)
-> Dictionary<K, V>
{
return array.reduce([:]) {
@ijoshsmith
ijoshsmith / CrossJoin.swift
Last active January 2, 2019 22:59
Cross-joining two Swift arrays
extension Array
{
func crossJoin<E, R>(
array: [E],
joiner: (t: T, e: E) -> R?)
-> [R]
{
return arrayCrossJoin(self, array, joiner)
}
}