"""Offline Scala semantic verification lab for Chapter 17 (collections)."""
import json
VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"
def case_immutable_vs_mutable_semantics():
# Immutable collection returns a new collection while leaving original intact
imm_list = (1, 2, 3, 4)
imm_next = imm_list + (5,)
mut_buf = [1, 2, 3, 4]
mut_buf.append(5)
expected = {
"immutable_origin": (1, 2, 3, 4),
"immutable_derived": (1, 2, 3, 4, 5),
"mutable_in_place": [1, 2, 3, 4, 5],
}
actual = {
"immutable_origin": imm_list,
"immutable_derived": imm_next,
"mutable_in_place": mut_buf,
}
return {
"name": "immutable_vs_mutable_semantics",
"summary": "对比不可变集合返回新实例且原集合不变与可变集合就地更新的核心差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_list_set_and_map_operations():
lst = (1, 2, 2, 3, 4)
st = frozenset((1, 3, 5, 7, 3))
mp = {"one": 1, "two": 2, "three": 3}
expected = {
"list_allows_duplicates": True,
"list_head_tail": (1, (2, 2, 3, 4)),
"set_dedup_size": 4,
"map_lookup_two": 2,
"map_missing_get": None,
}
actual = {
"list_allows_duplicates": len(lst) == 5,
"list_head_tail": (lst[0], lst[1:]),
"set_dedup_size": len(st),
"map_lookup_two": mp["two"],
"map_missing_get": mp.get("four"),
}
return {
"name": "list_set_and_map_operations",
"summary": "验证 List 线性可重复、Set 无序去重与 Map 键值映射三类核心集合语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_tuple_and_option_containers():
tup = (10, "BlueSky")
some_val = ("Some", 5)
none_val = ("None", None)
def get_or_else(opt, default):
return opt[1] if opt[0] == "Some" else default
expected = {
"tuple_1": 10,
"tuple_2": "BlueSky",
"some_unwrap": 5,
"none_fallback": 0,
}
actual = {
"tuple_1": tup[0],
"tuple_2": tup[1],
"some_unwrap": get_or_else(some_val, 0),
"none_fallback": get_or_else(none_val, 0),
}
return {
"name": "tuple_and_option_containers",
"summary": "验证异构不可变元组 Tuple 与空值安全容器 Option(Some/None) 的解包与兜底逻辑",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_iterator_lazy_traversal_intro():
it = iter([10, 20, 30])
first_pass = list(it)
second_pass = list(it)
expected = {"first_pass": [10, 20, 30], "second_pass_exhausted": []}
actual = {"first_pass": first_pass, "second_pass_exhausted": second_pass}
return {
"name": "iterator_lazy_traversal_intro",
"summary": "验证 Iterator 作为一次性访问视图而非持久化容器的单次消费特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_immutable_vs_mutable_semantics(),
case_list_set_and_map_operations(),
case_tuple_and_option_containers(),
case_iterator_lazy_traversal_intro(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。
入口文件:projects/scala-collections-lab/explore-collections.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-collections.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2413.zip