Skip to content

Commit

Permalink
Add generic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Release-Candidate committed Apr 16, 2021
1 parent 705ac0d commit 2ac5f52
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
52 changes: 24 additions & 28 deletions src/TzolkinDate/Generics.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ module Generics=
(gregorian: DateTime)
: ^T
=
let (refDate, refTzolkin) = referenceDate
let formatProvider = System.Globalization.DateTimeFormatInfo.InvariantInfo
let reference = System.DateTime.ParseExact (refDate, "dd.MM.yyyy", formatProvider)
let dayDiff: TimeSpan = gregorian - reference
(^T: (static member op_Addition : ^T * TimeSpan -> ^T) refTzolkin, dayDiff)
let (refDate, refTzolkin) = referenceDate
let formatProvider = System.Globalization.DateTimeFormatInfo.InvariantInfo
let reference = System.DateTime.ParseExact (refDate, "dd.MM.yyyy", formatProvider)
let dayDiff: TimeSpan = gregorian - reference
(^T: (static member ( + ) : ^T * TimeSpan -> ^T) refTzolkin, dayDiff)

/// Return the next Gregorian date after `start` with a Tzolk’in day type.
let inline internal getNext
Expand All @@ -40,13 +40,12 @@ module Generics=
(tzolkinDate: ^T)
(start: DateTime)
=
let startTzolkin = fromDate referenceDate start
let dayDiff = if tzolkinDate - startTzolkin = 0 then numElem else tzolkinDate - startTzolkin
start + System.TimeSpan.FromDays (float dayDiff)
let startTzolkin = fromDate referenceDate start
let dayDiff = if tzolkinDate - startTzolkin = 0 then numElem else tzolkinDate - startTzolkin
start + System.TimeSpan.FromDays (float dayDiff)

/// Add a `TzolkinGlyph`to the given list of `TzolkinGlyph`, to a length of `length`.
/// Helper function.
let rec internal addDate getTzolkin length num start list =
/// Add a gregorian date of a Tzolk’in day to the given list, to a length of `length`.
let rec private addDate getTzolkin length num start list =
let next = getTzolkin start
let nextNum = num + 1
if nextNum < length
Expand All @@ -62,21 +61,18 @@ module Generics=

if last = start then last + System.TimeSpan.FromDays (float -numElem) else last

/// Return a list of Gregorian dates before `start` with the same Tzolk’in day glyph
/// `tzolkinDate`. The number of elements in the returned list is `numDates`.
/// If `start` has a Tzolk’in day glyph of `tzolkinDate` the first element is the last
/// Gregorian date with a Tzolk’in day glyph of `tzolkinDate` (260 days before `start`).
///
/// Params:
/// `numDates` The number of returned dates in the list.
/// `tzolkinDate` The Tzolk’in day glyph to search for.
/// `start` The Gregorian date to start the search.
///
/// Returns:
/// A list with the last `numDates` Gregorian dates (backwards in time
/// before the date `start`) that have the same Tzolk’in day glyph as
/// `tzolkinDate`.
//let getLastList referenceDate numDates tzolkinDate start =
// let rec getLastTzolkin = addDate (getLast referenceDate tzolkinDate) numDates
/// Helper function.
let inline private getList getFunc referenceDate numElem numDates tzolkinDate start =
let rec getNextTzolkin = addDate (getFunc referenceDate numElem tzolkinDate) numDates

getNextTzolkin 0 start []

/// Return a list of gregorian dates with the given Tzolk’in date.
let inline internal getNextList referenceDate numElem numDates tzolkinDate start =
getList getNext referenceDate numElem numDates tzolkinDate start


/// Return a list of gregorian dates with the given Tzolk’in date.
let inline internal getLastList referenceDate numElem numDates tzolkinDate start =
getList getLast referenceDate numElem numDates tzolkinDate start

// getLastTzolkin 0 start []
8 changes: 2 additions & 6 deletions src/TzolkinDate/TzolkinDate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,7 @@ module TzolkinDate =
/// A list with the next `numDates` Gregorian dates (forward in time after
/// the date `start`) that have the same Tzolk’in dates as `tzolkinDate`.
let getNextList numDates tzolkinDate start =
let rec getNextTzolkin = Generics.addDate (getNext tzolkinDate) numDates

getNextTzolkin 0 start []
Generics.getNextList referenceDate 260 numDates tzolkinDate start

/// Return the last Gregorian date before or the same as `start` with a Tzolk’in
/// date of `tzolkinDate`.
Expand Down Expand Up @@ -356,9 +354,7 @@ module TzolkinDate =
/// before the date `start`) that have the same Tzolk’in dates as
/// `tzolkinDate`.
let getLastList numDates tzolkinDate start =
let rec getLastTzolkin = Generics.addDate (getLast tzolkinDate) numDates

getLastTzolkin 0 start []
Generics.getLastList referenceDate 260 numDates tzolkinDate start

/// Filter the given list of Gregorian dates by the string `filterStr`.
/// If `filterStr` is contained in the locale short date ("dd.MM.yyyy" or
Expand Down
8 changes: 2 additions & 6 deletions src/TzolkinDate/TzolkinGlyph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,7 @@ module TzolkinGlyph =
/// A list with the next `numDates` Gregorian dates (forward in time after
/// the date `start`) that have the same Tzolk’in day glyph as `tzolkinDate`.
let getNextList numDates tzolkinDate start =
let rec getNextTzolkin = Generics.addDate (getNext tzolkinDate) numDates

getNextTzolkin 0 start []
Generics.getNextList referenceDate 20 numDates tzolkinDate start

/// Return the last Gregorian date before or the same as `start` with a Tzolk’in
/// day glyph of `tzolkinDate`.
Expand Down Expand Up @@ -442,9 +440,7 @@ module TzolkinGlyph =
/// before the date `start`) that have the same Tzolk’in day glyph as
/// `tzolkinDate`.
let getLastList numDates tzolkinDate start =
let rec getLastTzolkin = Generics.addDate (getLast tzolkinDate) numDates

getLastTzolkin 0 start []
Generics.getLastList referenceDate 20 numDates tzolkinDate start

/// Return the Tzolk’in day glyph's name as a string.
///
Expand Down
8 changes: 2 additions & 6 deletions src/TzolkinDate/TzolkinNumber.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ module TzolkinNumber =
/// A list with the next `numDates` Gregorian dates (forward in time after
/// the date `start`) that have the same Tzolk’in day numbers as `tzolkinDate`.
let getNextList numDates tzolkinDate start =
let rec getNextTzolkin = Generics.addDate (getNext tzolkinDate) numDates

getNextTzolkin 0 start []
Generics.getNextList referenceDate 13 numDates tzolkinDate start

/// Return the last Gregorian date before or the same as `start` with a Tzolk’in
/// day number of `tzolkinDate`.
Expand Down Expand Up @@ -202,9 +200,7 @@ module TzolkinNumber =
/// before the date `start`) that have the same Tzolk’in day number as
/// `tzolkinDate`.
let getLastList numDates tzolkinDate start =
let rec getLastTzolkin = Generics.addDate (getLast tzolkinDate) numDates

getLastTzolkin 0 start []
Generics.getLastList referenceDate 13 numDates tzolkinDate start

/// Return the Tzolk’in day number as a Unicode symbol.
/// This works as soon as the Tzolk’in day numbers are included in the Unicode
Expand Down

0 comments on commit 2ac5f52

Please sign in to comment.