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

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

问题描述

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

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

而且,因为有这样的原则在 1 个数据结构上工作的 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 中通过方面(面向方面​​的编程)和注释实现.这是我对这个概念的看法,可能有些局限,所以不要想当然.

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