From 24a2c9c2b416af97261dd723b8085ad86456a24a Mon Sep 17 00:00:00 2001 From: Da Shen Date: Wed, 22 Oct 2025 10:26:18 +0800 Subject: [PATCH] =?UTF-8?q?[201=5F13]=20=E4=B8=BA=20rich-list%find=20?= =?UTF-8?q?=E5=92=8C=20rich-list%find-last=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- devel/201_13.md | 67 ++++++++++++- tests/goldfish/liii/rich-list-test.scm | 124 +++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 4 deletions(-) diff --git a/devel/201_13.md b/devel/201_13.md index b5e447f8..213246c6 100644 --- a/devel/201_13.md +++ b/devel/201_13.md @@ -1,8 +1,67 @@ # [201_13] rich-list相关的文档和测试 +## 所有的任务 +### 构造函数 ++ [x] rich-list@range ++ [x] rich-list@empty ++ [x] rich-list@concat ++ [x] rich-list@fill + +### 实例方法 ++ [x] rich-list%collect ++ [x] rich-list%empty? ++ [x] rich-list%length ++ [x] rich-list%map ++ [x] rich-list%filter ++ [x] rich-list%reverse ++ [x] rich-list%take ++ [x] rich-list%drop + +### 需要添加测试的实例方法 ++ [ ] rich-list%apply ++ [x] rich-list%find ++ [x] rich-list%find-last ++ [ ] rich-list%head ++ [ ] rich-list%head-option ++ [ ] rich-list%last ++ [ ] rich-list%last-option ++ [ ] rich-list%slice ++ [ ] rich-list%equals ++ [ ] rich-list%forall ++ [ ] rich-list%exists ++ [ ] rich-list%contains ++ [ ] rich-list%flat-map ++ [ ] rich-list%for-each ++ [ ] rich-list%take-right ++ [ ] rich-list%drop-right ++ [ ] rich-list%count ++ [ ] rich-list%fold ++ [ ] rich-list%fold-right ++ [ ] rich-list%sort-with ++ [ ] rich-list%sort-by ++ [ ] rich-list%group-by ++ [ ] rich-list%sliding ++ [ ] rich-list%zip ++ [ ] rich-list%zip-with-index ++ [ ] rich-list%distinct ++ [ ] rich-list%reduce ++ [ ] rich-list%reduce-option ++ [ ] rich-list%take-while ++ [ ] rich-list%drop-while ++ [ ] rich-list%index-where ++ [ ] rich-list%max-by ++ [ ] rich-list%min-by ++ [ ] rich-list%append ++ [ ] rich-list%max-by-option ++ [ ] rich-list%min-by-option ++ [ ] rich-list%to-string ++ [ ] rich-list%make-string ++ [ ] rich-list%to-vector ++ [ ] rich-list%to-rich-vector + ## 任务相关的代码文件 参考代码(一般情况下不修改,除非有缺陷): -- goldfish/list/rich-list.scm +- goldfish/liii/rich-list.scm - tests/goldfish/liii/lang-test.scm 测试用例: @@ -12,9 +71,9 @@ ``` xmake config --yes xmake b goldfish -bin/goldfish tools/lint.scm tests/goldfish/list/rich-list.scm -bin/goldfish tools/lint.scm tests/goldfish/list/rich-list-test.scm -bin/goldfish -m r7rs tests/goldfish/list/rich-list-test.scm +bin/lint goldfish/liii/rich-list.scm +bin/lint tests/goldfish/liii/rich-list-test.scm +bin/goldfish -m r7rs tests/goldfish/liii/rich-list-test.scm ``` ## 2025/10/21 文档从实现处迁移到测试用例处 diff --git a/tests/goldfish/liii/rich-list-test.scm b/tests/goldfish/liii/rich-list-test.scm index 60635efb..3eea1995 100644 --- a/tests/goldfish/liii/rich-list-test.scm +++ b/tests/goldfish/liii/rich-list-test.scm @@ -355,4 +355,128 @@ rich-list%collect (check (cadr result) => 2) (check (cddr result) => '(3))) + +#| +rich-list%find +在rich-list中查找第一个满足条件的元素。 + +语法 +---- +(lst :find pred) + +参数 +---- +pred : procedure +用于测试元素的谓词函数,接受一个参数并返回布尔值。 + +返回值 +----- +以option形式返回找到的第一个满足条件的元素。 +- 如果找到匹配元素:返回包含该元素的option对象 +- 如果没有找到匹配元素:返回none + +功能 +---- +从列表的开头开始遍历,返回第一个满足谓词条件的元素。 +使用option类型包装结果,避免空值异常。 + +边界条件 +-------- +- 空列表:返回none +- 没有满足条件的元素:返回none +- 多个满足条件的元素:返回第一个匹配的元素 + +性能特征 +-------- +- 时间复杂度:O(n),最坏情况下需要遍历整个列表 +- 空间复杂度:O(1),仅返回option对象引用 + +兼容性 +------ +- 与option类型系统兼容 +- 支持链式调用模式 +|# + +;; 基本测试 - 找到元素 +(check (($ '(1 2 3 4 5) :find even?) :get) => 2) +(check (($ '(1 3 5 7 9) :find (lambda (x) (> x 5))) :get) => 7) +(check (($ '(a b c d) :find (lambda (x) (eq? x 'c))) :get) => 'c) + +;; 边界测试 - 空列表 +(check (($ '() :find (lambda (x) #t)) :defined?) => #f) + +;; 边界测试 - 没有匹配元素 +(check (($ '(1 3 5 7) :find even?) :defined?) => #f) +(check (($ '(a b c) :find (lambda (x) (eq? x 'z))) :defined?) => #f) + +;; 边界测试 - 多个匹配元素,返回第一个 +(check (($ '(1 2 4 6 8) :find even?) :get) => 2) +(check (($ '(5 10 15 20) :find (lambda (x) (= (modulo x 5) 0))) :get) => 5) + +;; 链式调用测试 +(check (($ '(1 2 3 4 5) :filter (lambda (x) (> x 2)) :find even?) :get) => 4) + + +#| +rich-list%find-last +在rich-list中从后往前查找第一个满足条件的元素。 + +语法 +---- +(lst :find-last pred) + +参数 +---- +pred : procedure +用于测试元素的谓词函数,接受一个参数并返回布尔值。 + +返回值 +----- +以option形式返回从后往前找到的第一个满足条件的元素。 +- 如果找到匹配元素:返回包含该元素的option对象 +- 如果没有找到匹配元素:返回none + +功能 +---- +从列表的末尾开始向前遍历,返回第一个满足谓词条件的元素。 +与find方法相反,find-last返回最后一个匹配的元素。 +使用option类型包装结果,避免空值异常。 + +边界条件 +-------- +- 空列表:返回none +- 没有满足条件的元素:返回none +- 多个满足条件的元素:返回最后一个匹配的元素 + +性能特征 +-------- +- 时间复杂度:O(n),最坏情况下需要遍历整个列表 +- 空间复杂度:O(n),需要反转列表(临时空间) + +兼容性 +------ +- 与option类型系统兼容 +- 支持链式调用模式 +|# + +;; 基本测试 - 找到最后一个匹配元素 +(check (($ '(1 2 3 4 5) :find-last even?) :get) => 4) +(check (($ '(1 3 5 7 9) :find-last (lambda (x) (> x 5))) :get) => 9) +(check (($ '(a b c d c) :find-last (lambda (x) (eq? x 'c))) :get) => 'c) + +;; 边界测试 - 空列表 +(check (($ '() :find-last (lambda (x) #t)) :defined?) => #f) + +;; 边界测试 - 没有匹配元素 +(check (($ '(1 3 5 7) :find-last even?) :defined?) => #f) +(check (($ '(a b c) :find-last (lambda (x) (eq? x 'z))) :defined?) => #f) + +;; 边界测试 - 多个匹配元素,返回最后一个 +(check (($ '(1 2 4 6 8) :find-last even?) :get) => 8) +(check (($ '(5 10 15 20) :find-last (lambda (x) (= (modulo x 5) 0))) :get) => 20) + +;; 链式调用测试 +(check (($ '(1 2 3 4 5) :filter (lambda (x) (> x 1)) :find-last even?) :get) => 4) + + (check-report) -- Gitee