Java for Clojure用户 [英] Java for Clojure users

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

问题描述

我一直在使用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.

例如,我不得不花一些时间(googling)在Java中找到方形函数(数学/ sqrt在clojure符号)。

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

你能推荐我一些好

它可以是任何东西 - 好的书籍,网页,论坛或任何东西。

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最简单的方法是不要用它。我认为一本书只是开始使用Java从Clojure有点多。没有那么多你真的需要知道,除非你真的开始深入到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 ,根据你的例子,是调用静态方法(只是一个函数,基本上) sqrt Math

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,这里有一个指南可以帮助您入门。我会假设你已经做了一些命令性的OO编程,但不是很多。

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.

现在,查看 Double class

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 ,这是一个双。有点混乱,但真正的双是一个类,代表一个双人,可以做与双打有关的事情。

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"

现在我们使用了一个不是静态的方法, real,live 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.

这里我们有一个方法返回一个字符串在Python中,它将是隐式参数 self :这被称为

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。

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

因为你在Java中处理可变数据和类,所以你需要能够将函数应用到类(Classes的实例,真的),而不需要返回值。

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 ,我们可能需要 )( 我们可以这样:

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安装中可用的一切。会有新的概念,但快速的谷歌搜索应该回答大部分的问题,你总是可以回来与具体的。

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函数如 proxy reify 以及 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

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

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