4

I'm trying to refactor some python code which I'm using for financial analytics processing into F#, and I'm having difficulty in replicating some of my beloved Python datatypes. For instance in Python I could happily declare a variable like this:

timeSeries = { "20110131":1.5, "20110228":1.5, "20110331":1.5, 
               "20110431":1.5, "20110531":1.5 }

And then throw it around between functions but I don't know the equivalent in F#. I've tried this:

let timeSeries = ( ["20110131":1.5], ["20110228":1.5], ["20110331":1.5], 
                   ["20110431":1.5], ["20110531":1.5] )

But of course it fails... I'm aware that somebody out there is probably spluttering with laughter, but I'm very new to F# and it feels constrictive compared to Python.

I'm familiar with .NET as well, it's just that F# seems to sit somewhere between scripting and "proper" languages like C# and I haven't quite grokked how to use it effectively yet - everything takes ages.

pad
  • 41,040
  • 7
  • 92
  • 166
Mel Padden
  • 983
  • 1
  • 9
  • 21
  • 4
    What does `:` mean in Python? – pad Jan 30 '12 at 19:34
  • 1
    To be clear, the listed declaration for timeSeries in Python will raise a SyntaxError. The square brackets are used to define a list, e.g. [1,2,3], but the colons are used between keys and values in a dictionary, e.g. {1:2, "name": "Fred"}. – DSM Jan 30 '12 at 20:09
  • @DSM, thanks, edited. Was in a rush... – Mel Padden Jan 31 '12 at 09:55

2 Answers2

9

To be honest, your dictionary declaration in Python doesn't look much different from what you can declare in F#:

let timeSeries = dict [
             "20110131", 1.5; // ',' is tuple delimiter and ';' is list delimiter
             "20110228", 1.5;
             "20110331", 1.5; 
             "20110431", 1.5; 
             "20110531", 1.5; // The last ';' is optional
             ]

Because : is used for type annotation in F#, you couldn't redefine it. But you can create a custom infix operator to make the code more readable and closer to what you usually do in Python:

let inline (=>) a b = a, b

let timeSeries = dict [
         "20110131" => 1.5;
         "20110228" => 1.5;
         "20110331" => 1.5; 
         "20110431" => 1.5; 
         "20110531" => 1.5;
         ]
pad
  • 41,040
  • 7
  • 92
  • 166
  • 2
    How is `=>` better than `,` which is built-in? It is useful on occasion for combining tuple creation with other operations, e.g., http://stackoverflow.com/a/7410842/162396. But for the typical case it only adds a character. – Daniel Jan 30 '12 at 19:53
  • It's a matter of style. I find it more readable with dictionaries, but not much diference with lists, arrays, etc. – pad Jan 30 '12 at 20:16
  • I have to agree that built-in syntax is almost always the desired result. Thanks for the explanation. – Mel Padden Jan 31 '12 at 09:57
2

You may find this answer helpful. It looks like you want a dictionary. In case you don't already know, F# supports something akin to Python's generator expressions, called sequence expressions [MSDN], which you may also find useful. For example:

let d = dict [ for i in 0 .. 10 -> i, -i ]
Community
  • 1
  • 1
Daniel
  • 47,404
  • 11
  • 101
  • 179