Last active
February 18, 2022 12:45
-
-
Save bishabosha/68c557eb77c4623aa4ab223359d9058f to your computer and use it in GitHub Desktop.
ISO Continents and Countries, with lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @main def addContinents(path: String): Unit = | |
| val lines = scala.io.Source.fromFile(path).getLines | |
| for case s"$country, $visitors" <- lines.drop(1) do | |
| continentFromCountry(country) match | |
| case Some(continent) => | |
| println(s"${continent.name}, $country, $visitors") | |
| case None => | |
| Console.err.println( | |
| "[error] no continent found for country: " + country | |
| ) | |
| sys.exit(1) | |
| @main def collectContinents(path: String): Unit = | |
| val lines = scala.io.Source.fromFile(path).getLines | |
| val continents = lines | |
| .drop(1) | |
| .map { case s"$country, $visitors" => | |
| continentFromCountry(country) match | |
| case Some(continent) => (continent, visitors) | |
| case None => | |
| Console.err.println( | |
| "[error] no continent found for country: " + country | |
| ) | |
| sys.exit(1) | |
| } | |
| .toList | |
| .groupMap((continent, _) => continent)((_, visitors) => visitors) | |
| val RoundedK = raw"(\d+)\.(\d)k".r | |
| val WholeK = raw"(\d+)k".r | |
| val Whole = raw"(\d+)".r | |
| val Percent = raw"(\d+)%".r | |
| val agg = for (continent -> visitorPairs) <- continents yield | |
| val visitors = | |
| for case s"$number $ratio" <- visitorPairs yield | |
| def normaliseCount(count: String): Int = count match | |
| case RoundedK(whole, dec) => s"${whole}${dec}00".toInt | |
| case WholeK(whole) => s"${whole}000".toInt | |
| case Whole(whole) => whole.toInt | |
| normaliseCount(number) | |
| val totalVisitors = visitors.map(_.toLong).sum | |
| continent -> totalVisitors | |
| val total = agg.values.sum | |
| for | |
| (continent -> totalVisitors) <- agg.toList.sortBy(_(1))(Ordering.Long.reverse) | |
| yield | |
| val percent = (totalVisitors.toDouble / total * 100).toInt | |
| println(s"${continent.name}, ${totalVisitors}, ${percent}%") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum ISOCountryContinent(val continentCode: String, val iso2Code: String, val iso3Code: String, val isoNumber: Int, val name: String, val commonNames: List[String]) { | |
| case AF_AFG_AS extends ISOCountryContinent("AS", "AF", "AFG", 4, "Afghanistan, Islamic Republic of", Nil) | |
| case AL_ALB_EU extends ISOCountryContinent("EU", "AL", "ALB", 8, "Albania, Republic of", Nil) | |
| case AQ_ATA_AN extends ISOCountryContinent("AN", "AQ", "ATA", 10, "Antarctica (the territory South of 60 deg S)", Nil) | |
| case DZ_DZA_AF extends ISOCountryContinent("AF", "DZ", "DZA", 12, "Algeria, People's Democratic Republic of", Nil) | |
| case AS_ASM_OC extends ISOCountryContinent("OC", "AS", "ASM", 16, "American Samoa", Nil) | |
| case AD_AND_EU extends ISOCountryContinent("EU", "AD", "AND", 20, "Andorra, Principality of", Nil) | |
| case AO_AGO_AF extends ISOCountryContinent("AF", "AO", "AGO", 24, "Angola, Republic of", Nil) | |
| case AG_ATG_NA extends ISOCountryContinent("NA", "AG", "ATG", 28, "Antigua and Barbuda", Nil) | |
| case AZ_AZE_AS extends ISOCountryContinent("AS", "AZ", "AZE", 31, "Azerbaijan, Republic of", Nil) | |
| case AZ_AZE_EU extends ISOCountryContinent("EU", "AZ", "AZE", 31, "Azerbaijan, Republic of", Nil) | |
| case AR_ARG_SA extends ISOCountryContinent("SA", "AR", "ARG", 32, "Argentina, Argentine Republic", Nil) | |
| case AU_AUS_OC extends ISOCountryContinent("OC", "AU", "AUS", 36, "Australia, Commonwealth of", Nil) | |
| case AT_AUT_EU extends ISOCountryContinent("EU", "AT", "AUT", 40, "Austria, Republic of", Nil) | |
| case BS_BHS_NA extends ISOCountryContinent("NA", "BS", "BHS", 44, "Bahamas, Commonwealth of the", "The Bahamas" :: Nil) | |
| case BH_BHR_AS extends ISOCountryContinent("AS", "BH", "BHR", 48, "Bahrain, Kingdom of", Nil) | |
| case BD_BGD_AS extends ISOCountryContinent("AS", "BD", "BGD", 50, "Bangladesh, People's Republic of", Nil) | |
| case AM_ARM_AS extends ISOCountryContinent("AS", "AM", "ARM", 51, "Armenia, Republic of", Nil) | |
| case AM_ARM_EU extends ISOCountryContinent("EU", "AM", "ARM", 51, "Armenia, Republic of", Nil) | |
| case BB_BRB_NA extends ISOCountryContinent("NA", "BB", "BRB", 52, "Barbados", Nil) | |
| case BE_BEL_EU extends ISOCountryContinent("EU", "BE", "BEL", 56, "Belgium, Kingdom of", Nil) | |
| case BM_BMU_NA extends ISOCountryContinent("NA", "BM", "BMU", 60, "Bermuda", Nil) | |
| case BT_BTN_AS extends ISOCountryContinent("AS", "BT", "BTN", 64, "Bhutan, Kingdom of", Nil) | |
| case BO_BOL_SA extends ISOCountryContinent("SA", "BO", "BOL", 68, "Bolivia, Plurinational State of", Nil) | |
| case BA_BIH_EU extends ISOCountryContinent("EU", "BA", "BIH", 70, "Bosnia and Herzegovina", Nil) | |
| case BW_BWA_AF extends ISOCountryContinent("AF", "BW", "BWA", 72, "Botswana, Republic of", Nil) | |
| case BV_BVT_AN extends ISOCountryContinent("AN", "BV", "BVT", 74, "Bouvet Island (Bouvetoya)", Nil) | |
| case BR_BRA_SA extends ISOCountryContinent("SA", "BR", "BRA", 76, "Brazil, Federative Republic of", Nil) | |
| case BZ_BLZ_NA extends ISOCountryContinent("NA", "BZ", "BLZ", 84, "Belize", Nil) | |
| case IO_IOT_AF extends ISOCountryContinent("AF", "IO", "IOT", 86, "British Indian Ocean Territory", Nil) | |
| case SB_SLB_OC extends ISOCountryContinent("OC", "SB", "SLB", 90, "Solomon Islands", Nil) | |
| case VG_VGB_NA extends ISOCountryContinent("NA", "VG", "VGB", 92, "British Virgin Islands", Nil) | |
| case BN_BRN_AS extends ISOCountryContinent("AS", "BN", "BRN", 96, "Brunei, Nation of, the Abode of Peace", Nil) | |
| case BG_BGR_EU extends ISOCountryContinent("EU", "BG", "BGR", 100, "Bulgaria, Republic of", Nil) | |
| case MM_MMR_AS extends ISOCountryContinent("AS", "MM", "MMR", 104, "Myanmar, Union of", Nil) | |
| case BI_BDI_AF extends ISOCountryContinent("AF", "BI", "BDI", 108, "Burundi, Republic of", Nil) | |
| case BY_BLR_EU extends ISOCountryContinent("EU", "BY", "BLR", 112, "Belarus, Republic of", Nil) | |
| case KH_KHM_AS extends ISOCountryContinent("AS", "KH", "KHM", 116, "Cambodia, Kingdom of", Nil) | |
| case CM_CMR_AF extends ISOCountryContinent("AF", "CM", "CMR", 120, "Cameroon, Republic of", Nil) | |
| case CA_CAN_NA extends ISOCountryContinent("NA", "CA", "CAN", 124, "Canada", Nil) | |
| case CV_CPV_AF extends ISOCountryContinent("AF", "CV", "CPV", 132, "Cape Verde, Republic of", Nil) | |
| case KY_CYM_NA extends ISOCountryContinent("NA", "KY", "CYM", 136, "Cayman Islands", Nil) | |
| case CF_CAF_AF extends ISOCountryContinent("AF", "CF", "CAF", 140, "Central African Republic", Nil) | |
| case LK_LKA_AS extends ISOCountryContinent("AS", "LK", "LKA", 144, "Sri Lanka, Democratic Socialist Republic of", Nil) | |
| case TD_TCD_AF extends ISOCountryContinent("AF", "TD", "TCD", 148, "Chad, Republic of", Nil) | |
| case CL_CHL_SA extends ISOCountryContinent("SA", "CL", "CHL", 152, "Chile, Republic of", Nil) | |
| case CN_CHN_AS extends ISOCountryContinent("AS", "CN", "CHN", 156, "China, People's Republic of", Nil) | |
| case TW_TWN_AS extends ISOCountryContinent("AS", "TW", "TWN", 158, "Taiwan", Nil) | |
| case CX_CXR_AS extends ISOCountryContinent("AS", "CX", "CXR", 162, "Christmas Island", Nil) | |
| case CC_CCK_AS extends ISOCountryContinent("AS", "CC", "CCK", 166, "Cocos (Keeling) Islands", Nil) | |
| case CO_COL_SA extends ISOCountryContinent("SA", "CO", "COL", 170, "Colombia, Republic of", Nil) | |
| case KM_COM_AF extends ISOCountryContinent("AF", "KM", "COM", 174, "Comoros, Union of the", Nil) | |
| case YT_MYT_AF extends ISOCountryContinent("AF", "YT", "MYT", 175, "Mayotte, Department of", Nil) | |
| case CG_COG_AF extends ISOCountryContinent("AF", "CG", "COG", 178, "Congo, Republic of the", Nil) | |
| case CD_COD_AF extends ISOCountryContinent("AF", "CD", "COD", 180, "Congo, Democratic Republic of the", Nil) | |
| case CK_COK_OC extends ISOCountryContinent("OC", "CK", "COK", 184, "Cook Islands", Nil) | |
| case CR_CRI_NA extends ISOCountryContinent("NA", "CR", "CRI", 188, "Costa Rica, Republic of", Nil) | |
| case HR_HRV_EU extends ISOCountryContinent("EU", "HR", "HRV", 191, "Croatia, Republic of", Nil) | |
| case CU_CUB_NA extends ISOCountryContinent("NA", "CU", "CUB", 192, "Cuba, Republic of", Nil) | |
| case CY_CYP_AS extends ISOCountryContinent("AS", "CY", "CYP", 196, "Cyprus, Republic of", Nil) | |
| case CY_CYP_EU extends ISOCountryContinent("EU", "CY", "CYP", 196, "Cyprus, Republic of", Nil) | |
| case CZ_CZE_EU extends ISOCountryContinent("EU", "CZ", "CZE", 203, "Czech Republic", Nil) | |
| case BJ_BEN_AF extends ISOCountryContinent("AF", "BJ", "BEN", 204, "Benin, Republic of", Nil) | |
| case DK_DNK_EU extends ISOCountryContinent("EU", "DK", "DNK", 208, "Denmark, Kingdom of", Nil) | |
| case DM_DMA_NA extends ISOCountryContinent("NA", "DM", "DMA", 212, "Dominica, Commonwealth of", Nil) | |
| case DO_DOM_NA extends ISOCountryContinent("NA", "DO", "DOM", 214, "Dominican Republic", Nil) | |
| case EC_ECU_SA extends ISOCountryContinent("SA", "EC", "ECU", 218, "Ecuador, Republic of", Nil) | |
| case SV_SLV_NA extends ISOCountryContinent("NA", "SV", "SLV", 222, "El Salvador, Republic of", Nil) | |
| case GQ_GNQ_AF extends ISOCountryContinent("AF", "GQ", "GNQ", 226, "Equatorial Guinea, Republic of", Nil) | |
| case ET_ETH_AF extends ISOCountryContinent("AF", "ET", "ETH", 231, "Ethiopia, Federal Democratic Republic of", Nil) | |
| case ER_ERI_AF extends ISOCountryContinent("AF", "ER", "ERI", 232, "Eritrea, State of", Nil) | |
| case EE_EST_EU extends ISOCountryContinent("EU", "EE", "EST", 233, "Estonia, Republic of", Nil) | |
| case FO_FRO_EU extends ISOCountryContinent("EU", "FO", "FRO", 234, "Faroe Islands", Nil) | |
| case FK_FLK_SA extends ISOCountryContinent("SA", "FK", "FLK", 238, "Falkland Islands (Malvinas)", Nil) | |
| case GS_SGS_AN extends ISOCountryContinent("AN", "GS", "SGS", 239, "South Georgia and the South Sandwich Islands", Nil) | |
| case FJ_FJI_OC extends ISOCountryContinent("OC", "FJ", "FJI", 242, "Fiji, Republic of", Nil) | |
| case FI_FIN_EU extends ISOCountryContinent("EU", "FI", "FIN", 246, "Finland, Republic of", Nil) | |
| case AX_ALA_EU extends ISOCountryContinent("EU", "AX", "ALA", 248, "Åland Islands", Nil) | |
| case FR_FRA_EU extends ISOCountryContinent("EU", "FR", "FRA", 250, "France, French Republic", Nil) | |
| case GF_GUF_SA extends ISOCountryContinent("SA", "GF", "GUF", 254, "French Guiana", Nil) | |
| case PF_PYF_OC extends ISOCountryContinent("OC", "PF", "PYF", 258, "French Polynesia", Nil) | |
| case TF_ATF_AF extends ISOCountryContinent("AF", "TF", "ATF", 260, "French Southern Territories", Nil) | |
| case DJ_DJI_AF extends ISOCountryContinent("AF", "DJ", "DJI", 262, "Djibouti, Republic of", Nil) | |
| case GA_GAB_AF extends ISOCountryContinent("AF", "GA", "GAB", 266, "Gabon, Gabonese Republic", Nil) | |
| case GE_GEO_AS extends ISOCountryContinent("AS", "GE", "GEO", 268, "Georgia", Nil) | |
| case GE_GEO_EU extends ISOCountryContinent("EU", "GE", "GEO", 268, "Georgia", Nil) | |
| case GM_GMB_AF extends ISOCountryContinent("AF", "GM", "GMB", 270, "Gambia, Republic of the", Nil) | |
| case DE_DEU_EU extends ISOCountryContinent("EU", "DE", "DEU", 276, "Germany, Federal Republic of", Nil) | |
| case GH_GHA_AF extends ISOCountryContinent("AF", "GH", "GHA", 288, "Ghana, Republic of", Nil) | |
| case GI_GIB_EU extends ISOCountryContinent("EU", "GI", "GIB", 292, "Gibraltar", Nil) | |
| case KI_KIR_OC extends ISOCountryContinent("OC", "KI", "KIR", 296, "Kiribati, Republic of", Nil) | |
| case GR_GRC_EU extends ISOCountryContinent("EU", "GR", "GRC", 300, "Greece, Hellenic Republic", Nil) | |
| case GL_GRL_NA extends ISOCountryContinent("NA", "GL", "GRL", 304, "Greenland", Nil) | |
| case GD_GRD_NA extends ISOCountryContinent("NA", "GD", "GRD", 308, "Grenada", Nil) | |
| case GP_GLP_NA extends ISOCountryContinent("NA", "GP", "GLP", 312, "Guadeloupe", Nil) | |
| case GU_GUM_OC extends ISOCountryContinent("OC", "GU", "GUM", 316, "Guam", Nil) | |
| case GT_GTM_NA extends ISOCountryContinent("NA", "GT", "GTM", 320, "Guatemala, Republic of", Nil) | |
| case GN_GIN_AF extends ISOCountryContinent("AF", "GN", "GIN", 324, "Guinea, Republic of", Nil) | |
| case GY_GUY_SA extends ISOCountryContinent("SA", "GY", "GUY", 328, "Guyana, Co-operative Republic of", Nil) | |
| case HT_HTI_NA extends ISOCountryContinent("NA", "HT", "HTI", 332, "Haiti, Republic of", Nil) | |
| case HM_HMD_AN extends ISOCountryContinent("AN", "HM", "HMD", 334, "Heard Island and McDonald Islands", Nil) | |
| case VA_VAT_EU extends ISOCountryContinent("EU", "VA", "VAT", 336, "Holy See (Vatican City State)", Nil) | |
| case HN_HND_NA extends ISOCountryContinent("NA", "HN", "HND", 340, "Honduras, Republic of", Nil) | |
| case HK_HKG_AS extends ISOCountryContinent("AS", "HK", "HKG", 344, "Hong Kong, Special Administrative Region of China", Nil) | |
| case HU_HUN_EU extends ISOCountryContinent("EU", "HU", "HUN", 348, "Hungary, Republic of", Nil) | |
| case IS_ISL_EU extends ISOCountryContinent("EU", "IS", "ISL", 352, "Iceland, Republic of", Nil) | |
| case IN_IND_AS extends ISOCountryContinent("AS", "IN", "IND", 356, "India, Republic of", Nil) | |
| case ID_IDN_AS extends ISOCountryContinent("AS", "ID", "IDN", 360, "Indonesia, Republic of", Nil) | |
| case IR_IRN_AS extends ISOCountryContinent("AS", "IR", "IRN", 364, "Iran, Islamic Republic of", Nil) | |
| case IQ_IRQ_AS extends ISOCountryContinent("AS", "IQ", "IRQ", 368, "Iraq, Republic of", Nil) | |
| case IE_IRL_EU extends ISOCountryContinent("EU", "IE", "IRL", 372, "Ireland", Nil) | |
| case IL_ISR_AS extends ISOCountryContinent("AS", "IL", "ISR", 376, "Israel, State of", Nil) | |
| case IT_ITA_EU extends ISOCountryContinent("EU", "IT", "ITA", 380, "Italy, Italian Republic", Nil) | |
| case CI_CIV_AF extends ISOCountryContinent("AF", "CI", "CIV", 384, "Côte d'Ivoire, Republic of", "Ivory Coast" :: Nil) | |
| case JM_JAM_NA extends ISOCountryContinent("NA", "JM", "JAM", 388, "Jamaica", Nil) | |
| case JP_JPN_AS extends ISOCountryContinent("AS", "JP", "JPN", 392, "Japan", Nil) | |
| case KZ_KAZ_AS extends ISOCountryContinent("AS", "KZ", "KAZ", 398, "Kazakhstan, Republic of", Nil) | |
| case KZ_KAZ_EU extends ISOCountryContinent("EU", "KZ", "KAZ", 398, "Kazakhstan, Republic of", Nil) | |
| case JO_JOR_AS extends ISOCountryContinent("AS", "JO", "JOR", 400, "Jordan, Hashemite Kingdom of", Nil) | |
| case KE_KEN_AF extends ISOCountryContinent("AF", "KE", "KEN", 404, "Kenya, Republic of", Nil) | |
| case KP_PRK_AS extends ISOCountryContinent("AS", "KP", "PRK", 408, "Korea, Democratic People's Republic of", Nil) | |
| case KR_KOR_AS extends ISOCountryContinent("AS", "KR", "KOR", 410, "Korea, Republic of", "South Korea" :: Nil) | |
| case XK_XKX_EU extends ISOCountryContinent("EU", "XK", "XKX", -1, "Kosovo, Republic of", Nil) | |
| case KW_KWT_AS extends ISOCountryContinent("AS", "KW", "KWT", 414, "Kuwait, State of", Nil) | |
| case KG_KGZ_AS extends ISOCountryContinent("AS", "KG", "KGZ", 417, "Kyrgyz Republic", "Kyrgyzstan" :: Nil) | |
| case LA_LAO_AS extends ISOCountryContinent("AS", "LA", "LAO", 418, "Lao People's Democratic Republic", "Laos" :: Nil) | |
| case LB_LBN_AS extends ISOCountryContinent("AS", "LB", "LBN", 422, "Lebanon, Lebanese Republic", Nil) | |
| case LS_LSO_AF extends ISOCountryContinent("AF", "LS", "LSO", 426, "Lesotho, Kingdom of", Nil) | |
| case LV_LVA_EU extends ISOCountryContinent("EU", "LV", "LVA", 428, "Latvia, Republic of", Nil) | |
| case LR_LBR_AF extends ISOCountryContinent("AF", "LR", "LBR", 430, "Liberia, Republic of", Nil) | |
| case LY_LBY_AF extends ISOCountryContinent("AF", "LY", "LBY", 434, "Libya, State of", Nil) | |
| case LI_LIE_EU extends ISOCountryContinent("EU", "LI", "LIE", 438, "Liechtenstein, Principality of", Nil) | |
| case LT_LTU_EU extends ISOCountryContinent("EU", "LT", "LTU", 440, "Lithuania, Republic of", Nil) | |
| case LU_LUX_EU extends ISOCountryContinent("EU", "LU", "LUX", 442, "Luxembourg, Grand Duchy of", Nil) | |
| case MO_MAC_AS extends ISOCountryContinent("AS", "MO", "MAC", 446, "Macao, Special Administrative Region of China", Nil) | |
| case MG_MDG_AF extends ISOCountryContinent("AF", "MG", "MDG", 450, "Madagascar, Republic of", Nil) | |
| case MW_MWI_AF extends ISOCountryContinent("AF", "MW", "MWI", 454, "Malawi, Republic of", Nil) | |
| case MY_MYS_AS extends ISOCountryContinent("AS", "MY", "MYS", 458, "Malaysia", Nil) | |
| case MV_MDV_AS extends ISOCountryContinent("AS", "MV", "MDV", 462, "Maldives, Republic of", Nil) | |
| case ML_MLI_AF extends ISOCountryContinent("AF", "ML", "MLI", 466, "Mali, Republic of", Nil) | |
| case MT_MLT_EU extends ISOCountryContinent("EU", "MT", "MLT", 470, "Malta, Republic of", Nil) | |
| case MQ_MTQ_NA extends ISOCountryContinent("NA", "MQ", "MTQ", 474, "Martinique", Nil) | |
| case MR_MRT_AF extends ISOCountryContinent("AF", "MR", "MRT", 478, "Mauritania, Islamic Republic of", Nil) | |
| case MU_MUS_AF extends ISOCountryContinent("AF", "MU", "MUS", 480, "Mauritius, Republic of", Nil) | |
| case MX_MEX_NA extends ISOCountryContinent("NA", "MX", "MEX", 484, "Mexico, United Mexican States", Nil) | |
| case MC_MCO_EU extends ISOCountryContinent("EU", "MC", "MCO", 492, "Monaco, Principality of", Nil) | |
| case MN_MNG_AS extends ISOCountryContinent("AS", "MN", "MNG", 496, "Mongolia", Nil) | |
| case MD_MDA_EU extends ISOCountryContinent("EU", "MD", "MDA", 498, "Moldova, Republic of", Nil) | |
| case ME_MNE_EU extends ISOCountryContinent("EU", "ME", "MNE", 499, "Montenegro", Nil) | |
| case MS_MSR_NA extends ISOCountryContinent("NA", "MS", "MSR", 500, "Montserrat", Nil) | |
| case MA_MAR_AF extends ISOCountryContinent("AF", "MA", "MAR", 504, "Morocco, Kingdom of", Nil) | |
| case MZ_MOZ_AF extends ISOCountryContinent("AF", "MZ", "MOZ", 508, "Mozambique, Republic of", Nil) | |
| case OM_OMN_AS extends ISOCountryContinent("AS", "OM", "OMN", 512, "Oman, Sultanate of", Nil) | |
| case NA_NAM_AF extends ISOCountryContinent("AF", "NA", "NAM", 516, "Namibia, Republic of", Nil) | |
| case NR_NRU_OC extends ISOCountryContinent("OC", "NR", "NRU", 520, "Nauru, Republic of", Nil) | |
| case NP_NPL_AS extends ISOCountryContinent("AS", "NP", "NPL", 524, "Nepal, Federal Democratic Republic of", Nil) | |
| case NL_NLD_EU extends ISOCountryContinent("EU", "NL", "NLD", 528, "Netherlands, Kingdom of the", Nil) | |
| case CW_CUW_NA extends ISOCountryContinent("NA", "CW", "CUW", 531, "Curaçao", Nil) | |
| case AW_ABW_NA extends ISOCountryContinent("NA", "AW", "ABW", 533, "Aruba", Nil) | |
| case SX_SXM_NA extends ISOCountryContinent("NA", "SX", "SXM", 534, "Sint Maarten (Netherlands)", Nil) | |
| case BQ_BES_NA extends ISOCountryContinent("NA", "BQ", "BES", 535, "Bonaire, Sint Eustatius and Saba", Nil) | |
| case NC_NCL_OC extends ISOCountryContinent("OC", "NC", "NCL", 540, "New Caledonia", Nil) | |
| case VU_VUT_OC extends ISOCountryContinent("OC", "VU", "VUT", 548, "Vanuatu, Republic of", Nil) | |
| case NZ_NZL_OC extends ISOCountryContinent("OC", "NZ", "NZL", 554, "New Zealand", Nil) | |
| case NI_NIC_NA extends ISOCountryContinent("NA", "NI", "NIC", 558, "Nicaragua, Republic of", Nil) | |
| case NE_NER_AF extends ISOCountryContinent("AF", "NE", "NER", 562, "Niger, Republic of", Nil) | |
| case NG_NGA_AF extends ISOCountryContinent("AF", "NG", "NGA", 566, "Nigeria, Federal Republic of", Nil) | |
| case NU_NIU_OC extends ISOCountryContinent("OC", "NU", "NIU", 570, "Niue", Nil) | |
| case NF_NFK_OC extends ISOCountryContinent("OC", "NF", "NFK", 574, "Norfolk Island", Nil) | |
| case NO_NOR_EU extends ISOCountryContinent("EU", "NO", "NOR", 578, "Norway, Kingdom of", Nil) | |
| case MP_MNP_OC extends ISOCountryContinent("OC", "MP", "MNP", 580, "Northern Mariana Islands, Commonwealth of the", Nil) | |
| case UM_UMI_NA extends ISOCountryContinent("NA", "UM", "UMI", 581, "United States Minor Outlying Islands", Nil) | |
| case UM_UMI_OC extends ISOCountryContinent("OC", "UM", "UMI", 581, "United States Minor Outlying Islands", Nil) | |
| case FM_FSM_OC extends ISOCountryContinent("OC", "FM", "FSM", 583, "Micronesia, Federated States of", Nil) | |
| case MH_MHL_OC extends ISOCountryContinent("OC", "MH", "MHL", 584, "Marshall Islands, Republic of the", Nil) | |
| case PW_PLW_OC extends ISOCountryContinent("OC", "PW", "PLW", 585, "Palau, Republic of", Nil) | |
| case PK_PAK_AS extends ISOCountryContinent("AS", "PK", "PAK", 586, "Pakistan, Islamic Republic of", Nil) | |
| case PS_PSE_AS extends ISOCountryContinent("AS", "PS", "PSE", 275, "Palestine, State of", "West Bank" :: Nil) | |
| case PA_PAN_NA extends ISOCountryContinent("NA", "PA", "PAN", 591, "Panama, Republic of", Nil) | |
| case PG_PNG_OC extends ISOCountryContinent("OC", "PG", "PNG", 598, "Papua New Guinea, Independent State of", Nil) | |
| case PY_PRY_SA extends ISOCountryContinent("SA", "PY", "PRY", 600, "Paraguay, Republic of", Nil) | |
| case PE_PER_SA extends ISOCountryContinent("SA", "PE", "PER", 604, "Peru, Republic of", Nil) | |
| case PH_PHL_AS extends ISOCountryContinent("AS", "PH", "PHL", 608, "Philippines, Republic of the", Nil) | |
| case PN_PCN_OC extends ISOCountryContinent("OC", "PN", "PCN", 612, "Pitcairn Islands", Nil) | |
| case PL_POL_EU extends ISOCountryContinent("EU", "PL", "POL", 616, "Poland, Republic of", Nil) | |
| case PT_PRT_EU extends ISOCountryContinent("EU", "PT", "PRT", 620, "Portugal, Portuguese Republic", Nil) | |
| case GW_GNB_AF extends ISOCountryContinent("AF", "GW", "GNB", 624, "Guinea-Bissau, Republic of", Nil) | |
| case TL_TLS_AS extends ISOCountryContinent("AS", "TL", "TLS", 626, "Timor-Leste, Democratic Republic of", "East Timor" :: Nil) | |
| case PR_PRI_NA extends ISOCountryContinent("NA", "PR", "PRI", 630, "Puerto Rico, Commonwealth of", Nil) | |
| case QA_QAT_AS extends ISOCountryContinent("AS", "QA", "QAT", 634, "Qatar, State of", Nil) | |
| case RE_REU_AF extends ISOCountryContinent("AF", "RE", "REU", 638, "Reunion", Nil) | |
| case RO_ROU_EU extends ISOCountryContinent("EU", "RO", "ROU", 642, "Romania", Nil) | |
| case RU_RUS_AS extends ISOCountryContinent("AS", "RU", "RUS", 643, "Russian Federation", Nil) | |
| case RU_RUS_EU extends ISOCountryContinent("EU", "RU", "RUS", 643, "Russian Federation", "Russia" :: Nil) | |
| case RW_RWA_AF extends ISOCountryContinent("AF", "RW", "RWA", 646, "Rwanda, Republic of", Nil) | |
| case BL_BLM_NA extends ISOCountryContinent("NA", "BL", "BLM", 652, "Saint Barthelemy", Nil) | |
| case SH_SHN_AF extends ISOCountryContinent("AF", "SH", "SHN", 654, "Saint Helena", Nil) | |
| case KN_KNA_NA extends ISOCountryContinent("NA", "KN", "KNA", 659, "Saint Kitts and Nevis, Federation of", Nil) | |
| case AI_AIA_NA extends ISOCountryContinent("NA", "AI", "AIA", 660, "Anguilla", Nil) | |
| case LC_LCA_NA extends ISOCountryContinent("NA", "LC", "LCA", 662, "Saint Lucia", Nil) | |
| case MF_MAF_NA extends ISOCountryContinent("NA", "MF", "MAF", 663, "Saint Martin", Nil) | |
| case PM_SPM_NA extends ISOCountryContinent("NA", "PM", "SPM", 666, "Saint Pierre and Miquelon", Nil) | |
| case VC_VCT_NA extends ISOCountryContinent("NA", "VC", "VCT", 670, "Saint Vincent and the Grenadines", Nil) | |
| case SM_SMR_EU extends ISOCountryContinent("EU", "SM", "SMR", 674, "San Marino, Republic of", Nil) | |
| case ST_STP_AF extends ISOCountryContinent("AF", "ST", "STP", 678, "São Tomé and Príncipe, Democratic Republic of", Nil) | |
| case SA_SAU_AS extends ISOCountryContinent("AS", "SA", "SAU", 682, "Saudi Arabia, Kingdom of", Nil) | |
| case SN_SEN_AF extends ISOCountryContinent("AF", "SN", "SEN", 686, "Senegal, Republic of", Nil) | |
| case RS_SRB_EU extends ISOCountryContinent("EU", "RS", "SRB", 688, "Serbia, Republic of", Nil) | |
| case SC_SYC_AF extends ISOCountryContinent("AF", "SC", "SYC", 690, "Seychelles, Republic of", Nil) | |
| case SL_SLE_AF extends ISOCountryContinent("AF", "SL", "SLE", 694, "Sierra Leone, Republic of", Nil) | |
| case SG_SGP_AS extends ISOCountryContinent("AS", "SG", "SGP", 702, "Singapore, Republic of", Nil) | |
| case SK_SVK_EU extends ISOCountryContinent("EU", "SK", "SVK", 703, "Slovakia (Slovak Republic)", Nil) | |
| case VN_VNM_AS extends ISOCountryContinent("AS", "VN", "VNM", 704, "Vietnam, Socialist Republic of", Nil) | |
| case SI_SVN_EU extends ISOCountryContinent("EU", "SI", "SVN", 705, "Slovenia, Republic of", Nil) | |
| case SO_SOM_AF extends ISOCountryContinent("AF", "SO", "SOM", 706, "Somalia, Federal Republic of", Nil) | |
| case ZA_ZAF_AF extends ISOCountryContinent("AF", "ZA", "ZAF", 710, "South Africa, Republic of", Nil) | |
| case ZW_ZWE_AF extends ISOCountryContinent("AF", "ZW", "ZWE", 716, "Zimbabwe, Republic of", Nil) | |
| case ES_ESP_EU extends ISOCountryContinent("EU", "ES", "ESP", 724, "Spain, Kingdom of", Nil) | |
| case SS_SSD_AF extends ISOCountryContinent("AF", "SS", "SSD", 728, "South Sudan, Republic of", Nil) | |
| case SD_SDN_AF extends ISOCountryContinent("AF", "SD", "SDN", 729, "Sudan, Republic of the", Nil) | |
| case EH_ESH_AF extends ISOCountryContinent("AF", "EH", "ESH", 732, "Sahrawi Arab Democratic Republic", Nil) | |
| case SR_SUR_SA extends ISOCountryContinent("SA", "SR", "SUR", 740, "Suriname, Republic of", Nil) | |
| case SJ_SJM_EU extends ISOCountryContinent("EU", "SJ", "SJM", 744, "Svalbard & Jan Mayen Islands", Nil) | |
| case SZ_SWZ_AF extends ISOCountryContinent("AF", "SZ", "SWZ", 748, "Eswatini, Kingdom of", "Swaziland" :: Nil) | |
| case SE_SWE_EU extends ISOCountryContinent("EU", "SE", "SWE", 752, "Sweden, Kingdom of", Nil) | |
| case CH_CHE_EU extends ISOCountryContinent("EU", "CH", "CHE", 756, "Switzerland, Swiss Confederation", Nil) | |
| case SY_SYR_AS extends ISOCountryContinent("AS", "SY", "SYR", 760, "Syrian Arab Republic", "Syria" :: Nil) | |
| case TJ_TJK_AS extends ISOCountryContinent("AS", "TJ", "TJK", 762, "Tajikistan, Republic of", Nil) | |
| case TH_THA_AS extends ISOCountryContinent("AS", "TH", "THA", 764, "Thailand, Kingdom of", Nil) | |
| case TG_TGO_AF extends ISOCountryContinent("AF", "TG", "TGO", 768, "Togo, Togolese Republic", Nil) | |
| case TK_TKL_OC extends ISOCountryContinent("OC", "TK", "TKL", 772, "Tokelau", Nil) | |
| case TO_TON_OC extends ISOCountryContinent("OC", "TO", "TON", 776, "Tonga, Kingdom of", Nil) | |
| case TT_TTO_NA extends ISOCountryContinent("NA", "TT", "TTO", 780, "Trinidad and Tobago, Republic of", Nil) | |
| case AE_ARE_AS extends ISOCountryContinent("AS", "AE", "ARE", 784, "United Arab Emirates", Nil) | |
| case TN_TUN_AF extends ISOCountryContinent("AF", "TN", "TUN", 788, "Tunisia, Republic of", Nil) | |
| case TR_TUR_AS extends ISOCountryContinent("AS", "TR", "TUR", 792, "Turkey, Republic of", Nil) | |
| case TR_TUR_EU extends ISOCountryContinent("EU", "TR", "TUR", 792, "Turkey, Republic of", Nil) | |
| case TM_TKM_AS extends ISOCountryContinent("AS", "TM", "TKM", 795, "Turkmenistan", Nil) | |
| case TC_TCA_NA extends ISOCountryContinent("NA", "TC", "TCA", 796, "Turks and Caicos Islands", Nil) | |
| case TV_TUV_OC extends ISOCountryContinent("OC", "TV", "TUV", 798, "Tuvalu", Nil) | |
| case UG_UGA_AF extends ISOCountryContinent("AF", "UG", "UGA", 800, "Uganda, Republic of", Nil) | |
| case UA_UKR_EU extends ISOCountryContinent("EU", "UA", "UKR", 804, "Ukraine", Nil) | |
| case MK_MKD_EU extends ISOCountryContinent("EU", "MK", "MKD", 807, "North Macedonia, Republic of", "Macedonia" :: Nil) | |
| case EG_EGY_AF extends ISOCountryContinent("AF", "EG", "EGY", 818, "Egypt, Arab Republic of", Nil) | |
| case EG_EGY_AS extends ISOCountryContinent("AS", "EG", "EGY", 818, "Egypt, Arab Republic of", Nil) | |
| case GB_GBR_EU extends ISOCountryContinent("EU", "GB", "GBR", 826, "United Kingdom of Great Britain & Northern Ireland", Nil) | |
| case GG_GGY_EU extends ISOCountryContinent("EU", "GG", "GGY", 831, "Guernsey, Bailiwick of", Nil) | |
| case JE_JEY_EU extends ISOCountryContinent("EU", "JE", "JEY", 832, "Jersey, Bailiwick of", Nil) | |
| case IM_IMN_EU extends ISOCountryContinent("EU", "IM", "IMN", 833, "Isle of Man", Nil) | |
| case TZ_TZA_AF extends ISOCountryContinent("AF", "TZ", "TZA", 834, "Tanzania, United Republic of", Nil) | |
| case US_USA_NA extends ISOCountryContinent("NA", "US", "USA", 840, "United States of America", Nil) | |
| case VI_VIR_NA extends ISOCountryContinent("NA", "VI", "VIR", 850, "United States Virgin Islands", Nil) | |
| case BF_BFA_AF extends ISOCountryContinent("AF", "BF", "BFA", 854, "Burkina Faso", Nil) | |
| case UY_URY_SA extends ISOCountryContinent("SA", "UY", "URY", 858, "Uruguay, Oriental Republic of", Nil) | |
| case UZ_UZB_AS extends ISOCountryContinent("AS", "UZ", "UZB", 860, "Uzbekistan, Republic of", Nil) | |
| case VE_VEN_SA extends ISOCountryContinent("SA", "VE", "VEN", 862, "Venezuela, Bolivarian Republic of", Nil) | |
| case WF_WLF_OC extends ISOCountryContinent("OC", "WF", "WLF", 876, "Wallis and Futuna", Nil) | |
| case WS_WSM_OC extends ISOCountryContinent("OC", "WS", "WSM", 882, "Samoa, Independent State of", Nil) | |
| case YE_YEM_AS extends ISOCountryContinent("AS", "YE", "YEM", 887, "Yemen", Nil) | |
| case ZM_ZMB_AF extends ISOCountryContinent("AF", "ZM", "ZMB", 894, "Zambia, Republic of", Nil) | |
| case XD_null_AS extends ISOCountryContinent("AS", "XD", "<null>", -1, "United Nations Neutral Zone", Nil) | |
| case XS_null_AS extends ISOCountryContinent("AS", "XS", "<null>", -1, "Spratly Islands", Nil) | |
| case XX_null_OC extends ISOCountryContinent("OC", "XX", "<null>", -1, "Disputed Territory", Nil) | |
| } | |
| enum ISOContinent(val continentCode: String, val name: String) { | |
| case AF extends ISOContinent("AF", "Africa") | |
| case NA extends ISOContinent("NA", "North America") | |
| case OC extends ISOContinent("OC", "Oceania") | |
| case AN extends ISOContinent("AN", "Antarctica") | |
| case AS extends ISOContinent("AS", "Asia") | |
| case EU extends ISOContinent("EU", "Europe") | |
| case SA extends ISOContinent("SA", "South America") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private val countryIndex = | |
| IArray.unsafeFromArray(ISOCountryContinent.values) | |
| private val continentIndex = | |
| ISOContinent.values.map(c => c.continentCode -> c).toMap | |
| private val dynamicCommonNames = | |
| def subNames(name: String): List[String] = | |
| name | |
| .split(" ") | |
| .toList | |
| .scanLeft(List.empty[String])((acc, s) => acc :+ s) | |
| .tail | |
| .map(_.mkString(" ")) | |
| def formatName(country: ISOCountryContinent): List[String] = | |
| val parts = country.name.split(',').toSeq | |
| parts match | |
| case Seq(name) => subNames(name) | |
| case Seq(name, qual) => subNames(name) ::: s"${qual.drop(1)} $name" :: Nil | |
| case Seq(name, qual, desc) => | |
| subNames(name) ::: s"${qual.drop(1)} $name, $desc" :: Nil | |
| countryIndex.flatMap(c => (formatName(c) ::: c.commonNames).map(_ -> c)).toMap | |
| def lookupCountry(country: String): Option[ISOCountryContinent] = | |
| dynamicCommonNames | |
| .get(country) | |
| .orElse(countryIndex.find(_.iso3Code == country)) | |
| .orElse(countryIndex.find(_.iso2Code == country)) | |
| def lookupContinent(continent: ISOCountryContinent): Option[ISOContinent] = | |
| continentIndex.get(continent.continentCode) | |
| def continentFromCountry(country: String): Option[ISOContinent] = | |
| lookupCountry(country).flatMap(lookupContinent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment