This file contains 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
struct Person { | |
let name: String | |
let age: Int | |
let city: String | |
} |
This file contains 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 parse(source: String) -> Person? { | |
let lines = source.components(separatedBy: "\n") | |
let age = matches(for: "(?<=age: )[0-9]+", in: lines[1]).flatMap({Int($0)}) | |
let name = matches(for: "(?<=name: )\\w+", in: lines[2]) | |
let city = matches(for: "(?<=city: )[\\w ]+", in: lines[3]) | |
if let a = age, let n = name, let c = city { | |
return Person(name: n, age: a, city: c) | |
} else { |
This file contains 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 parse(source: String) -> Person? { | |
func startingIndex(`for` string: String, after offset: Int) -> String.Index { | |
return string.index(string.startIndex, offsetBy: offset) | |
} | |
let lines = source.components(separatedBy: "\n") | |
guard lines.count >= 4 else { return nil } | |
var age: Int? = nil | |
if lines[1].hasPrefix("age: ") { | |
let start = startingIndex(for: lines[1], after: 5) |
This file contains 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
// | |
// NaiveTree.swift | |
// immutable_nary_tree | |
// | |
// Created by zephyz on 18.02.17. | |
// Copyright © 2017 moe.zephyz. All rights reserved. | |
// | |
import Foundation |
This file contains 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
prefix operator ¬ | |
prefix func ¬ (op: Bool) -> Bool { | |
return !op | |
} |
This file contains 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
infix operator • | |
public func • <A, B, C>(f : @escaping (B) -> C, g : @escaping (A) -> B) -> (A) -> C { | |
return { f(g($0)) } | |
} |
This file contains 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
prefix operator == | |
prefix func == <T: Equatable>(rhs: T) -> (T) -> Bool { | |
return { lhs in lhs == rhs } | |
} |
This file contains 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
/// Partially applies the right argument of a binary function | |
func partialRightApply<A, B, C>(_ f: @escaping (A, B) -> C, arg rhs: B) -> (A) -> C { | |
return {lhs in f(lhs, rhs)} | |
} | |
prefix operator > | |
prefix func > <T: Comparable>(rhs: T) -> (T) -> Bool { | |
return partialRightApply(>, arg: rhs) | |
} |
This file contains 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
mutual | |
tryGenOp : StackCmd () pre (S _) -> StackIO h | |
tryGenOp x {pre} {h} = case decEq pre h of | |
(Yes Refl) => do x | |
res <- Top | |
PutStr (show res ++ "\n") | |
stackCalc | |
(No contra) => do PutStr $ "not enough item on the stack, expected " ++ |
This file contains 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
-- as long as there is a proof that a < b then we can build a Fin b | |
natToProvedFin : (n : Nat) -> (bound : Nat) -> {auto prf : LT n bound} -> Fin bound | |
natToProvedFin Z (S right) {prf=LTESucc x} = FZ | |
natToProvedFin (S k) (S right) {prf=LTESucc x} = FS $ natToProvedFin k right | |
record Numbers where | |
constructor MkNumbers | |
max : Nat | |
bonus : Nat | |
current : Fin (S (max + bonus)) |
OlderNewer