Skip to content

Instantly share code, notes, and snippets.

@oropon
Created July 5, 2014 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oropon/927fec25734975891cc4 to your computer and use it in GitHub Desktop.
Save oropon/927fec25734975891cc4 to your computer and use it in GitHub Desktop.
//
// 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