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
# 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
-- 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
> foldr (-) 0 [3, 2, 1] | |
2 | |
> (3 - (2 - (1 - 0))) | |
2 | |
> foldl (-) 0 [3, 2, 1] | |
-6 | |
> (((0 - 3) - 2) - 1) |
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 | |
/** | |
Returns a compacted array, with repeated values coalesced. | |
Example: [a, a, b, c, c, c] yields [(a, 2), (b, 1), (c, 3)] | |
*/ | |
func compressArray<T: Comparable>(input: [T]) -> [(T, Int)] { | |
// TODO - implement this function | |
return [] | |
} |
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
// | |
// main.swift | |
// HigherOrderFunctions | |
// | |
// Created by Joshua Smith on 12/6/15. | |
// Copyright © 2015 iJoshSmith. All rights reserved. | |
// | |
/* | |
This file contains simple implementations of several |
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 Dictionary where Value: Equatable { | |
/// Returns all keys mapped to the specified value. | |
/// ``` | |
/// let dict = ["A": 1, "B": 2, "C": 3] | |
/// let keys = dict.keysForValue(2) | |
/// assert(keys == ["B"]) | |
/// assert(dict["B"] == 2) | |
/// ``` | |
func keysForValue(value: Value) -> [Key] { | |
return flatMap { (key: Key, val: Value) -> Key? in |
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
Dy MxT MnT AvT HDDay AvDP 1HrP TPcpn WxType PDir AvSp Dir MxS SkyC MxR MnR AvSLP | |
1 88 59 74 53.8 0.00 F 280 9.6 270 17 1.6 93 23 1004.5 | |
2 79 63 71 46.5 0.00 330 8.7 340 23 3.3 70 28 1004.5 | |
3 77 55 66 39.6 0.00 350 5.0 350 9 2.8 59 24 1016.8 | |
4 77 59 68 51.1 0.00 110 9.1 130 12 8.6 62 40 1021.1 | |
5 90 66 78 68.3 0.00 TFH 220 8.3 260 12 6.9 84 55 1014.4 | |
6 81 61 71 63.7 0.00 RFH 030 6.2 030 13 9.7 93 60 1012.7 | |
7 73 57 65 53.0 0.00 RF 050 9.5 050 17 5.3 90 48 1021.8 | |
8 75 54 65 50.0 0.00 FH 160 4.2 150 10 2.6 93 41 1026.3 |
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
func findAnagramsIn(words: [String]) -> [[String]] { | |
var signatureToAnagrams = [String: [String]]() | |
for word in words { | |
let signature = String(word.characters.sort()) | |
if let anagrams = signatureToAnagrams[signature] { | |
signatureToAnagrams[signature] = anagrams + [word] | |
} | |
else { | |
signatureToAnagrams[signature] = [word] | |
} |
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
/// Represents an entire Morse encoded message. | |
struct EncodedMessage { let encodedTerms: [EncodedTerm] } | |
/// Represents a word or number consisting of Morse code symbols. | |
struct EncodedTerm { let symbols: [Symbol] } | |
/// Represents a character encoded with Morse code marks. | |
struct Symbol { let marks: [Mark] } | |
/// Represents an individual component of a Morse code symbol. |