gpt4 book ai didi

clojure - 关于序列转换的惯用 Clojure 问题

转载 作者:行者123 更新时间:2023-12-02 23:15:14 27 4
gpt4 key购买 nike

我正在学习 Clojure,我需要在正确的方向上插入我提出的这个问题。

我有一系列事件。每个事件都包含一个“日期”。

(def events
[
[1509 :marry "Catherine of Aragon"]
[1527 :unmarry "Catherine of Aragon"]
[1533 :marry "Anne Boleyn"]
[1536 :unmarry "Anne Boleyn"]
[1536 :marry "Jane Seymour"]
[1537 :unmarry "Jane Seymour"]
[1540 :marry "Anne of Cleves"]
[1540 :unmarry "Anne of Cleves"]
[1540 :marry "Catherine Howard"]
[1542 :unmarry "Catherine Howard"]
[1543 :marry "Catherine Parr"]])

我想将其转换为惰性时间线,即每年包含一个向量的序列。从第一个事件发生的年份开始,一直到无穷大。

[[[:marry "Catherine of Aragon"]] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [[:unmarry "Catherine of Aragon"]] [] [] [] [] [] [[:marry "Ane Boleyn"]] [] [] [[:unmarry "Anne Boleyn"] [:marry "Jayne Seymour"]] ...]

最佳答案

(def timeline
(let [events-by-year (group-by first events)]
(map #(map next (events-by-year %))
(iterate inc (reduce min (keys events-by-year))))))

快速测试:

=> (take 30 timeline)
(((:marry "Catherine of Aragon")) () () () () () () () () () () () () () () () ()
() ((:unmarry "Catherine of Aragon")) () () () () () ((:marry "Anne Boleyn")) ()
() ((:unmarry "Anne Boleyn") (:marry "Jane Seymour")) ((:unmarry "Jane Seymour"))
())

关于clojure - 关于序列转换的惯用 Clojure 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4850379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com