以下函数编程模式的正确术语是什么? [英] What is the correct term for the following functional programming pattern?

查看:119
本文介绍了以下函数编程模式的正确术语是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过它被称为 stream 作为 无限列表< a>,,有时甚至作为 延迟序列



以下模式的正确术语是什么? (显示Clojure代码)

 (def first $ first)

(defn second $ [str]
(cond
(empty?str)()
true((first(rest str)))))

(defn stream-builder [next_ n]
(cons n(cons(fn [](stream-builder next_(next_ n)))()))

(defn stream [str n]

(= 0 n)()
true(cons(first $ str)(stream(second $ str)( - n 1))))


(stream-builder(fn [n]
(+ 2 n))1))

(println(stream odd 23))

> (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45)


解决方案

简短的回答: stream-builder 返回一个返回无限序列/列表的函数, '(因为你不能在有限时间内评价无限长的东西)。在Clojure世界中,你可能不应该调用你的示例streams中的任何东西,以避免与另一个概念混淆。



更长的答案:



编程语言中的多样性的一个不幸的副作用是,相同的词为不同的意思。你提到的所有三个字(Stream,无限列表,延迟序列)以串行方式处理元素,在Clojure中我们称之为序列,但是每个元素的细微差别略有不同。 / p>

流通常指元素的某些序列,并且现在经常在有限字符序列的上下文中使用。通常这些字符序列来自文件,网络源,或Unix管道。



如果一个序列定义的方式有一个无限数量的元素,我们可以称之为一个无限序列。在内部作为链接列表,因此我们可以将其称为无限列表。虽然,诚实地,我宁愿在Clojure社区听到无限序列这个术语,所以我们不束缚于一个特定的实现。



最后,lazy sequence在Clojure中是指对按需发生的数据结构进行顺序评估的模式。换句话说,这里的重点在于评价的懒惰性质;在您请求序列之前,实际上不计算序列中的特定元素的值。



总而言之,在Clojure中应使用以下字词:




  • 引用具有链接列表实现的内容

  • lazy指代需要评估的事物

  • infinite

  • stream指来自外部源的管道状(字符)序列


I've heard it referred to as a stream, as an infinite list, and sometimes even as a lazy sequence.

What is the correct term for the following pattern? (Clojure code shown)

(def first$ first)

(defn second$ [str]
  (cond
    (empty? str) ()
    true ((first (rest str)))))

(defn stream-builder [next_ n]
  (cons n (cons (fn [] (stream-builder next_ (next_ n))) ())))

(defn stream [str n]
  (cond
    (= 0 n) ()
    true (cons (first$ str) (stream (second$ str) (- n 1)))))

(def odd 
  (stream-builder (fn [n] 
        (+ 2 n))1))

(println (stream odd 23))

> (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45)

解决方案

Short answer: stream-builder returns a function that returns an infinite sequence/list, which must be evaluated 'lazily' (since you can't evaluate something infinitely long in finite time). In the Clojure world, you should probably call none of the things in your example "streams" to avoid confusion with another concept.

Longer answer:

An unfortunate side effect of the diversity of thought in programming languages is that we often use the same words for different meanings. All three words you mentioned ("Stream", infinite list", "lazy sequence") refer to processing elements in a serial fashion, and in Clojure we call these "Sequences". However, the nuances implied by each are slightly different.

A "stream" refers generally to some sequence of elements, and is nowadays often used in the context of finite character sequences. Often these character sequences come from a file, network source, or Unix pipe.

If a sequence is defined in a way that it has an infinite number of elements, we can call it an infinite sequence. Usually infinite sequences are represented internally as a linked list , so we may call these "infinite lists". Although, to be honest I would prefer to hear the term "infinite sequence" in the Clojure community so we are not tied to a particular implementation.

Finally, the nuance of "lazy sequence" in Clojure refers to a pattern of sequential evaluation on a data structure that occurs "on demand". In other words, the emphasis here is on the lazy nature of evaluation; the value of a particular element in the sequence is not actually computed until you ask for it.

In summary, in Clojure you should use the words:

  • "list" to refer to something with a linked list implementation
  • "lazy" to refer to things that evaluate on demand
  • "infinite" to refer to sequences that aren't of finite size (and must therefore be lazy)
  • "stream" to refer to a pipe-like (character) sequence from an external source

这篇关于以下函数编程模式的正确术语是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆