I’m looking for something akin to the dict/list/set comprehensions in Python. In Python you can do:
[x+2 for x in list if x > 10]
and in F#
[for x in list do if x > 10 yield x+2]
but in Python, you can also do:
{x+2 for x in list if x > 10}
to generate Sets, and
{k: (v+10)/2 for k, v in list.items() if k > 5}
to generate Dicts. Is there any equivalent (both in terms of function and in terms of overall tidyness) syntax for this sort of thing in F#?
There is a
setfunction acceptingseq<_>, so you can do:Same with
dict. Here are the relevant docs:set: http://msdn.microsoft.com/en-us/library/ee353747.aspxdict: http://msdn.microsoft.com/en-us/library/ee353774.aspx