Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Sum<A,B> {
- case left(A)
- case right(B)
- }
- struct Product<A,B> {
- let a: A
- let b: B
- }
- struct Iso<A,B> {
- let to: (A) -> B
- let from: (B) -> A
- }
- struct Unit { }
- extension Unit: Show {
- var text: String {
- return ""
- }
- }
- protocol Show {
- var text: String { get }
- }
- extension Sum: Show where A: Show, B: Show {
- var text: String {
- switch self {
- case .left(let a): return ".left(\(a.text)"
- case .right(let b): return ".right(\(b.text)"
- }
- }
- }
- extension Product: Show where A: Show, B: Show {
- var text: String {
- return "(\(self.a), \(self.b))"
- }
- }
- protocol Generic {
- associatedtype Rep
- var to: Rep { get }
- }
- struct Rec<A> where A: Generic {
- let value: A
- }
- extension Rec: Show where A.Rep: Show {
- var text: String {
- return value.to.text
- }
- }
- extension Array: Generic {
- typealias Rep = Sum<Unit, Product<Element, Rec<Array<Element>>>>
- var to: Sum<Unit, Product<Element, Rec<Array<Element>>>> {
- if self.isEmpty { return .left(Unit()) }
- return .right(Product(a: self[0], b: Rec(value: Array(self.suffix(from: 1)))))
- }
- }
- extension Int: Show {
- var text: String {
- return "\(self)"
- }
- }
- let arr = [1,2,3]
- arr.to.text
Add Comment
Please, Sign In to add comment