Java进行Clojure的用户 [英] Java for Clojure users

查看:169
本文介绍了Java进行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.

例如,我不得不花一些时间(使用Google)查找平方功能的Java(Clojure中的符号数学/开方)。

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

您能推荐我一些好的信息资源的Java功能(库)那些不那么熟悉Java Clojure的用户?

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开发年前,但仍pretty不熟悉图书馆在那里。

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.to例如,开方 is在generic.math函数的在clojure.contrib。

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。例如数学/开方,按您的例子中,调用静态方法(这只是一个功能,基本上)开方从类数学

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.

一个类是(它作用于类函数)方法捆绑
也可以是一个数据类型:例如创建一个新的类类型双击(双1.2)其中初始化类双(周期为语法糖调用类的构造方法,它与值初始化您提供的值)类 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.

所以你可以看到那里发生了什么。你建一个新的双击值为 1.2 ,这是一个双。有点混乱,有,但一个真正的双在于重新presents一张双人床和可以做与双打的东西一类。

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值一个字符串,我们可以使用静态方法(这意味着我们不需要双击的特定实例,我们可以只是这样称呼它,我们叫开方 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

不要棘手那里。

Not to tricky there.

说,我们要使用我们初始化的东西的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"

现在,我们已经使用的也不是一成不变的方法,而这需要一个真正的,活字符串对象来处理。让我们来看看如何文档说它的工作原理:

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中,它会是隐含参数:这就是所谓的这个在Java中

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(字符串嘿)。) ,并得到相同的结果。

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.

例如,假设我们有一个的JFrame 的javax.swing 库处理。我们可能需要做一些事情的的它,而不是的的(你通常操作的的值,而不是的它们在功能性语言)。我们可以,像这样的:

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 

多托只是传递的第一个参数给所有您提供它的其他职能,并将它作为第一个参数传递给他们。所以在这里我们只是在做很多事情的的的的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).

这应该prepare你现在探索 JavaDoc中的自己。在这里,你会发现一切是提供给你一个标准的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的功能,如代理具体化以及扩展型和它的朋友们。我不经常使用它们,但是当我有需要,他们可以是无价的。我还在了解他们自己,其实。

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进行Clojure的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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