Colecciones de estilo Python en F #

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.

preguntado el 30 de enero de 12 a las 19:01

¿Qué : mean in Python? -

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, thanks, edited. Was in a rush... -

2 Respuestas

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

Porque : 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;

Respondido 18 Feb 12, 17:02

¿Cómo es => mejor que , which is built-in? It is useful on occasion for combining tuple creation with other operations, e.g., But for the typical case it only adds a character. - Daniel

It's a matter of style. I find it more readable with dictionaries, but not much diference with lists, arrays, etc. - almohadilla

I have to agree that built-in syntax is almost always the desired result. Thanks for the explanation. - Mel Padden

Puedes encontrar esta respuesta 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 expresiones de secuencia [MSDN], which you may also find useful. For example:

let d = dict [ for i in 0 .. 10 -> i, -i ]

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.