Last active
August 29, 2015 14:08
-
-
Save srenault/3a8b08b73abb5bc520d3 to your computer and use it in GitHub Desktop.
mithril 'propMap'
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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example in elm : https://gist.github.com/Warry/b4382a5b4373de57f5ba
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.