This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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… [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/** Outputs of the JSONObjectWithData function. */ | |
enum JSONObjectWithDataResult | |
{ | |
case Success(AnyObject) | |
case Failure(NSError) | |
} | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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?, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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([:]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array | |
{ | |
func crossJoin<E, R>( | |
array: [E], | |
joiner: (t: T, e: E) -> R?) | |
-> [R] | |
{ | |
return arrayCrossJoin(self, array, joiner) | |
} | |
} |