Skip to content

Instantly share code, notes, and snippets.

@aryairani
Created April 18, 2024 14:55
Show Gist options
  • Save aryairani/bce4bc6c21b47bf87840421dcb28b8cd to your computer and use it in GitHub Desktop.
Save aryairani/bce4bc6c21b47bf87840421dcb28b8cd to your computer and use it in GitHub Desktop.
-- simpleHttp.main :
-- '{IO, Exception} ServiceHash HttpRequest HttpResponse
-- simpleHttp.main =
-- Cloud.main do
-- helloService : HttpRequest -> HttpResponse
-- helloService request =
-- HttpResponse.ok (Body (Text.toUtf8 "Hello world"))
-- deployHttp !Environment.default helloService
service.lookupEVs = Route.run do
state = route GET (s "lookupEVs"/Parser.text)
match abbreviationToJurisdiction state with
Some evs -> ok.text (Nat.toText evs)
None -> respond.notFound.text ("Unknown state: " Text.++ state)
deployLocal = Cloud.main.local.interactive deploy
deployCloud = Cloud.main deploy
deploy = do
serviceHash = deployHttp !Environment.default lookupEVs
name = ServiceName.create "electoral-vote-lookup-service"
ServiceName.assign name serviceHash
type ElectoralJurisdictions
= Alabama
| Alaska
| Arizona
| Arkansas
| California
| Colorado
| Connecticut
| Delaware
| DistrictOfColumbia
| Florida
| Georgia
| Hawaii
| Idaho
| Illinois
| Indiana
| Iowa
| Kansas
| Kentucky
| Louisiana
| Maine
| Maryland
| Massachusetts
| Michigan
| Minnesota
| Mississippi
| Missouri
| Montana
| Nebraska
| Nevada
| NewHampshire
| NewJersey
| NewMexico
| NewYork
| NorthCarolina
| NorthDakota
| Ohio
| Oklahoma
| Oregon
| Pennsylvania
| RhodeIsland
| SouthCarolina
| SouthDakota
| Tennessee
| Texas
| Utah
| Vermont
| Virginia
| Washington
| WestVirginia
| Wisconsin
| Wyoming
> List.map (cases
(abbrev, count) -> match abbreviationToJurisdiction abbrev with
Some jurisdiction ->
if jurisdictionToElectoralVotes jurisdiction == count then (true, jurisdiction)
else (false, jurisdiction)
None -> bug ("Unknown abbreviation" Text.++ abbrev)
) (Map.toList !genStateEvMap)
jurisdictionToElectoralVotes : ElectoralJurisdictions -> Nat
jurisdictionToElectoralVotes = cases
Alabama -> 9
Alaska -> 3
Arizona -> 11
Arkansas -> 6
California -> 54
Colorado -> 10
Connecticut -> 7
Delaware -> 3
DistrictOfColumbia -> 3
Florida -> 30
Georgia -> 16
Hawaii -> 4
Idaho -> 4
Illinois -> 19
Indiana -> 11
Iowa -> 6
Kansas -> 6
Kentucky -> 8
Louisiana -> 8
Maine -> 4
Maryland -> 10
Massachusetts -> 11
Michigan -> 15
Minnesota -> 10
Mississippi -> 6
Missouri -> 10
Montana -> 4
Nebraska -> 5
Nevada -> 6
NewHampshire -> 4
NewJersey -> 14
NewMexico -> 5
NewYork -> 28
NorthCarolina -> 16
NorthDakota -> 3
Ohio -> 17
Oklahoma -> 7
Oregon -> 8
Pennsylvania -> 19
RhodeIsland -> 4
SouthCarolina -> 9
SouthDakota -> 3
Tennessee -> 11
Texas -> 40
Utah -> 6
Vermont -> 3
Virginia -> 13
Washington -> 12
WestVirginia -> 4
Wisconsin -> 10
Wyoming -> 3
jurisdictionToAbbreviation : ElectoralJurisdictions -> Text
jurisdictionToAbbreviation = cases
Alabama -> "AL"
Alaska -> "AK"
Arizona -> "AZ"
Arkansas -> "AR"
California -> "CA"
Colorado -> "CO"
Connecticut -> "CT"
Delaware -> "DE"
DistrictOfColumbia -> "DC"
Florida -> "FL"
Georgia -> "GA"
Hawaii -> "HI"
Idaho -> "ID"
Illinois -> "IL"
Indiana -> "IN"
Iowa -> "IA"
Kansas -> "KS"
Kentucky -> "KY"
Louisiana -> "LA"
Maine -> "ME"
Maryland -> "MD"
Massachusetts -> "MA"
Michigan -> "MI"
Minnesota -> "MN"
Mississippi -> "MS"
Missouri -> "MO"
Montana -> "MT"
Nebraska -> "NE"
Nevada -> "NV"
NewHampshire -> "NH"
NewJersey -> "NJ"
NewMexico -> "NM"
NewYork -> "NY"
NorthCarolina -> "NC"
NorthDakota -> "ND"
Ohio -> "OH"
Oklahoma -> "OK"
Oregon -> "OR"
Pennsylvania -> "PA"
RhodeIsland -> "RI"
SouthCarolina -> "SC"
SouthDakota -> "SD"
Tennessee -> "TN"
Texas -> "TX"
Utah -> "UT"
Vermont -> "VT"
Virginia -> "VA"
Washington -> "WA"
WestVirginia -> "WV"
Wisconsin -> "WI"
Wyoming -> "WY"
abbreviationToJurisdiction : Text -> Optional ElectoralJurisdictions
abbreviationToJurisdiction = cases
"AL" -> Some Alabama
"AK" -> Some Alaska
"AZ" -> Some Arizona
"AR" -> Some Arkansas
"CA" -> Some California
"CO" -> Some Colorado
"CT" -> Some Connecticut
"DE" -> Some Delaware
"DC" -> Some DistrictOfColumbia
"FL" -> Some Florida
"GA" -> Some Georgia
"HI" -> Some Hawaii
"ID" -> Some Idaho
"IL" -> Some Illinois
"IN" -> Some Indiana
"IA" -> Some Iowa
"KS" -> Some Kansas
"KY" -> Some Kentucky
"LA" -> Some Louisiana
"ME" -> Some Maine
"MD" -> Some Maryland
"MA" -> Some Massachusetts
"MI" -> Some Michigan
"MN" -> Some Minnesota
"MS" -> Some Mississippi
"MO" -> Some Missouri
"MT" -> Some Montana
"NE" -> Some Nebraska
"NV" -> Some Nevada
"NH" -> Some NewHampshire
"NJ" -> Some NewJersey
"NM" -> Some NewMexico
"NY" -> Some NewYork
"NC" -> Some NorthCarolina
"ND" -> Some NorthDakota
"OH" -> Some Ohio
"OK" -> Some Oklahoma
"OR" -> Some Oregon
"PA" -> Some Pennsylvania
"RI" -> Some RhodeIsland
"SC" -> Some SouthCarolina
"SD" -> Some SouthDakota
"TN" -> Some Tennessee
"TX" -> Some Texas
"UT" -> Some Utah
"VT" -> Some Vermont
"VA" -> Some Virginia
"WA" -> Some Washington
"WV" -> Some WestVirginia
"WI" -> Some Wisconsin
"WY" -> Some Wyoming
_ -> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment