Clojure如何解决问题? [英] How does Clojure aproach Separation of Concerns?

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

问题描述

Clojure如何解决问题?因为代码是数据,函数可以作为参数传递并用作返回...

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

并且,因为有一个原则更好的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应用程序,总体目标是接受请求并将其转换为响应,这可以被认为是简单地将请求数据转换为响应数据。 (在一个非平凡的网络应用程序中,启动数据可能不仅包括请求,还包括会话和数据库信息)。大多数编程任务可以这样考虑。

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