开发者

Is there a common name for a function that takes a list of lists and returns a single list containing the contents of those lists?

EDIT: My question was originally "Is there a standard name for a function that flattens a list of lists, but only one level deep?", but Chuck's answer is phrased much closer to what I actually wanted to ask, so I renamed it. All t开发者_运维百科hree answers were useful to me, though. Thanks.

'flatten' seems to be a well-accepted name for a function that takes a tree and builds a list of atoms however deep they are nested, but what about a function that stops after just one level? So ((1 2) ((3 4) (5 6)) (7 8)) "somethings" to (1 2 (3 4) (5 6) 7 8). Does "something" have a common name across multiple languages/libraries?

The answers to this question:

Flattening a shallow list in Python

suggest that 'chain' might be a good guess, but is it common enough to be "standard"?


For removing an inner set of brackets of a list of lists, concat is very popular. A more general function, for flattening an M of Ms for a monad M, is often called join. In abstract algebra, this function is standardly called µ.


The function that takes a list of lists and returns a single list containing the contents of those lists is called "concat" in many functional languages (e.g. OCaml, F#, Haskell, Clojure).


I'm not sure there is a standard name for this. I can name 3 different implementations with 3 different names

  • Python: chain
  • F#: concat
  • LINQ: SelectMany


In Common Lisp, you can apply APPEND, or CONCATENATE with the right type parameter. The result of APPEND shares substructure with the argument lists; CONCATENATE always copies, and can also be applied to non-list sequences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜