Skip to content

Instantly share code, notes, and snippets.

@lambdahands
Created December 9, 2014 21:50
Show Gist options
  • Save lambdahands/3a1caaec0a9eb967b60e to your computer and use it in GitHub Desktop.
Save lambdahands/3a1caaec0a9eb967b60e to your computer and use it in GitHub Desktop.
flow-type + immutable-js
/* @flow */
var T = require('immutable');
type Branch = T.List<string>
type TrainLine = T.Map<string, Branch>
type CTAMap = T.Map<string, TrainLine>
/*
* Representation of Chicago's train stops and branches
*/
var ctaMap: CTAMap = new T.Map();
var blueLine: TrainLine = new T.Map().set("O'Hare Branch",
T.List.of(
"O'Hare",
"Rosemont",
"Cumberland",
"Harlem",
"Jefferson Park",
"Montrose",
"Irving Park",
"Addison",
"Belmont",
"Logan Square",
"California",
"Western",
"Damen"
)
).set("Milwaulkee-Dearborn Subway",
T.List.of(
"Division",
"Chicago",
"Grand",
"Clark / Lake",
"Washington",
"Monroe",
"Jackson",
"LaSalle",
"Clinton"
)
).set("Forest Park 'Congress' Branch",
T.List.of(
"UIC-Halsted",
"Racine",
"Illinois Medical Center",
"Western",
"Kedzie-Homan",
"Pulaski",
"Cicero",
"Austin",
"Oak Park",
"Harlem",
"Forest Park"
)
);
ctaMap = ctaMap.set('blue', blueLine);
// ERROR: call of method count
// Method cannot be called on optional of Branch
var numStops: number = blueLine.toSeq().reduce((r, n) => r + n.count(), 0);
// ERROR: call of method count
// Method cannot be called on optional of Branch
var numStopsByBranch = blueLine.map((v, k) => v.count());
console.log("The CTA Blue Line has " + numStops + " stops.")
// => The CTA Blue Line has 33 stops.
console.log(numStopsByBranch);
// => Map { O'Hare Branch: 13, Milwaulkee-Dearborn Subway: 9, Forest Park 'Congress' Branch: 11 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment