Implement closest and closestIndex in a type-safer(ish...) way
authorCameron Ball <cameron@cameron1729.xyz>
Fri, 28 Dec 2018 15:13:56 +0000 (23:13 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Fri, 4 Jan 2019 20:47:20 +0000 (04:47 +0800)
src/common.php
src/purjolok.php
src/tasks.php
src/unfinished.php

index 668a11b..1a29ef3 100644 (file)
@@ -326,13 +326,19 @@ function getFilePathsForYear(int $year) {
     })(['summer', 'winter', 'spring', 'autumn']));
 }
 
-function closest($n, $list) {
-    $a = array_filter($list, function($value) use ($n) {
-        return $value <= $n;
-    });
+function closestIndex($n, array $list) {
+    //return $list[closestIndex($n, $list)];
+    $a = map(function(int $v) use ($n) : int {
+            return (int)abs($v - $n);
+        })
+        ($list);
 
-    arsort($a);
-    return array_values($a)[0];
+    asort($a);
+    return array_keys($a)[0];
+}
+
+function closest($n, array $list) : int {
+    return $list[closestIndex($n, $list)];
 }
 
 function reveal($str) {
@@ -347,7 +353,8 @@ function reveal($str) {
     $penultimateBytes = array_filter(
         (unpack('C*', $str)),
         function($key) {
-            return ($key + 1) % 4 == 0;
+            return ($key + 1) % 4 ==
+                                  0;
         },
         ARRAY_FILTER_USE_KEY
     );
index 0326267..661e6cf 100644 (file)
@@ -23,10 +23,9 @@ if ($between = between(reveal($message ?? ''), '[taskid]')) {
         (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
         (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
     ];
-    $closestMonday = closest($dt->format('d'), $mondays);
-    $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
+    $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
+    $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
     $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
-    $currentWeekOfMonth = array_search($closestMonday, $mondays);
     $tasksForTheWeek = getTasksForTheWeek(
         $currentWeekOfMonth,
         (int)$dt->format('m'),
@@ -247,8 +246,7 @@ getTelegram()->addCommand(
                 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
             ];
             $currentMonth = (int)(new DateTimeImmutable())->format('m');
-            $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
-            $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
+            $currentWeekOfMonth = closestIndex((new DateTimeImmutable())->format('d'), $mondays);
             $currentYear = (int)(new DateTimeImmutable())->format('Y');
             $tasksForTheWeek = getTasksForTheWeek(
                 $currentWeekOfMonth,
@@ -289,12 +287,12 @@ getTelegram()->addCommand(
                 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
                 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
             ];
-            $closestMonday = closest($dt->format('d'), $mondays);
+            $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
 
-            $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
+            $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
 
             $tasksForTheWeek = getTasksForTheWeek(
-                array_search($closestMonday, $mondays),
+                $currentWeekOfMonth,
                 (int)$dt->format('m'),
                 require 'taskMatrix.php'
             );
index f58a7d4..5e37f6f 100644 (file)
@@ -11,7 +11,7 @@ $mondays = [
 ];
 $currentMonth = (int)(new DateTimeImmutable())->format('m');
 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
-$currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
+$currentWeekOfMonth = closestIndex($currentDayOfMonth, $mondays);
 
 $taskLists = array_merge(
     isStartOfSeason($currentMonth, $currentDayOfMonth) ? [unlines(map(getStringAndCode)(getTasksForTheSeason(getSeason($currentMonth), $taskMatrix)))] : [],
@@ -43,7 +43,7 @@ $messages = zipWith(
     },
     // Magic. startOfSeason implies startofMonth so we get 2, start of month without start of season gives 1 and
     // a regular week (not the start of a season or month) gives 0. And this is how the indicies are ordered in the array.
-    $taskMessages[isStartOfSeason($currentMonth, $currentDayOfMonth) + isStartOfMonth($currentDayOfMonth)],
+    $taskMessages[(int)isStartOfSeason($currentMonth, $currentDayOfMonth) + (int)isStartOfMonth($currentDayOfMonth)],
     $taskLists
 );
 
index 55cb519..d8e58c9 100644 (file)
@@ -15,7 +15,7 @@ $currentMonth = (int)(new DateTimeImmutable())->format('n');
 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
 $currentSeason = getSeason($currentMonth);
 $currentYear = (int)(new DateTimeImmutable())->format('Y');
-$currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
+$currentWeekOfMonth = closestIndex($currentDayOfMonth, $mondays);
 
 $extractTasks = function($tasks, $path) {
     return array_merge($tasks, file_exists($path) ? lines(trim(file_get_contents($path))) : []);
@@ -46,7 +46,7 @@ $unfinishedForWeek = array_diff(
     file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : []
 );
 
-//EOY => (EOM & EOW) & !EOS
+//EOY => (EOM & EOW) & !EOSx
 //EOS => (EOM & EOW) & !EOY
 $taskLists = array_merge(
     isEndOfYear($currentYear, $currentMonth, $currentDayOfMonth) ? [unlines(map(getStringAndCode)($unfinishedForYear))] : [],