gpt4 book ai didi

functional-programming - 我应该在 clojure 中实现 Seq 接口(interface)吗

转载 作者:行者123 更新时间:2023-12-04 00:16:31 24 4
gpt4 key购买 nike

我在 Clojure 中有一个数据结构,它代表一组实验结果:

(defprotocol ResultSet
(columns [rs] "return a collection of the columns in the resultset, represented by keywords")
(rows [rs] [rs column-keys] "returns a seq of the rows in the resultset, order and column specified as keywords by column-keys. with a single argument returns rows with all columns present"))

我有一个 deftype它实现了这个协议(protocol)。我对编写函数感兴趣,这些函数可以在结果集中的所有结果上映射一个函数,或者折叠结果集,基本上与内置的 seq 函数做同样的事情。

在 Haskell 中,我会通过实现相关的类型类(例如 Functor)然后使用 fmap 或 mfilter 之类的函数来做到这一点。所以我研究了在 Clojure 中做这件事,并总结了一些关于实现 ISeq 接口(interface)的想法。

那么,这是个好主意吗?我找不到很多关于实现 ISeq 的资源,我想知道这方面的惯用方法是什么。

最佳答案

据我所知,实现这样的“最佳”方式不是实现 ISeq,而是实现 clojure.lang.Seqable;换句话说,提供 (.seq) 的实现以将您的 ResultSet 映射到(可能是惰性的)序列。这是 clojure 用于大多数(所有?)集合的路由,除了列表(列表实现 ISeq,因为 seq API 已经是列表 API 的子集)。

关于functional-programming - 我应该在 clojure 中实现 Seq 接口(interface)吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7313824/

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