Clojure:为什么扁平化“用错了东西"? [英] Clojure: Why is flatten "the wrong thing to use"

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

问题描述

自从我开始使用 Clojure 以来,我已经读过几次这种东西.

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

例如,这里:如何将映射转换为序列?

在一些推文中,我不记得确切地说是如果你使用 flatten 你可能做错了".

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".

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

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 将从键和值中移除结构,这可能不是您想要的.在某些用例中,您确实希望删除嵌套序列的结构,并且为这种情况编写了 flatten.但是对于解构映射,您通常确实希望保持内部序列不变.

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天全站免登陆