gpt4 book ai didi

f# - 顺序 - 随后几年的同一日期

转载 作者:行者123 更新时间:2023-12-01 00:05:20 29 4
gpt4 key购买 nike

我想创建一个序列,当星期几与函数参数中的日期相同时,它会返回所有年份的结果(例如:自开始日期起,2 月 12 日为星期日的所有年份)。

let myDate (dw:System.DayOfWeek) (start:System.DateTime) =
seq {
...
}

希望你明白我的意思。

最佳答案

这个怎么样:

let myDate (dw:System.DayOfWeek) (start:System.DateTime) =
Seq.initInfinite ((+)1) // from 1..∞
|> Seq.map start.AddYears
|> Seq.takeWhile (fun x -> x < DateTime.MaxValue)
|> Seq.filter (fun x -> x.DayOfWeek = dw)
|> Seq.map (fun x -> x.Year)

myDate System.DayOfWeek.Monday (new DateTime(2001,1,1)) |> Dump

let myDate (dw:System.DayOfWeek) (start:System.DateTime) =
let rec inner i =
seq {
let someDay = start.AddYears(i)
if someDay.DayOfWeek = dw then yield someDay.Year
yield! inner (i+1)
}
inner 1

myDate System.DayOfWeek.Monday (new DateTime(2001,12,1)) |> Dump

关于f# - 顺序 - 随后几年的同一日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42187818/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com