Clojure库(Libraries)

Clojure库(Libraries) 首页 / Clojure入门教程 / Clojure库(Libraries)

使Clojure库如此强大的一件事是Clojure框架有很多可用的库,在前面的示例中,我们已经看到很多用于Web测试,Web开发,开发基于swing的应用程序的库,以及用于连接MySQL数据库的jdbc库,以下只是几个其他库的几个示例。

date.hml

该库允许Clojure处理XML数据,要使用的库版本为org.clojure/data.xml“ 0.0.8”。 data.xml支持解析和发出XML。解析函数将从Reader或InputStream读取XML。

以下是从字符串到XML的数据处理示例。

无涯教程网

(ns clojure.examples.example
   (use 'clojure.date.hml)
   (:gen-class))
(defn Example []
   (let [input-xml (java.io.StringReader. "<?xml version =\"1.0\"
      encoding =\"UTF-8\"?><example><clo><Tutorial>The Tutorial
      value</Tutorial></clo></example>")]
      (parse input-xml)))

#clojure.date.hml.Element{
   :tag :example, :attrs {}, :content (#clojure.date.hml.Element {
      :tag :clo, :attrs {}, :content (#clojure.date.hml.Element {
         :tag :Tutorial, :attrs {},:content ("The Tutorial value")})})}
(Example)

data.json

该库允许Clojure处理JSON数据,要使用的库版本为org.clojure/data.json" 0.2.6"。

以下是使用此库的示例。

(ns clojure.examples.example
   (:require [clojure.data.json :as json])
   (:gen-class))
(defn Example []
   (println (json/write-str {:a 1 :b 2})))
(Example)

上面的程序产生以下输出。

{\"a\":1,\"b\":2}

data.csv

该库允许Clojure处理" csv" 数据,要使用的库版本为org.clojure/data.csv" 0.1.3"。

以下是使用此库的示例。

(ns clojure.examples.example
   (require '[clojure.data.csv :as csv]
      '[clojure.java.io :as io])
   (:gen-class))
(defn Example []
   (with-open [in-file (io/reader "in-file.csv")]
      (doall
      (csv/read-csv in-file)))
   (with-open [out-file (io/writer "out-file.csv")]
   (csv/write-csv out-file
      [[":A" "a"]
      [":B" "b"]])))
(Example)

在上面的代码中," csv"函数将首先读取名为 in-file.csv 的文件,并将所有数据放入变量in-file中,接下来,我们使用write-csv函数将所有数据写入名为 out-file.csv的文件。

链接:https://www.learnfk.comhttps://www.learnfk.com/clojure/clojure-libraries.html

来源:LearnFk无涯教程网

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

手把手带你写一个 MiniTomcat -〔郭屹〕

结构学习力 -〔李忠秋〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

说透低代码 -〔陈旭〕

Go 语言项目开发实战 -〔孔令飞〕

RPC实战与核心原理 -〔何小锋〕

设计模式之美 -〔王争〕

玩转Spring全家桶 -〔丁雪丰〕

好记忆不如烂笔头。留下您的足迹吧 :)