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 | |
| indirect enum LinkedList<Element> { | |
| case node(element: Element, next: LinkedList) | |
| case end | |
| init<S: Sequence>(values: S, last: LinkedList = .end) where S.Element == Element { | |
| self = values.reversed().reduce(last, { .node(element: $1, next: $0) }) | |
| } | |
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
| public extension Dictionary { | |
| /// Returns a dictionary where the keys are the first elements of | |
| /// the tuple and the values are the second elements, grouped in | |
| /// arrays associated with the given key. | |
| /// | |
| /// let question = [(1, "Hello"), (2, "How"), (3, "Are"), (1, "You")] | |
| /// let g = Dictionary(grouping: question) | |
| /// // g == [1: ["Hello", "You"], 2: ["How"], 3: ["Are"]] | |
| /// | |
| /// - Returns: A dictionary containing the grouped elements of this sequence. |
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 | |
| public enum Either<Left, Right> { | |
| case left(Left) | |
| case right(Right) | |
| public var left: Left? { | |
| switch self { | |
| case let .left(value): | |
| return value |
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
| data TicketCategory = Infant | Child | Adult | |
| recurseTickets :: Int -> IO Double | |
| recurseTickets 0 = pure 0.0 | |
| recurseTickets n = do | |
| putStrLn "How old is the passenger?" | |
| age <- fmap read getLine :: IO Int | |
| let priceMultiplier = | |
| case age of | |
| x | x `elem` [0..4] -> 0 |
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 Signal { | |
| // Turn the producer from a Signal<Result<Value, Error>, NoError> into a Signal<Value, Error>. | |
| func liftError<T, E>() -> Signal<T, E> where Value == Result<T, E>, Error == NoError { | |
| return materialize() | |
| .map { event -> Signal<T, E>.Event in | |
| switch event { | |
| case let .value(.success(value)): | |
| return .value(value) | |
| case let .value(.failure(error)): | |
| return .failed(error) |
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
| #!/usr/bin/env ruby | |
| require 'yaml' | |
| require 'json' | |
| # You can either pipe in the data that this script expects or | |
| # The data expected by this script should be in the following format: | |
| # Request: | |
| # Method: GET | |
| # URL: https://example.com/example/url/slug |
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
| enum Plain {} | |
| enum Encoded {} | |
| enum Decompressed {} | |
| enum Compressed {} | |
| struct Bytes<A, B> { | |
| let string: String | |
| } | |
| extension Bytes where A == Plain, B == Decompressed { |
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
| // | |
| // RegexKit.swift | |
| // RegexKit | |
| // | |
| // Created by Charlotte Tortorella on 3/7/17. | |
| // Copyright © 2017 Monadic Consulting. All rights reserved. | |
| // | |
| import Foundation |
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 SignalProtocol where Value: OptionalProtocol { | |
| func errorOnNil(_ error: Error) -> Signal<Value.Wrapped, Error> { | |
| return attemptMap { | |
| switch $0.optional { | |
| case let unwrapped?: | |
| return .success(unwrapped) | |
| case nil: | |
| return .failure(error) | |
| } | |
| } |
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 Sequence { | |
| func map<U>(_ keyPath: KeyPath<Element, U>) -> [U] { | |
| return map { $0[keyPath: keyPath] } | |
| } | |
| func flatMap<U>(_ keyPath: KeyPath<Element, U?>) -> [U] { | |
| return flatMap { $0[keyPath: keyPath] } | |
| } | |
| func flatMap<U, S: Sequence>(_ keyPath: KeyPath<Element, S>) -> [U] where S.Iterator.Element == U { |