Clojure:为什么扁平“使用错误的东西” [英] Clojure: Why is flatten "the wrong thing to use"

查看:127
本文介绍了Clojure:为什么扁平“使用错误的东西”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,这里:如何将地图转换为序列?



在一些tweet中,我不记得那些是否或多或少地说如果你使用flatten你可能是错了。



我想知道,flatten是什么问题?

解决方案

我认为这是他们在回答中说的linked:

  so> (b)(b)(b)(b)(b)(b)(b)(b)(b)(b)(b) (apply concat {:a [1 2]:b [3 4]})
(:b [3 4]:a [1 2])

Flatten将从键和值中删除结构,这可能不是您想要的。有一些用例你想删除嵌套序列的结构,而flatten是为这种情况写的。但是对于解构地图,你通常想要保持内部序列。


I've read this kind of thing a couple of times since I've started Clojure.

For instance, here: How to convert map to a sequence?

And in some tweet I don't remember exactly that was more or less saying "if you're using flatten you're probably doing it wrong".

I would like to know, what is wrong with flatten?

解决方案

I think this is what they were talking about in the answer you linked:

so> ((comp flatten seq) {:a [1 2] :b [3 4]})
(:b 3 4 :a 1 2)
so> (apply concat {:a [1 2] :b [3 4]})
(:b [3 4] :a [1 2])

Flatten will remove the structure from the keys and values, which is probably not what you want. There are use cases where you do want to remove the structure of nested sequences, and flatten was written for such cases. But for destructuring a map, you usually do want to keep the internal sequences as is.

这篇关于Clojure:为什么扁平“使用错误的东西”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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