Skip to content

Instantly share code, notes, and snippets.

@srenault
Last active August 29, 2015 14:08
Show Gist options
  • Save srenault/3a8b08b73abb5bc520d3 to your computer and use it in GitHub Desktop.
Save srenault/3a8b08b73abb5bc520d3 to your computer and use it in GitHub Desktop.
mithril 'propMap'
function propMap<T>(values: Array<[string, T]>): (key: string, value?: T) => T {
var _prop = (map?: Map<string, T>) => {
var prop = (key: string, value?: T) => {
if (value !== undefined) map.set(key, value);
return map.get(key);
}
return prop;
}
var map = values.reduce((acc, pair) => {
acc.set(pair[0], pair[1]);
return acc;
}, new Map<string, T>());
return _prop(map);
}
}
@Warry
Copy link

Warry commented Nov 6, 2014

Example in elm : https://gist.github.com/Warry/b4382a5b4373de57f5ba

propMap : (a -> comparable) -> [a] -> Dict.Dict comparable a
propMap getKey values = Dict.fromList (map (\v -> (getKey v, v)) values)

It allows to check at compile time that key is in T (to make an analogy)

hmmmm. I guess I was mistaken on the purpose of your function! I thought it was transforming an array to map using a property of the object. It looks like it's transforming an array of tuples to a map... My bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment