Created
July 5, 2014 07:06
-
-
Save oropon/927fec25734975891cc4 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
// | |
// utils.swift | |
// md_bgmasset | |
// | |
// Created by oropon on 6/11/14. | |
// Copyright (c) 2014 oropon. All rights reserved. | |
// | |
extension Array { | |
func any(p: (T) -> Bool) -> Bool { | |
for x in self { | |
if p(x) { | |
return true | |
} | |
} | |
return false | |
} | |
} | |
func strip(cs: String) -> String{ | |
let whitespaces :Array<Character> = [" ", "\t", "\n", "\r"] | |
func stripl(cs: String) -> String { | |
var ix :String.Index | |
for ix = cs.startIndex; ix != cs.endIndex; ix = ix.succ() { | |
if !whitespaces.any({cs[ix] == $0}) { | |
break | |
} | |
} | |
return cs[ix..cs.endIndex] | |
} | |
func stripr(cs: String) -> String { | |
var ix :String.Index | |
for ix = cs.endIndex.pred(); ix != cs.startIndex; ix = ix.pred() { | |
if !whitespaces.any({cs[ix] == $0}) { | |
break | |
} | |
} | |
return cs[cs.startIndex...ix] | |
} | |
return stripl(stripr(cs)) | |
} | |
class Stack<T> { | |
var array :Array<T> = T[]() | |
func stack(e: T) { | |
array.append(e) | |
} | |
func pop(e: T) -> T? { | |
if self.isEmpty() {return nil} | |
return array.removeLast() | |
} | |
func peek(e: T) -> T? { | |
if self.isEmpty() {return nil} | |
return array[array.endIndex.pred()] | |
} | |
func isEmpty() -> Bool { | |
return array.count == 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment