简体   繁体   English

Common Lisp 等同于 Python 的 itertools.starmap?

[英]Common Lisp equivalent of Python's itertools.starmap?

Python's Itertools has what is called starmap . Python 的 Itertools 有所谓的starmap Given a collection of collections and a function, it applies the function to each collection strictly inside the collection, using the elements of said internal collection as arguments to the function. For example,给定一个集合 collections 和一个 function,它将 function 严格应用于集合内部的每个集合,使用所述内部集合的元素作为 arguments 到 function。例如,

from itertools import starmap
 
NestedList = [(1, 2), (3, 4), (5, 6), (0, 0), (1, 1), (2, 2)]

list(starmap(lambda x, y:x + y, NestedList))

returns the list containing 3, 7, 11, 0, 2, and 4.返回包含 3、7、11、0、2 和 4 的列表。

I refuse to believe that Python was the first to come up with this concept, but I'm drawing a blank when I try to think of what it was called in older languages.我不相信 Python 是第一个提出这个概念的人,但当我试着想一想它在旧语言中的称呼时,我一片空白。 Does any analogous functionality exist in common lisp?普通的 lisp 中是否存在任何类似的功能? I feel certain that it does, but I cannot name it.我确信它确实如此,但我不能说出它的名字。

Use a combination of mapcar and apply :结合使用mapcarapply

(defun starmap (f list)
  (mapcar (lambda (x) (apply f x)) list))

Or loop with the keyword collect :或者使用关键字collect loop

(defun starmap (f list)
  (loop for x in list 
        collect (apply f x)))

Examples:例子:

> (starmap (lambda (x y) (+ x y)) '((1 2) (3 4) (5 6) (0 0) (1 1) (2 2)))
(3 7 11 0 2 4)

> (starmap #'expt '((1 2) (3 4) (5 6) (0 0) (1 1) (2 2)))
(1 81 15625 1 1 4)

I refuse to believe that Python was the first to come up with this concept, but I'm drawing a blank when I try to think of what it was called in older languages.我不相信 Python 是第一个提出这个概念的人,但当我试着想一想它在旧语言中的称呼时,我一片空白。 [...] I feel certain that it does, but I cannot name it. [...] 我确信它确实如此,但我不能说出它的名字。

To address this particular point, consider that Lispers or more generally functional programmers would say: " map the apply function ".为了解决这一点,请考虑 Lispers 或更一般的函数式程序员会说:“ map apply function ”。 Consequently, a natural name would be mapply .因此,自然名称将是mapply

By looking for that name, you can see that there are indeed libraries like Serapeum that define it, but also that there are older references to this function (with the same meaning) from 1992 (H. Baker: Look'Lively Linear Lisp — 'Look Ma, No Garbage!' ) and probably before that too.通过查找该名称,您可以看到确实有像Serapeum这样的库定义了它,而且还有 1992 年对这个 function(具有相同含义)的旧引用(H. Baker: Look'Lively Linear Lisp - '看,妈妈,没有垃圾!' ),而且可能在此之前。

The fact that Python uses * to expand a list of values in the same way as apply explains why it is named starmap . Python 使用*以与apply相同的方式扩展值列表这一事实解释了为什么将其命名为starmap

In common lisp that can be achieved using a combination of mapcar and apply .在常见的 lisp 中,可以使用mapcarapply的组合来实现。

(mapcar (lambda (lst) (apply (lambda (a b) (+ a b)) lst))
    '((1 2) (3 4)))

Similarly we can also implement it in javascript:同样我们也可以在javascript中实现:

[[1, 2], [3, 4]].map(([x, y]) => x + y);

So starmap is nothing really new, just a convenient shortcut.所以starmap并不是什么新鲜事,只是一个方便的快捷方式。

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

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