Clojure如何处理关注点分离? [英] How does Clojure approach Separation of Concerns?

查看:62
本文介绍了Clojure如何处理关注点分离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clojure如何处理关注点分离?由于代码是数据,因此函数可以作为参数传递并用作返回值...

How does Clojure approach Separation of Concerns ? Since code is data, functions can be passed as parameters and used as returns...

而且,由于存在这样的原理,对1000个数据结构使用1000个更好的函数 ,而不是100个数据结构上的100个函数(或类似的东西)。

And, since there is that principle "Better 1000 functions that work on 1 data structure, than 100 functions on 100 data structures" (or something like that).

我的意思是,将所有内容打包成一个地图,给它一个关键字作为键,就是这样吗?函数,标量,集合,一切...

I mean, pack everything a map, give it a keyword as key, and that's it ? functions, scalars, collections, everything...

关注点分离的思想是通过Java中的Aspects(面向方面​​的编程)和注释来实现的。这是我对这个概念的看法,可能有些局限,所以不要认为这是理所当然的。

The idea of Separation of Concerns is implemented, in Java, by means of Aspects (aspect oriented programming) and annotations. This is my view of the concept and might be somewhat limited, so don't take it for granted.

什么是正确的方法(惯用的方法) Clojure,为了避免其他程序员的WTF _

What is the right way (idiomatic way) to go about in Clojure, to avoid the WTFs of fellow programmers _

推荐答案

在功能语言中,处理关注点分离的最佳方法是:将任何编程问题转换为数据结构上的一组转换。例如,如果您编写一个Web应用程序,则总体目标是接受一个请求并将其转换为响应,可以将其视为将请求数据转换为响应数据。 (在非平凡的Web应用程序中,起始数据可能不仅包括请求,还包括会话和数据库信息。)大多数编程任务都可以通过这种方式来考虑。

In a functional language, the best way to handle separation of concerns is to convert any programming problem into a set of transformations on a data structure. For instance, if you write a web app, the overall goal is to take a request and transform it into a response, which can be thought of as simply transforming the request data into response data. (In a non-trivial web app, the starting data would probably include not only the request, but also session and database information) Most programming tasks can be thought of in this way.

每个关注点都是管道中的一个函数,有助于实现转换。这样,每个功能就完全与其他步骤分离了。

Each "concern" would be a function in a "pipeline" that helps make the transform possible. In this way, each function is completely decoupled from the other steps.

请注意,这意味着您的数据在经历了这些转换后,必须具有丰富的结构。本质上,我们希望将程序的所有智能都放入数据中,而不是代码中。在复杂的功能程序中,不同级别的数据可能足够复杂,以至于需要看起来像本身就是一种编程语言-这就是特定领域语言的思想在这里发挥作用的。

Note that this means that your data, as it undergoes these transformations, needs to be rich in its structure. Essentially, we want to put all the "intelligence" of our program into the data, not in the code. In a complicated functional program, the data at the different levels may be complex enough that in needs to look like a programming language in its own right- This is where the idea of "domain-specific languages" comes into play.

Clojure对操纵复杂的异构数据结构提供了出色的支持,这使其听起来不那么麻烦(即,如果做对的话一点也不麻烦)

Clojure has excellent support for manipulating complex heterogenous data structures, which makes this less cumbersome than it may sound (i.e. it's not cumbersome at all if done right)

此外,Clojure对惰性数据结构的支持使这些中间数据结构实际上(在概念上)具有无限的大小,这使得在大多数情况下可以实现这种解耦。有关为何在这种情况下具有无限数据结构如此重要的信息,请参见以下论文: http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

In addition, Clojure's support for lazy data structures allows these intermediate data structures to actually be (conceptually) infinite in size, which makes this decoupling possible in most scenarios. See the following paper for info on why having infinite data structures is so valuable in this situation: http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

这种管道方法可以满足您90%的分离关注需求。对于剩下的10%,您可以使用Clojure宏,在较高的层次上,可以将其视为面向方面编程的非常强大的工具。

This "pipeline" approach can handle 90% of your needs for separating concerns. For the remaining 10% you can use Clojure macros, which, at a high level, can be thought of as a very powerful tool for aspect-oriented programming.

我相信您可以在Clojure中最好地消除关注点-请注意,对象或方面并不是这种方法中真正必要的概念。

That's how I believe you can best decouple concerns in Clojure- Note that "objects" or "aspects" are not really necessary concepts in this approach.

这篇关于Clojure如何处理关注点分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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