在线Clojure - IDE、编辑器、编译器、解释器

使用SCI(Small Clojure Interpreter)在线运行Clojure代码:函数、map、vector、线程宏和惰性序列,直接在浏览器中运行。

ClojureScript via SCI (Scittle 0.8) · 在浏览器中运行,代码绝不会发送到我们的服务器。
main.cljsClojure
输出

            
        
Ctrl/⌘ + Enter 运行

在线 Clojure 编译器的功能(IDE、编辑器 & 解释器)

免费在线编写、运行和测试 Clojure 代码。本页面集在线 Clojure IDE、代码编辑器、编译器和解释器于一体:输入代码,点击“运行”,立即查看输出。一切都在浏览器中运行,无需安装,也无需注册账号。

  • 即时运行代码: ClojureScript via SCI (Scittle 0.8),首次加载后一秒内即可输出结果。
  • 代码编辑器: 语法高亮、行号、括号匹配和自动补全括号。
  • 清晰的错误提示: 错误信息和警告会显示在输出面板中,与在您自己的电脑上一样。
  • 全屏: Ctrl/⌘ + Shift + F,可调节字号,支持并排或堆叠布局,面板大小可调。
  • 自动保存: 代码保存在您的浏览器中;可一键下载为 main.cljs 或复制。
  • 放心尝试: 时间限制和“停止”按钮可终止无限循环。
  • 私密: Clojure 运行时在您的设备上运行,代码绝不会被上传。
  • 深色模式 以及键盘快捷键:Ctrl/⌘ + Enter 运行代码。
  • 免费: 无需注册,不限使用次数。

学习其他编程语言

How to use the online Clojure compiler

  1. Write or paste Clojure code into the editor. Reset example brings back the starter program with a factorial, a sorted list of maps, frequencies and a ->> pipeline.
  2. Press Run or use Ctrl + Enter (⌘ + Enter on a Mac).
  3. Text printed with println appears in the Output panel. After the run, the value of the last form is printed as => ... in readable pr-str form. If that value is nil, for example when the program ends with println, nothing extra is shown. Errors are shown with their message.
  4. Choose a time limit of 5, 15 or 60 seconds, or press Stop to end a run early.

On the first run the page downloads the interpreter, about 1 MB. Copy and main.cljs save your code, and A−/A+ change the text size.

Everything runs in a Web Worker in your browser. Your code is never sent to a server. The editor auto-saves your last program in your browser, separately for each compiler.

About Clojure, ClojureScript and SCI

Clojure is a modern Lisp built around immutable data structures and functional programming. The original Clojure runs on the Java Virtual Machine, and ClojureScript is the version that targets JavaScript. This page uses Scittle 0.8, which packages SCI, the Small Clojure Interpreter, for the browser. SCI interprets your code directly with ClojureScript semantics, with no JVM or build tool involved.

What's supported

  • Definitions and control flow: def, defn, fn and #(...) literals, let, if, when, cond, and loop/recur.
  • Persistent collections: maps, vectors, sets and lists, keywords, and destructuring in let and function arguments.
  • Sequence functions, including map, filter, reduce, sort-by, group-by and frequencies, plus lazy sequences such as range and iterate.
  • Threading macros -> and ->>, and atoms for managed mutable state.
  • JavaScript interop in ClojureScript style, as in (js/Math.sqrt 16) or (.toUpperCase "abc").

Limitations:

  • No JVM interop. Classes like java.util.Date or Thread do not exist.
  • No Leiningen or deps.edn. External libraries cannot be added, so only the code in the editor and the built-in namespaces are available.
  • No input box and no DOM. Define your test data in the program.

Worked example: immutability and threading

(def inventory {:apples 3 :pears 0})
(def restocked (assoc inventory :pears 12))
(println inventory)   ; {:apples 3, :pears 0}  (unchanged)
(println restocked)   ; {:apples 3, :pears 12}

(def counter (atom 0))
(dotimes [_ 5] (swap! counter inc))
(println @counter)    ; 5

(->> [3 8 1 9 4]
     (filter odd?)
     (map #(* % 10))
     (reduce +))      ; => 130

assoc returns a new map and leaves the original unchanged. When you need a value that changes over time, keep it in an atom and update it with swap!. The last form is a ->> pipeline. It passes the vector through each step as the last argument, and its result is printed as => 130. Use -> instead when the value should be the first argument, as with assoc and update on a map.

Differences from JVM Clojure and common pitfalls

  • Numbers are JavaScript numbers. With ClojureScript semantics there are no ratios or big integers. (/ 1 3) returns 0.3333333333333333, and very large integers lose precision.
  • There is no separate character type. A character literal like \a is a one-letter string, so (frequencies "mississippi") has string keys.
  • Laziness hides side effects. (map println xs) may print nothing if the result is never used. Use doseq or run! for side effects.
  • Infinite sequences. (range) and iterate never end, so always limit them with take. If you print an infinite sequence, the run continues until the time limit stops it.
  • println vs prn. println prints strings inside collections without quotes. prn keeps the quotes, which makes data easier to read.

Frequently asked questions

Is this real Clojure?

It is Clojure code run by SCI with ClojureScript semantics, not the JVM implementation. The core language, collections and sequence library work the same way, so most pure Clojure code runs unchanged. Java interop and JVM-specific behaviour do not work.

Can I add libraries or use a project file?

No. Leiningen, deps.edn and Maven dependencies are not available. Each run interprets the single file in the editor.

Why is the file called main.cljs?

Because the code runs with ClojureScript semantics. You can paste the contents into a .clj or .cljc file, as long as it does not rely on JavaScript interop.

Which other languages can I try?

The online Scheme interpreter is another Lisp to explore. For JavaScript itself, use the online JavaScript compiler, or see all online compilers.

更新于

已复制到剪贴板