Getting started with clojure CLR

Source: Internet
Author: User
Tags reflector

After seeing "hackers and painters", are you so curious about lisp? Then, how many pages of ACL (ANSI Common LISP) Is it daunting? Sigh: it would be nice if you could write lisp code on. net clr one day! This day has arrived. This is clojure CLR. Looking at the language conversion matrix, clojure has a strong parasitic capability, which is even more powerful than Javascript. It has a place on CLR.

As a beginner, you must answer the following questions:
  • How to install it? How to run repl?
  • What ide is used to write clojure?
  • How to compile CLJ files?
  • What is the difference between clojure Clr and clojure JVM?
  • How does clojure call C #?
  • C # How to call clojure?
  • How does clojure call. Net winform?
The first step of installing this isn't that smooth, and if you download the code compilation from GitHub (https://github.com/clojure/clojure-clr), you may encounter a lot of problems, for example, you may find that the Lib Folder does not have any Microsoft Project dependency. dynamic. DLL and Microsoft. scripting. DLL. this problem is not big, as long as you go to the http://dlr.codeplex.com/download a copy of it (download extract copy and other steps omitted ), through this problem, we can know that clojure CLR is built on Microsoft's Dynamic Language Runtime (DLR. the reference problem is solved, followed by compiling the project. You may see the following error message:
  Error     16     The command ""D:\Code\clojure-clr-master\bin\4.0\Debug\Clojure.Compile.exe" clojure.core clojure.core.protocols clojure.main clojure.set clojure.zip  clojure.walk clojure.stacktrace clojure.template clojure.test clojure.test.tap clojure.test.junit clojure.pprint clojure.clr.io clojure.repl clojure.clr.shell clojure.string clojure.data clojure.reflect" exited with code 1.     Clojure.Compile

 

With this, my choice is: directly with the compiled Binary Package, do not patrol too long at the door of clojure.: https://github.com/clojure/clojure-clr/wiki/Getting-binaries http://sourceforge.net/projects/clojureclr/files/ running repl relief behind the contents of only one with clojuregraph clojure.main.exe, double click it, a new world comes:

Clojure 1.4.1user=> (+ 1 2)3user=> (println "hello world")hello worldniluser=>

 

  

If you follow the steps above, you may ask: How did you copy the above text? Clojure.main.exe is. net console application. You cannot directly use the selected operation by clicking the mouse on the interface. You can click the icon in the upper left corner of the application to edit the menu. You can select "select all ", then press enter to complete the copy. let's take a look at this slightly nonsense operation method: clojure ide does not have to look for clojure clr ide, as long as it is clojure IDE can be used. the options are: Eclipse plug-in, light table, VIM plug-in, and EMAC. Choose your preferred one. The discussion on stackoverflow below basically includes several clojure ides with high voices: http://stackoverflow.com/questions/463776/clojure-ide-on-windows compile the following file hello. CLJ (NS Hello)
(Println "Hello World" 2013) command line compilation: C: \ clojure-CLR 1.4> clojure.compile.exe hello
Compiling hello to. Hello World 2013
-- 415 milliseconds. After compilation is complete, the hello. CLJ. dll file will be generated and dragged into reflector to see what the generated code looks like. Note that the following 0x7ddl is the constant 2013:

public class __Init__{    // Fields    protected internal static Var const__0;    protected internal static AFn const__1;    protected internal static Var const__2;    protected internal static object const__3;    // Methods    static __Init__()    {        try        {            Compiler.PushNS();            __static_ctor_helper_constants();        }        finally        {            Var.popThreadBindings();        }    }    private static void __static_ctor_helper_constants()    {        const__0 = RT.var("clojure.core", "in-ns");        const__1 = Symbol.intern(null, "hello");        const__2 = RT.var("clojure.core", "println");        const__3 = 0x7ddL;    }    public static void Initialize()    {        ((IFn) const__0.getRawRoot()).invoke(const__1);        ((IFn) new hello$loading__16463__auto____5()).invoke();        ((IFn) const__2.getRawRoot()).invoke("hello world", const__3);    }}

 

  

What is the difference between clojure Clr and clojure JVM?  Clojure CLR project goals:

-- Implement a feature-complete clojure on top of CLR/DLR.
-- Stay as close as possible to the JVM implementation.
-- Have some fun. Different basics must be different, such as the https://github.com/richhickey/clojure-clr/wiki/Completing-CLR-interop clojure calls C # CLR InterOP is essential tially the same as JVM InterOP. However, we have to make a number of extensions to allow for parts of the CLR object model that are not in the JVM. https://github.com/richhickey/clojure-clr/wiki/CLR-Interop

User => (system. console/writeline "now we use console writeline") Now we use console writelinenil; read/write file user => (DEF file (system. io. streamwriter. "test.txt") # 'user/fileuser => (. writeline file "= Hello clojure =") niluser => (. close file) niluser => (println (slurp "test.txt") Warning: (slurp f enc) is deprecated, use (slurp F: encoding ENC ). === Hello clojure === niluser =>

 

  

We translate the code for capturing web pages into clojure: C #: 

System.Net.WebClient webClient= new System.Net.WebClient();byte[] bResponse = webClient.DownloadData("http://www.baidu.com");Console.WriteLine(Encoding.UTF8.GetString(bResponse));

  

Clojure: 

(import (System.Net WebClient))(import (System.Text Encoding))  (.GetString Encoding/UTF8        (.DownloadData (WebClient.) "http://www.baidu.com"))

 

 

We will continue to write some articles on this code. We will compile it into the hello. CLJ file:

 

(ns hello) (import (System.Net WebClient))(import (System.Text Encoding))(defn getbaidu []  (.GetString Encoding/UTF8        (.DownloadData (WebClient.) "http://www.baidu.com")))

 

Drag hello. CLJ. DLL into reflector again to view the generated code:

[Serializable]public class hello$getbaidu__11 : AFunction{    // Methods    public override bool HasArity(int num1)    {        if (num1 != 0)        {            return false;        }        return true;    }     public override object invoke()    {        return Encoding.UTF8.GetString(new WebClient().DownloadData("http://www.baidu.com"));    }}

Is there a problem that has been solved without knowing it? (How to call CLJ code in C) Clojure's call to. Net winform is certainly not a problem.You only need to load the system. Windows. Forms assembly to import the corresponding class. The sample code is as follows:

user=> (System.Reflection.Assembly/Load "System.Windows.Forms,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")#<RuntimeAssembly System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>user=> (import (System.Windows.Forms MessageBox))System.Windows.Forms.MessageBoxuser=> (MessageBox/Show "Hello world from clojure-clr!" "Clojure-CLR DialogBox")OKuser=>

 

Happy trip to clojure CLR! [0] mirror http://www.4clojure.com/[2] http://clojure-doc.org/[3] http://try-clojure.org/%4] http://dev.clojure.org/display/doc/getting%started%5] the last thumbnail of the https://github.com/rippinrobr/clojure-clr-intro is:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.