Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active August 11, 2020 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/ebf0d24a256af18617cba2ff59121202 to your computer and use it in GitHub Desktop.
Save isaacabraham/ebf0d24a256af18617cba2ff59121202 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ICantSeeSharp
{
static class DateStream
{
public static IEnumerable<DateTime> AllDates
{
get {
var theDate = DateTime.MinValue;
while (theDate < DateTime.MaxValue.Date) {
yield return theDate;
theDate = theDate.AddDays(1);
}
}
}
public static Func<DateTime, bool> IsDayOfMonth(params int[] targets) => (DateTime actual) => targets.Contains(actual.Day);
public static Func<DateTime, bool> IsDay(params DayOfWeek[] targets) => (DateTime actual) => targets.Contains(actual.DayOfWeek);
public static Func<DateTime, bool> IsYear(params int[] target) => (DateTime actual) => target.Contains(actual.Year);
}
}
namespace App
{
using static ICantSeeSharp.DateStream;
class Program
{
static void Main(string[] args)
{
var monOrWedStartOfMonth =
AllDates
.Where(IsYear(2020))
.Where(IsDay(DayOfWeek.Monday, DayOfWeek.Wednesday))
.Where(IsDayOfMonth(1))
.ToArray();
foreach (var date in monOrWedStartOfMonth)
{
System.Console.WriteLine($"{date} is {date.DayOfWeek}");
}
}
}
}
open System
[<AutoOpen>]
module DateStream =
let allDates = seq {
let mutable theDate = DateTime.MinValue
while theDate < DateTime.MaxValue.Date do
theDate
theDate <- theDate.AddDays 1.
}
let isDayOfMonth targets (actual:DateTime) = targets |> Seq.contains actual.Day
let isDay targets (actual:DateTime) = targets |> Seq.contains actual.DayOfWeek
let isYear targets (actual:DateTime) = targets |> Seq.contains actual.Year
open System
[<EntryPoint>]
let main argv =
allDates
|> Seq.filter (isYear [ 2020 ])
|> Seq.filter (isDay [ DayOfWeek.Monday; DayOfWeek.Wednesday ])
|> Seq.filter (isDayOfMonth [ 1 ])
|> Seq.iter (printfn "%O")
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment