Skip to content

Instantly share code, notes, and snippets.

@worthmine
Last active August 29, 2015 14:13
Show Gist options
  • Save worthmine/1bf7d7f55ef49858b7d0 to your computer and use it in GitHub Desktop.
Save worthmine/1bf7d7f55ef49858b7d0 to your computer and use it in GitHub Desktop.
It works!
class StringLikePerlArray {
var text :String = ""
init(_ string :String = ""){
text = string
}
func count()->Int {
var count :Int = 0
for string in text {
count++
}
return count
}
func pop() -> String {
if self.count() == 0 { return "" }
let removed = self.text.removeAtIndex(self.text.endIndex.predecessor())
return String(removed)
}
func push(value: String) -> String {
self.text.insert(Character(value), atIndex: self.text.startIndex)
return self.text
}
func shift() -> String {
if self.count() == 0 { return "" }
let removed = self.text.removeAtIndex(self.text.startIndex)
return String(removed)
}
func unshift(value: String) -> String {
self.text.insert(Character(value), atIndex: self.text.endIndex)
return self.text
}
}
let a = StringLikePerlArray("String") // (text "String")
a.count() // 6
a.text = "another String" // (text "anotherString")
a.count() // 14
a.push("1") // "1anotherString"
a.count() // 15
a.unshift("F") // "1anotherStringF"
a.shift() // "1"
a.shift() // "a"
a.pop() // "F"
a.count() // 13
a.text // "nother String"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment