简体   繁体   English

IEnumerable<t> 在 OCaml 中</t>

[英]IEnumerable<T> in OCaml

I use F# a lot.我经常使用 F#。 All the basic collections in F# implement IEumberable interface, thus it is quite natural to access them using the single Seq module in F#. F# 中的所有基本 collections 都实现了 IEumberable 接口,因此使用 F# 中的单个Seq模块访问它们是很自然的。 Is this possible in OCaml?这在 OCaml 中可能吗?

The other question is that 'a seq in F# is lazy, eg I can create a sequence from 1 to 100 using {1..100} or more verbosely:另一个问题是'a seq中的序列是惰性的,例如,我可以使用{1..100}或更详细地创建从1100的序列:

seq { for i=1 to 100 do yield i }

In OCaml, I find myself using the following two methods to work around with this feature:在 OCaml 中,我发现自己使用以下两种方法来解决此功能:

  1. generate a list:生成一个列表:

     let rec range ab = if a > b then [] else a:: range (a+1) b;;
  2. or resort to explicit recursive functions.或诉诸显式递归函数。

The first generates extra lists.第一个生成额外的列表。 The second breaks the abstraction as I need to operate on the sequence level using higher order functions such as map and fold .第二个打破了抽象,因为我需要使用mapfold等高阶函数在序列级别上进行操作。

I know that the OCaml library has Stream module.我知道 OCaml 库有Stream模块。 But the functionality of it seems to be quite limited, not as general as 'a seq in F#.但它的功能似乎相当有限,不如 F# 中'a seq通用。

BTW, I am playing Project Euler problems using OCaml recently.顺便说一句,我最近正在使用 OCaml 玩 Project Euler 问题。 So there are quite a few sequences operations, that in an imperative language would be loops with a complex body.所以有相当多的序列操作,在命令式语言中将是具有复杂主体的循环。

This Ocaml library seems to offer what you are asking.这个 Ocaml 库似乎可以提供您所要求的。 I've not used it though.虽然我没用过。

http://batteries.forge.ocamlcore.org/ http://batteries.forge.ocamlcore.org/

Checkout this module, Enum http://batteries.forge.ocamlcore.org/doc.preview:batteries-beta1/html/api/Enum.html查看此模块,枚举http://batteries.forge.ocamlcore.org/doc.preview:batteries-beta1/html/api/Enum.html

I somehow feel Enum is a much better name than Seq.我不知何故觉得 Enum 是一个比 Seq 更好的名字。 It eliminates the lowercase/uppercase confusion on Seqs.它消除了 Seqs 上的小写/大写混淆。

An enumerator, seen from a functional programming angle, is exactly a fold function.从函数式编程的角度来看,枚举数恰好是fold function。 Where a class would implement an Enumerable interface in an object-oriented data structures library, a type comes with a fold function in a functional data structure library. class 将在面向对象的数据结构库中实现Enumerable接口,而类型在功能数据结构库中带有fold function。

Stream is a slightly quirky imperative lazy list (imperative in that reading an element is destructive). Stream是一个有点古怪的命令式惰性列表(命令式读取元素是破坏性的)。 CamlP5 comes with a functional lazy list library, Fstream . CamlP5 带有一个功能性惰性列表库Fstream This already cited thread offers some alternatives .这个已经引用的线程提供了一些替代方案

It seems you are looking for something like Lazy lists.看来您正在寻找诸如惰性列表之类的东西。 Check out this SO question看看这个SO 问题

The Batteries library mentioned also provides the (--) operator:提到的电池库还提供了 (--) 运算符:

# 1 -- 100;;
- : int BatEnum.t = <abstr>

The enum is not evaluated until you traverse the items, so it provides a feature similar to your second request.在您遍历项目之前不会评估枚举,因此它提供了类似于您的第二个请求的功能。 Just beware that Batteries' enum s are mutable.请注意 Batteries 的enum是可变的。 Ideally, there would also be a lazy list implementation that all data structures could be converted to/from, but that work has not been done yet.理想情况下,还有一个惰性列表实现,所有数据结构都可以转换为/从,但这项工作尚未完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM