Skip to content

Instantly share code, notes, and snippets.

@stuffmc
Created September 23, 2014 20:58
Show Gist options
  • Save stuffmc/5cd2b25b35fddf8d1d37 to your computer and use it in GitHub Desktop.
Save stuffmc/5cd2b25b35fddf8d1d37 to your computer and use it in GitHub Desktop.
let bytes = [84,11]
// Imperative — 6 lines but more readable
var hex = ""
for byte in bytes {
let format = NSString(format: "%02x", byte)
hex = "\(hex)\(format)"
}
hex
// Functionnal - 4 lines but weird
bytes.map({ (byte: Int) -> String in
return NSString(format: "%02x", byte)
}).reduce("", combine: { (hex, byte) -> String in
return hex+byte
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment