3

When importing numbers from a csv file, I need to convert them to floats with unit.

Currently I do this with an inline function:

data |> List.map float |> List.map (fun n -> n * 1.0<m>)

But I'm wondering if there is a more elegant way to do this - or do I have to create my own 'units' module with conversion functions?

What would be really nice would be something like this, but I doubt it's possible...

data |> List.map float |> List.map lift<m>

This is the opposite of my previous question (How to generically remove F# Units of measure).

UPDATE: For homemade units, I've tried this, which works ok:

[<Measure>]
type km = 
    static member lift (v:float) = v * 1.0<km>

data |> List.map float |> List.map km.lift

or, following the question in this answer

data |> List.map (float >> km.lift)
Community
  • 1
  • 1
Benjol
  • 63,995
  • 54
  • 186
  • 268

2 Answers2

2

It looks like units of measure can't be type parameters for the moment (no idea if this will change). So the shortest way to write this is:

data |> List.map float |> List.map ((*) 1.0<m>)

EDIT

See also now FloatWithMeasure here

http://msdn.microsoft.com/en-us/library/ee806527(VS.100).aspx

Brian
  • 117,631
  • 17
  • 236
  • 300
Robert
  • 6,407
  • 2
  • 34
  • 41
1

Is there any reason why you have to map twice? What's wrong with this:

data |> List.map (fun x -> (float x) * 1.0<m>)
Juliet
  • 80,494
  • 45
  • 196
  • 228
  • Yeah, I could, have done so, but the idea was that my lift function would be generally applicable to floats. But see corrected code in UPDATE in question. – Benjol Jan 09 '09 at 10:04