Skip to content

Instantly share code, notes, and snippets.

struct Person {
let name: String
let age: Int
let city: String
}
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 {
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)
//
// NaiveTree.swift
// immutable_nary_tree
//
// Created by zephyz on 18.02.17.
// Copyright © 2017 moe.zephyz. All rights reserved.
//
import Foundation
prefix operator ¬
prefix func ¬ (op: Bool) -> Bool {
return !op
}
infix operator •
public func • <A, B, C>(f : @escaping (B) -> C, g : @escaping (A) -> B) -> (A) -> C {
return { f(g($0)) }
}
prefix operator ==
prefix func == <T: Equatable>(rhs: T) -> (T) -> Bool {
return { lhs in lhs == rhs }
}
/// 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)
}
@andrevidela
andrevidela / ex_13_1_2.idr
Created August 17, 2017 22:24
Can't infer explicit argument to tryGenOp, Can't infer explicit argument to S, Can't infer explicit argument to doAdd
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 " ++
-- 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))