Created
November 28, 2016 00:36
-
-
Save Qata/3b72eedde5d3314ed9dea0db92b31fef to your computer and use it in GitHub Desktop.
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
// | |
// Monoid.swift | |
// FPExamples | |
// | |
// Created by Charlotte Tortorella on 28/11/16. | |
// Copyright © 2016 Charlotte Tortorella. All rights reserved. | |
// | |
import Foundation | |
protocol Alternative: Equatable { | |
static var empty: Self { get } | |
static func <|>(_ a: Self, _ b: @autoclosure () -> Self) -> Self | |
} | |
infix operator <|> : NilCoalescingPrecedence | |
extension Alternative { | |
static func <|>(_ a: Self, _ b: @autoclosure () -> Self) -> Self { | |
guard a != Self.empty else { | |
return b() | |
} | |
return a | |
} | |
} | |
protocol Monoid: Alternative { | |
static var empty: Self { get } | |
func append(_ b: Self) -> Self | |
} | |
extension Array where Element: Monoid { | |
func concat() -> Element { | |
return reduce(Element.empty, +) | |
} | |
} | |
func +<A: Monoid>(_ a: A, _ b: A) -> A { | |
return a.append(b) | |
} | |
extension Int: Monoid { | |
static let empty: Int = 0 | |
func append(_ b: Int) -> Int { | |
return self + b | |
} | |
} | |
extension Int8: Monoid { | |
static let empty: Int8 = 0 | |
func append(_ b: Int8) -> Int8 { | |
return self + b | |
} | |
} | |
extension Int16: Monoid { | |
static let empty: Int16 = 0 | |
func append(_ b: Int16) -> Int16 { | |
return self + b | |
} | |
} | |
extension Int32: Monoid { | |
static let empty: Int32 = 0 | |
func append(_ b: Int32) -> Int32 { | |
return self + b | |
} | |
} | |
extension Int64: Monoid { | |
static let empty: Int64 = 0 | |
func append(_ b: Int64) -> Int64 { | |
return self + b | |
} | |
} | |
extension UInt: Monoid { | |
static let empty: UInt = 0 | |
func append(_ b: UInt) -> UInt { | |
return self + b | |
} | |
} | |
extension UInt8: Monoid { | |
static let empty: UInt8 = 0 | |
func append(_ b: UInt8) -> UInt8 { | |
return self + b | |
} | |
} | |
extension UInt16: Monoid { | |
static let empty: UInt16 = 0 | |
func append(_ b: UInt16) -> UInt16 { | |
return self + b | |
} | |
} | |
extension UInt32: Monoid { | |
static let empty: UInt32 = 0 | |
func append(_ b: UInt32) -> UInt32 { | |
return self + b | |
} | |
} | |
extension UInt64: Monoid { | |
static let empty: UInt64 = 0 | |
func append(_ b: UInt64) -> UInt64 { | |
return self + b | |
} | |
} | |
extension Float: Monoid { | |
static let empty: Float = 0 | |
func append(_ b: Float) -> Float { | |
return self + b | |
} | |
} | |
extension Float80: Monoid { | |
static let empty: Float80 = 0 | |
func append(_ b: Float80) -> Float80 { | |
return self + b | |
} | |
} | |
extension Double: Monoid { | |
static let empty: Double = 0 | |
func append(_ b: Double) -> Double { | |
return self + b | |
} | |
} | |
extension String: Monoid { | |
static let empty: String = "" | |
func append(_ b: String) -> String { | |
return self + b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment