Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.