Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created July 19, 2015 05:00
Show Gist options
  • Save jweinst1/ade3010b17b803da134d to your computer and use it in GitHub Desktop.
Save jweinst1/ade3010b17b803da134d to your computer and use it in GitHub Desktop.
Two functions that slice arrays in Swift
// Returns a slice of an array that contains characters
func StringSlicer(list:Array<Character>, n1:Int, n2:Int) ->Array<Character> {
var slicer = n1; var slicedlist = [Character]()
while slicer <= n2 {
slicedlist.append(list[slicer])
slicer += 1
}
return slicedlist
}
// returns a slice of an array of characters with an increment
func StringSlicerIncrement(list:Array<Character>, n1:Int, n2:Int, inc:Int) ->Array<Character> {
var slicer = n1; var slicedlist = [Character]()
while slicer <= n2 {
slicedlist.append(list[slicer])
slicer += inc
}
return slicedlist
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment