Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# generator of daterange?

Tags:

generator

f#

I'm attempting to write a function that generates a list of DateTimes using the generator syntax:

let dateRange = 

    let endDate = System.DateTime.Parse("6/1/2010")
    let startDate = System.DateTime.Parse("3/1/2010")

    seq {
          for date in startDate..endDate do
              if MyDateClass.IsBusinessDay(date) then yield date
        }

but the generator ('seq') block does not parse correctly. It wants a timespan. While the generator syntax seems perfect for what I want to do, it's rather non-intuitive for anything but two numbers.

  1. Is it possible to use the generator syntax to create a DateTime range?
  2. is there a better way to think about how to create the range than I wrote (i.e. the 'in' clause)
like image 647
Kevin Won Avatar asked Sep 15 '10 19:09

Kevin Won


People also ask

How can I open my old facebook account without password?

Keep in mind that you'll need access to the email associated with your account. Tap Forgot Password?. Type the email, mobile phone number, full name or username associated with your account, then tap Search. Follow the on-screen instructions.


1 Answers

If TimeSpan had a static Zero property, then you could do something like startDate .. TimeSpan(1,0,0,0) .. endDate. Even though it doesn't, you can create a wrapper that will do the same thing:

open System

type TimeSpanWrapper = { timeSpan : TimeSpan } with
  static member (+)(d:DateTime, tw) = d + tw.timeSpan
  static member Zero = { timeSpan = TimeSpan(0L) }

let dateRange =
    let endDate = System.DateTime.Parse("6/1/2010")
    let startDate = System.DateTime.Parse("5/1/2010")
    let oneDay = { timeSpan = System.TimeSpan(1,0,0,0) }

    seq {
          for date in startDate .. oneDay .. endDate do
             if MyDateClass.IsBusinessDay(date) then yield date
        }
like image 81
kvb Avatar answered Oct 03 '22 05:10

kvb