Clojure替换ADT和模式匹配? [英] Clojure replacement for ADTs and Pattern Matching?

查看:179
本文介绍了Clojure替换ADT和模式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当在Haskell中我们需要一些变体数据类型,我们将使用ADT结合模式匹配。

Whenever in Haskell we need some variant data type, we would use ADTs in conjunction with pattern matching. What do Clojure folks use for such usecases?

推荐答案

实际上,有一些模式匹配库是为Clojure编写的。 Clojure的宏使这种事情成为可能。 Matchure 是最近的一个。甚至还有一些 ADTs in contrib

Well, there are actually some pattern matching libraries written for Clojure. Clojure's macros make this sort of thing possible. Matchure is one of the most recent. There are even some stuff for ADTs in contrib.

不考虑这些东西,我们在Clojure核心中最接近Haskell的ADT是Clojure 1.2中的新记录和数据类型。但是,除非您需要使用记录或数据类型所带来的好处,否则通常只需使用地图。 Clojure是一个动态语言,所以你不会得到静态类型检查如果你使用一个记录等等。

Disregarding that stuff, the closest thing we have to Haskell's ADTs in core Clojure is the new records and datatypes in Clojure 1.2. But, unless you need the benefits that come from using a record or datatype, you'll usually just use a map. Clojure is a dynamic language, so you wont get static type checking if you use a record and such anyway.

Clojure有destructuring这让人想起模式匹配,在习语Clojure中使用很多。请参见。前者是你的答案真的在这里。 ; p

Clojure has "destructuring" which is reminiscent of pattern matching, and is used a lot in idiomatic Clojure. See this and this. The former is an answer by yours truly here on SO. ;p

这篇关于Clojure替换ADT和模式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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