Skip to content

Instantly share code, notes, and snippets.

@TheDarkCode
Last active May 13, 2019 07:36
Show Gist options
  • Save TheDarkCode/d46dddaaac763eae5137833c41daf28d to your computer and use it in GitHub Desktop.
Save TheDarkCode/d46dddaaac763eae5137833c41daf28d to your computer and use it in GitHub Desktop.
Swift Gematria - English Ordinal Example
public extension Character {
func EnglishOrdinal() - > Int {
switch (self) {
case "a":
return 1;
case "b":
return 2;
case "c":
return 3;
case "d":
return 4;
case "e":
return 5;
case "f":
return 6;
case "g":
return 7;
case "h":
return 8;
case "i":
return 9;
case "j":
return 10;
case "k":
return 11;
case "l":
return 12;
case "m":
return 13;
case "n":
return 14;
case "o":
return 15;
case "p":
return 16;
case "q":
return 17;
case "r":
return 18;
case "s":
return 19;
case "t":
return 20;
case "u":
return 21;
case "v":
return 22;
case "w":
return 23;
case "x":
return 24;
case "y":
return 25;
case "z":
return 26;
case "A":
return 1;
case "B":
return 2;
case "C":
return 3;
case "D":
return 4;
case "E":
return 5;
case "F":
return 6;
case "G":
return 7;
case "H":
return 8;
case "I":
return 9;
case "J":
return 10;
case "K":
return 11;
case "L":
return 12;
case "M":
return 13;
case "N":
return 14;
case "O":
return 15;
case "P":
return 16;
case "Q":
return 17;
case "R":
return 18;
case "S":
return 19;
case "T":
return 20;
case "U":
return 21;
case "V":
return 22;
case "W":
return 23;
case "X":
return 24;
case "Y":
return 25;
case "Z":
return 26;
default:
return 0;
}
}
}
public extension String {
static
var Empty: String {
return ""
}
func ToEnglishOrdinal() - > Int {
if (self.count == 0) {
return 0
} else {
return self.map {
$0.EnglishOrdinal()
}.reduce(0, +)
}
}
}
// Usage
// import UIKit
// var str = "Hello, playground"
// print(str.ToEnglishOrdinal())
// print("".ToEnglishOrdinal())
// print(String.Empty.ToEnglishOrdinal())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment