From 56350b181f13bad145ec893d3c3bec91ceb2007a Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Wed, 8 Jan 2025 20:36:54 +0800 Subject: [PATCH] opt: findClosestNumber Signed-off-by: bggRGjQaUbCoE --- lib/utils/utils.dart | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index bb08de30e..8c774a41e 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -794,22 +794,9 @@ class Utils { } static int findClosestNumber(int target, List numbers) { - return switch (numbers.length) { - 0 => target, - 1 => numbers.first, - _ => numbers.reduce((a, b) { - if (b > target) { - return a; - } - int diff1 = (a - target).abs(); - int diff2 = (b - target).abs(); - return diff1 == diff2 - ? max(a, b) - : diff1 < diff2 - ? a - : b; - }), - }; + return numbers + .where((number) => number <= target) + .reduce((a, b) => a > b ? a : b); } // 检查更新