Skip to content

Instantly share code, notes, and snippets.

View Qata's full-sized avatar

Charles Maria Tor Qata

View GitHub Profile
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) })
}
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.
@Qata
Qata / Either.swift
Last active February 28, 2020 03:57
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
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
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)
@Qata
Qata / request.rb
Last active January 23, 2018 00:11
#!/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
enum Plain {}
enum Encoded {}
enum Decompressed {}
enum Compressed {}
struct Bytes<A, B> {
let string: String
}
extension Bytes where A == Plain, B == Decompressed {
//
// RegexKit.swift
// RegexKit
//
// Created by Charlotte Tortorella on 3/7/17.
// Copyright © 2017 Monadic Consulting. All rights reserved.
//
import Foundation
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)
}
}
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 {