Dictionaries of values, records and functions
The complete source of collectionTypes_dictionary.ek9, compiled by the EK9 compiler. Shown on collectionTypes.html.
#!ek9 defines module site.collectionTypes.dictionary defines type List of getAddams Dict of (Integer, String) Dict of (String, Date) Dict of (Integer, Addams) Dict of (Integer, getAddams) defines record Addams name <- String() dob <- Date() Addams() -> dateOfBirth as Date if dateOfBirth? dob :=: dateOfBirth Addams() -> name as String dateOfBirth as Date this(dateOfBirth) this.name :=: name operator $ as pure <- rtn as String: `${name} ${dob}` override operator ? as pure <- rtn as Boolean: name? and dob? operator #^ as pure <- rtn as String: $this defines function idToString() as abstract -> id as Integer <- rtn as Optional of String? nameToDate() as abstract -> name as String <- rtn as Optional of Date? idToAddams() as abstract -> id as Integer <- rtn as Optional of Addams? idToGetAddams() as abstract -> id as Integer <- rtn as getAddams? addamsToString() as pure -> person as Addams <- rtn as String: `DOB: ${person.dob} Name: ${person.name}` getAddams() as abstract <- rtn as Addams? getMorticia() is getAddams <- rtn as Addams: Addams("Morticia", 1965-01-03) getGomez() is getAddams <- rtn as Addams: Addams( "Gomez", 1963-06-08) getPugsley() is getAddams <- rtn as Addams: Addams("Pugsley", 1984-10-21) getFester() is getAddams <- rtn as Addams: Addams(1960-11-21) rtn.name: "Fester" getWednesday() is getAddams <- rtn as Addams: Addams() rtn.name: "Wednesday" rtn.dob: 1998-01-09 defines program ListDictionaryExample() dictionaryExample <- SimpleDictionaryUse() dictionaryExample.showDictionary() defines class SimpleDictionaryUse private getName() <- name String: "Gomez" showDictionary() idNameDictionary <- { 1: getName(), 2: getMorticia().name, 3: "Pugsley", 4: "Fester", 5: "Wednesday" } addamsParents <- { getName(): 1963-06-08, getMorticia().name: getMorticia().dob } addamsKids <- { "Pugsley": 1984-10-21, "Wednesday": 1998-01-09 } nameDateDictionary <- addamsParents + addamsKids + DictEntry("Fester", 1960-11-21) idRecordDictionary <- { 1: getGomez(), 2: getMorticia(), 3: getPugsley(), 4: getFester(), 5: getWednesday() } idFunctionDictionary <- { 1: getGomez, 2: getMorticia, 3: getPugsley, 4: getFester, 5: getWednesday } results as List of String: List() of String keyIter <- idNameDictionary.keys() while keyIter? key <- keyIter.next() name <- idNameDictionary.getOrDefault(key, String()) if name? results += `Key ${key} with name ${name}` //Some keys that won't ever be found invalidValues <- [9, 10, 100] keys1 <- idNameDictionary.keys() keys2 <- invalidValues.iterator() //This is a dynamic function that 'captures' a variable. nameMapping <- (idNameDictionary) is idToString as function rtn: Optional(idNameDictionary.getOrDefault(id, String())) validValues <- cat keys1, keys2 | map with nameMapping | flatten | collect as List of String require $validValues == `["Gomez", "Morticia", "Pugsley", "Fester", "Wednesday"]` //This is also a dynamic function that 'captures' a variable. dateMapping <- (nameDateDictionary) extends nameToDate rtn: Optional(nameDateDictionary.getOrDefault(name, Date())) //Now we've used the previous iterator up so need new ones //We can also use the pipeline in column format keys1A <- idNameDictionary.keys() validDates <- cat keys1A | map nameMapping | flatten | map dateMapping | flatten | collect as List of Date require $validDates == "[1963-06-08, 1965-01-03, 1984-10-21, 1960-11-21, 1998-01-09]" //The records and functions held in a Dict are used in the same way. showRecordsAndFunctions(idNameDictionary, idRecordDictionary, idFunctionDictionary) private showRecordsAndFunctions() -> idNameDictionary as Dict of (Integer, String) idRecordDictionary as Dict of (Integer, Addams) idFunctionDictionary as Dict of (Integer, getAddams) recordMapping <- (idRecordDictionary) extends idToAddams rtn: Optional(idRecordDictionary.getOrDefault(id, Addams())) //Now we've used the previous iterator up so need new ones keys1B <- idNameDictionary.keys() validAddams <- cat keys1B | map recordMapping | flatten | collect as List of Addams require length validAddams == 5 //"[Gomez 1963-06-08, Morticia 1965-01-03, Pugsley 1984-10-21, Fester 1960-11-21, Wednesday 1998-01-09]" //Example of an inline dynamic function //And and example of cat on a call return. addamsFamily <- cat idNameDictionary.keys() | map with (idFunctionDictionary) extends idToGetAddams (rtn: idFunctionDictionary.getOrDefault(id, getGomez)) | call | collect as List of Addams require length addamsFamily == 5 //"[Gomez 1963-06-08, Morticia 1965-01-03, Pugsley 1984-10-21, Fester 1960-11-21, Wednesday 1998-01-09]" values <- cat idNameDictionary.values() | collect as List of String require $values == `["Gomez", "Morticia", "Pugsley", "Fester", "Wednesday"]` found <- idFunctionDictionary.getOrDefault(1, getGomez) require found? require $found() == "Gomez 1963-06-08"
Download collectionTypes_dictionary.ek9