From b07f069489593abda0d0679bc586d8316842a25f Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 28 Dec 2018 23:13:56 +0800 Subject: [PATCH] Implement closest and closestIndex in a type-safer(ish...) way --- src/common.php | 21 ++++++++++++++------- src/purjolok.php | 14 ++++++-------- src/tasks.php | 4 ++-- src/unfinished.php | 4 ++-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/common.php b/src/common.php index 668a11b..1a29ef3 100644 --- a/src/common.php +++ b/src/common.php @@ -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 ); diff --git a/src/purjolok.php b/src/purjolok.php index 0326267..661e6cf 100644 --- a/src/purjolok.php +++ b/src/purjolok.php @@ -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' ); diff --git a/src/tasks.php b/src/tasks.php index f58a7d4..5e37f6f 100644 --- a/src/tasks.php +++ b/src/tasks.php @@ -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 ); diff --git a/src/unfinished.php b/src/unfinished.php index 55cb519..d8e58c9 100644 --- a/src/unfinished.php +++ b/src/unfinished.php @@ -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))] : [], -- 2.11.0