F# 测量单位 - “提升” 浮动值;
从 csv 文件导入数字时,我需要将它们转换为带单位的浮点数。
目前我使用内联函数执行此操作:
data |> List.map float |> List.map (fun n -> n * 1.0<m>)
但我想知道是否有更优雅的方法来执行此操作 - 或者我是否必须使用转换函数创建自己的“单位”模块?
像这样的事情真是太好了,但我怀疑这是可能的......
data |> List.map float |> List.map lift<m>
这与我之前的问题相反(如何一般删除 F# 测量单位)。
更新:对于自制单位,我已经尝试过这个,效果很好:
[<Measure>]
type km =
static member lift (v:float) = v * 1.0<km>
data |> List.map float |> List.map km.lift
或,按照此答案中的问题
data |> List.map (float >> km.lift)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来度量单位暂时不能作为类型参数(不知道这是否会改变)。 因此,编写此内容的最短方法是:
编辑
现在另请参阅
FloatWithMeasure
此处http://msdn.microsoft.com/en-us/library/ee806527(VS.100).aspx
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:
EDIT
See also now
FloatWithMeasure
herehttp://msdn.microsoft.com/en-us/library/ee806527(VS.100).aspx
有什么理由必须映射两次吗? 这是怎么回事:
Is there any reason why you have to map twice? What's wrong with this: