从现有价值中投射新价值 [英] Project new values from existing value
问题描述
我正在写我的第一个F#程序,目的只是为了学习F#.
I'm writing my very first F# program, the aim being simply to learn F#.
我想要提供的是日期列表,以及这些日期的属性(例如DayOfWeek,DayOfMonth).我已经设法提供了日期列表,并且我知道.net Framework为我提供了提取所有属性所需的一切,但我不知道如何将属性添加为列表中的新列. 这是我到目前为止的内容:
What I want to is provide a list of dates, and attributes (e.g.DayOfWeek, DayOfMonth) of those dates. I have managed to provide the list of dates and I know that the .net Framework gives me everything I need to extract all the attributes, I just can't figure out how to add the attribute as new columns in my list. Here's what I have so far:
type Span = Span of TimeSpan with
static member (+) (d:DateTime, Span wrapper) = d + wrapper //this is defining the + operator
static member Zero = Span(new TimeSpan(0L))
type Dates() =
let a = DateTime.Parse("01/12/2013")
let b =DateTime.Parse("02/12/2013")
let ts = TimeSpan.FromDays(1.0)
member this.Get() = [a .. Span(ts) .. b]
let mydates = new Dates()
mydates.Get()
运行该代码时,我得到一个DateTime值列表,该列表中有2条记录.我现在可以做这样的事情:
When I run that code I get a list of DateTime values, with 2 records in the list. I can now do something like this:
mydates.Get() |> List.map (fun x -> x.DayOfWeek);;
返回:
val it:DayOfWeek list = [Sunday;星期一]
val it : DayOfWeek list = [Sunday; Monday]
或
mydates.Get() |> List.map (fun x -> x.DayOfYear);;
返回:
val it:int list = [335; 336]
val it : int list = [335; 336]
那太好了,但是我想做的是投影一个包含2个列"的列表(如果列是正确的单词),以便我的输出是(类似):
That's all great, however what I would like to do is project a list that has 2 "columns" (if columns is the right word) so that my output is (something like):
val it:int list = [(Sunday,335); (星期一,336)]
val it : int list = [(Sunday,335); (Monday,336)]
我希望这可以解释我的追求.
I hope that explains what I'm after.
谢谢
杰米
推荐答案
在您的示例中,解决方案很简单,使地图返回像这样的元组
For your example, the solution is simple, make the map return a tuple like so
mydates.Get() |> List.map (fun x -> x.DayOfWeek,x.DayOfYear);;
这篇关于从现有价值中投射新价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!