面向 Clojure 用户的 Java [英] Java for Clojure users

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

问题描述

我一直在断断续续地使用 Lisp,我正在赶上 clojure.clojure的好处是我可以很自然地使用所有java函数,clojure的坏处也是我必须自然了解java函数.

I've been using Lisp on and off, and I'm catching up with clojure. The good thing about clojure is that I can use all the java functions naturally, and the bad thing about clojure is also that I have to know java function naturally.

例如,我不得不花一些时间(谷歌搜索)在 Java 中找到平方函数(Clojure 符号中的数学/sqrt).

For example, I had to spend some time (googling) to find square function in Java (Math/sqrt in clojure notation).

能否为不熟悉 Java 的 clojure 用户推荐一些关于 Java 函数(库)的好的信息资源?

Could you recommend me some good information resource for Java functions (libraries) for clojure users that are not so familiar with Java?

它可以是任何东西 - 好书、网页、论坛或其他任何东西.

It can be anything - good books, webpages, forums or whatever.

推荐答案

我第一次开始使用 Clojure 时遇到了类似的问题.几年前我做过一些 Java 开发,但对那里的库仍然很不熟悉.

I had similar problems when I first started using Clojure. I had done some Java development years ago, but was still pretty unfamiliar with the libraries out there.

我发现使用 Java 的最简单方法是不真正使用它.我认为从 Clojure 开始使用 Java 的书会有点多.没有那么多你真正需要知道的,除非你真的开始深入研究 JVM/Java 库.让我解释一下.

I find the easiest way to use Java is to not really use it. I think a book would be a little bit much to just get started using Java from Clojure. There isn't that much you really need to know, unless you really start getting down into the JVM/Java libraries. Let me explain.

花更多时间学习如何从内到外使用 Clojure,并熟悉 Clojure-Contrib.例如,sqrt 在 clojure.contrib 中的 generic.math-functions 中.

Spend more time learning how to use Clojure inside and out, and become familiar with Clojure-Contrib. For instance, sqrt is in generic.math-functions in clojure.contrib.

您需要的许多东西实际上已经在 Clojure 中了——但仍有很多还没有.

Many of the things you'll need are in fact already in Clojure–but still plenty are not.

熟悉 Clojure 中使用 Java 的调用约定和语法糖.例如Math/sqrt,根据您的示例,正​​在调用 Math 类中的静态方法(基本上只是一个函数)sqrt.

Become familiar with calling conventions and syntactic sugar in Clojure for using Java. e.g. Math/sqrt, as per your example, is calling the static method (which just a function, basically) sqrt from the class Math.

无论如何,如果您发现自己真的需要使用 Java,这里的指南应该可以帮助您入门.我将假设您已经完成了一些 命令式面向对象编程,但除此之外并没有太多.即使你没有,你也应该没事.

Anyway, here's a guide that should help you get started if you find yourself really needing to use Java. I'm going to assume you've done some imperative OO programming, but not much else. And even if you haven't, you should be okay.

类是一组方法(作用于类的函数),它们也可以是数据类型:例如创建一个 Double 类型的新类: (Double.1.2) 初始化类 Double(句点是调用类构造函数方法的语法糖,它初始化具有您提供的值的类)具有值 1.2.

A class is a bundle of methods (functions which act on the class) that can also be a data type: e.g. to create a new class of the type Double : (Double. 1.2) which initializes the class Double (the period is the syntactic sugar for calling the class constructor methods, which initialize the class with the values you provide) with the value 1.2.

现在,看看Java 6 API 中的双重:

Now, look at the Double class in the Java 6 API:

Double

public Double(double value)

Constructs a newly allocated Double object that represents the 
primitive double argument.

Parameters:
value - the value to be represented by the Double.

所以你可以看到那里发生了什么.你建造"了一个新的 Double 值为 1.2,它是一个双精度值.这有点令人困惑,但实际上 Double 是一个表示 Double 并且可以执行与 double 相关的事情的类.

So you can see what happened there. You "built" a new Double with value 1.2, which is a double. A little confusing there, but really a Double is a class that represents a Double and can do things relating to doubles.

例如,要从字符串中解析出 Double 值,我们可以使用静态方法(意味着我们不需要 Double 的特定实例,我们可以像调用一样调用它sqrt) parseDouble(String s):

For instance, to parse a Double value out of a string, we can use the static method (meaning we don't need a particular instance of Double, we can just call it like we called sqrt) parseDouble(String s):

(Double/parseDouble "1.2") => 1.2

不要在那里棘手.

假设我们想要使用一个我们初始化为某个东西的 Java 类.不太难:

Say we want to use a Java class that we initialized to something. Not too difficult:

(-> (String. "Hey there") ;; make a new String object
    (.toUpperCase)) ;; pass it to .toUpperCase (look up -> to see what it does)
                    ;; toUpperCase is a non-static method

=> "HEY THERE"

所以现在我们使用了一个非静态的方法,它需要一个真实的、实时的String 对象来处理.让我们看看文档是如何工作的:

So now we've used a method which is not static, and which requires a real, live String object to deal with. Let's look at how the docs say it works:

toUpperCase

public String toUpperCase()

Converts all of the characters in this String to upper case using 
the rules of the default locale. This method is equivalent to
toUpperCase(Locale.getDefault()).

Returns:
the String, converted to uppercase.

所以这里我们有一个返回字符串的方法(如定义中 public 后面的String"所示,不带参数.但是等等!它确实带参数.在 Python 中,它是隐式参数self:在Java中称为this.

So here we have a method which returns a string (as shown by the "String" after the public in the definition, and takes no parameters. But wait! It does take a parameter. In Python, it'd be the implicit parameter self: this is called this in Java.

我们也可以使用这样的方法:(.toUpper (String."Hey there")) 并得到相同的结果.

We could also use the method like this: (.toUpper (String. "Hey there")) and get the same result.

由于您在 Java 中处理可变数据和类,因此您需要能够将函数应用于类(实际上是类的实例)并且不期望返回值.

Since you deal with mutable data and classes in Java, you need to be able to apply functions to Classes (instances of Classes, really) and not expect a return value.

例如,假设我们正在处理来自 javax.swing 库的 JFrame.我们可能需要为做很多事情,而不是使用它(您通常使用值操作,而不是onem> 它们在函数式语言中).我们可以这样:

For instance, say we're dealing with a JFrame from the javax.swing library. We might need to do a number of things to it, not with it (you generally operate with values, not on them in functional languages). We can, like this:

(doto (JFrame. "My Frame!");; clever name
   (.setContentPane ... here we'd add a JPanel or something to the JFrame)
   (.pack) ;; this simply arranges the stuff in the frame–don't worry about it
   (.setVisibleTrue)) ;; this makes the Frame visible 

doto 只是将它的第一个参数传递给您提供的所有其他函数,并将其作为第一个参数传递给它们.所以这里我们只是做了很多事情JFrame 不返回任何特别的东西.所有这些方法都在文档中列为 JFrame 的方法(或其超类……暂时不要担心).

doto just passes its first argument to all the other functions you supply it, and passes it as the first argument to them. So here we're just doing a lot of things to the JFrame that don't return anything in particular. All these methods are listed as methods of the JFrame in the documentation (or its superclasses… don't worry about those yet).

这应该让您为现在自己探索JavaDocs做好准备.在这里,您将找到标准 Java 1.6 安装中可用的所有内容.会有新概念出现,但在 Google 上快速搜索应该可以回答您的大部分问题,而且您可以随时回到这里了解具体的问题.

This should prepare you for now exploring the JavaDocs yourself. Here you'll find everything that is available to you in a standard Java 1.6 install. There will be new concepts, but a quick Google search should answer most of your questions, and you can always come back here with specific ones.

一定要研究其他重要的 Clojure 函数,如 proxyreify 以及 extend-type 和它的朋友.我不经常使用它们,但当我需要时,它们可能是无价的.事实上,我自己仍然在理解它们.

Be sure to look into the other important Clojure functions like proxy and reify as well as extend-type and its friends. I don't often use them, but when I need to, they can be invaluable. I still am understanding them myself, in fact.

那里有很多,但主要是数量问题而不是复杂性问题.这不是一个坏问题.

There's a ton out there, but it's mostly a problem of volume rather than complexity. It's not a bad problem to have.

  • Static or Nonstatic? ;; a guide to statis vs. nonstatic methods
  • The Java Class Library ;; an overview of what's out there, with a nice picture
  • The JavaDocs ;; linked above
  • Clojure Java Interop Docs ;; from the Clojure website
  • Best Java Books ;; as per clartaq's answer

这篇关于面向 Clojure 用户的 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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