搬运工 发表于 2022-10-22 20:00:02

Clojure经典实例



译者序 .....................................................................................................................................................xi
前言........................................................................................................................................................xiii
第 1 章 原生数据 ................................................................................................................................1
1.0 简介 .............................................................................................................................................1
1.1 改变字符串的大小写 .................................................................................................................2
1.2 清除字符串中的空白字符 .........................................................................................................3
1.3 利用部件构建字符串 .................................................................................................................5
1.4 将字符串作为字符序列 .............................................................................................................6
1.5 字符与整数的转换 ....................................................................................................................7
1.6 格式化字符串 ............................................................................................................................9
1.7 按模式查找字符串 ...................................................................................................................11
1.8 利用正则表达式从字符串中取出值 .......................................................................................12
1.9 对字符串执行查找和替换 .......................................................................................................13
1.10 将字符串切分成部分 .............................................................................................................15
1.11 基于数量为字符串加复数 .....................................................................................................16
1.12 在字符串、符号和关键字之间的转换 .................................................................................18
1.13 利用非常大或非常小的数来保持精度 .................................................................................20
1.14 使用有理数 .............................................................................................................................21
1.15 解析数字 ................................................................................................................................23
1.16 数的截断和舍入 .....................................................................................................................24
1.17 模糊比较 .................................................................................................................................26
1.18 三角计算 .................................................................................................................................27
1.19 根据不同的进制输入和输出整数 .........................................................................................29
vi | 目录
1.20 计算数值集合的统计值 .........................................................................................................30
1.21 位操作 .....................................................................................................................................33
1.22 生成随机数 .............................................................................................................................34
1.23 操作货币 .................................................................................................................................36
1.24 生成唯一 ID ............................................................................................................................37
1.25 得到当前的日期和时间 .........................................................................................................39
1.26 用字面值来表示日期 .............................................................................................................40
1.27 利用 clj-time 解析日期和时间 ...........................................................................................42
1.28 利用 clj-time 格式化日期 ...................................................................................................43
1.29 比较日期 ................................................................................................................................45
1.30 计算时间间隔的长度 .............................................................................................................46
1.31 生成一系列的日期和时间 .....................................................................................................48
1.32 利用原生 Java 类型生成一系列日期和时间 ........................................................................49
1.33 根据日期间的关系取得日期 .................................................................................................52
1.34 处理时区 .................................................................................................................................53
1.35 将 Unix 时间戳转换成 Date 对象 .........................................................................................55
1.36 将 Date 对象转换成 Unix 时间戳 .........................................................................................56
第 2 章 复合数据 ..............................................................................................................................58
2.0 简介 ...........................................................................................................................................58
2.1 创建列表 ...................................................................................................................................59
2.2 从已有的数据结构创建列表 ...................................................................................................61
2.3 在列表中“添加”一个元素 ...................................................................................................62
2.4 从列表中“移除”一个元素 ...................................................................................................63
2.5 测试是否列表 ...........................................................................................................................64
2.6 创建向量 ...................................................................................................................................65
2.7 在向量中“添加”一个元素 ...................................................................................................66
2.8 从向量中“移除”一个元素 ...................................................................................................67
2.9 取得索引处的值 .......................................................................................................................68
2.10 设置索引处的值 .....................................................................................................................70
2.11 创建集 .....................................................................................................................................71
2.12 在集中添加和移除元素 ........................................................................................................73
2.13 测试集成员 .............................................................................................................................74
2.14 使用集操作 ............................................................................................................................76
2.15 创建映射表 .............................................................................................................................77
2.16 从映射表中取得值 ................................................................................................................79
2.17 从映射表中同时取出多个键 .................................................................................................82
2.18 设置映射表中的键 ................................................................................................................84
2.19 用复合值作为映射表的键 ....................................................................................................86
目录 | vii
2.20 将映射表作为序列(或反过来) ..........................................................................................88
2.21 对映射表应用函数 ................................................................................................................90
2.22 一个键保存多个值 ................................................................................................................92
2.23 合并映射表 ............................................................................................................................95
2.24 值的比较与排序 ....................................................................................................................97
2.25 从集合中移除重复元素 .......................................................................................................100
2.26 检测集合是否包含几个值中的一个 ...................................................................................102
2.27 实现定制的数据结构:红黑树(第一部分) .....................................................................103
2.28 实现定制的数据结构:红黑树(第二部分) .....................................................................106
第 3 章 广义计算 ............................................................................................................................111
3.0 简介 .........................................................................................................................................111
3.1 运行最小的 Clojure REPL .....................................................................................................111
3.2 交互式文档 .............................................................................................................................112
3.3 探索命名空间 ........................................................................................................................114
3.4 尝试库而不指明依赖关系 .....................................................................................................116
3.5 运行 Clojure 程序 ...................................................................................................................117
3.6 从命令行运行程序 ................................................................................................................119
3.7 解析命令行参数 .....................................................................................................................121
3.8 创建定制的项目模板 .............................................................................................................124
3.9 构建具有多态行为的函数 .....................................................................................................128
3.10 扩展内建的类型 ..................................................................................................................133
3.11 用 core.async 解除消费者和生产者的耦合 ......................................................................135
3.12 用 core.match 为 Clojure 表达式制作解析器 ....................................................................138
3.13 用 core.logic 查询层级图 ..................................................................................................141
3.14 演奏儿歌 ...............................................................................................................................146
第 4 章 本地 I/O ............................................................................................................................150
4.0 简介 .........................................................................................................................................150
4.1 写入 STDOUT 和 STDERR ...................................................................................................150
4.2 从控制台读入一次击键 .........................................................................................................152
4.3 执行系统命令 .........................................................................................................................153
4.4 访问资源文件 .........................................................................................................................156
4.5 复制文件 .................................................................................................................................157
4.6 删除文件或目录 .....................................................................................................................159
4.7 列出目录中的文件 .................................................................................................................161
4.8 文件的内存映射 .....................................................................................................................163
4.9 读写文本文件 .........................................................................................................................164
4.10 使用临时文件 .......................................................................................................................165
4.11 在任意位置读写文件 ...........................................................................................................166
viii | 目录
4.12 并行文件处理 .......................................................................................................................168
4.13 带归约的并行文件处理 .......................................................................................................170
4.14 读写 Clojure 数据 .................................................................................................................172
4.15 在配置文件中使用 edn ........................................................................................................174
4.16 将记录作为 edn 值发布 .......................................................................................................178
4.17 读取 Clojure 数据时处理未知的带标签字面值 .................................................................180
4.18 从文件中读取属性 ...............................................................................................................182
4.19 读写二进制文件 ...................................................................................................................184
4.20 读写 CSV 数据 .....................................................................................................................186
4.21 读写压缩文件 .......................................................................................................................187
4.22 处理 XML 数据 ....................................................................................................................189
4.23 读写 JSON 数据 ...................................................................................................................190
4.24 生成 PDF 文件......................................................................................................................192
4.25 生成带可滚动文本的 GUI 窗口 ..........................................................................................196
第 5 章 网络 I/O 和 Web 服务 ...................................................................................................200
5.0 简介 .........................................................................................................................................200
5.1 发出 HTTP 请求 .....................................................................................................................200
5.2 执行异步 HTTP 请求 .............................................................................................................202
5.3 发出 Ping 请求........................................................................................................................204
5.4 取得并解析 RSS 数据 ............................................................................................................205
5.5 发送邮件 .................................................................................................................................206
5.6 用 RabbitMQ 实现队列通信 ..................................................................................................210
5.7 通过 MQTT 与嵌入式设备通信............................................................................................215
5.8 并发使用 ZeroMQ .................................................................................................................219
5.9 创建 TCP 客户端....................................................................................................................222
5.10 创建 TCP 服务器..................................................................................................................223
5.11 收发 UDP 包 .........................................................................................................................227
第 6 章 数据库 ................................................................................................................................230
6.0 简介 .........................................................................................................................................230
6.1 连接 SQL 数据库 ...................................................................................................................231
6.2 利用连接池连接 SQL 数据库 ...............................................................................................233
6.3 操作 SQL 数据库 ...................................................................................................................236
6.4 用 Korma 简化 SQL ..............................................................................................................242
6.5 用 Lucene 进行全文查找 .......................................................................................................245
6.6 用 ElasticSearch 建立数据索引 .............................................................................................248
6.7 使用 Cassandra .......................................................................................................................252
6.8 使用 MongoDB .......................................................................................................................256
6.9 使用 Redis ...............................................................................................................................259
目录 | ix
6.10 连接 Datomic 数据库 ...........................................................................................................262
6.11 为 Datomic 数据库定义数据模式 .......................................................................................264
6.12 向 Datomic 写入数据 ...........................................................................................................267
6.13 从 Datomic 数据库中删除数据 ...........................................................................................270
6.14 尝试 Datomic 事务而不提交 ...............................................................................................272
6.15 遍历 Datomic 索引 ...............................................................................................................274
第 7 章 Web 应用 ..........................................................................................................................277
7.0 简介 .........................................................................................................................................277
7.1 Ring 简介 ................................................................................................................................277
7.2 使用 Ring 中间件 ...................................................................................................................279
7.3 用 Ring 提供静态文件 ...........................................................................................................281
7.4 用 Ring 处理表单数据 ...........................................................................................................282
7.5 用 Ring 处理 Cookie...............................................................................................................284
7.6 用 Ring 保存会话 ...................................................................................................................286
7.7 在 Ring 中读写请求和响应的头 ...........................................................................................288
7.8 用 Compojure 路由请求 ........................................................................................................289
7.9 用 Ring 执行 HTTP 重定向 ...................................................................................................291
7.10 用 Liberator 构建 REST 风格的应用 ..................................................................................292
7.11 用 Enlive 实现 HTML 模板 .................................................................................................294
7.12 用 Selmer 实现模板 ..............................................................................................................300
7.13 用 Hiccup 实现模板 .............................................................................................................305
7.14 渲染 Markdown 文档 ...........................................................................................................307
7.15 用 Luminus 来构建应用 .......................................................................................................310
第 8 章 性能与开发效率 ..............................................................................................................312
8.0 简介 .........................................................................................................................................312
8.1 AOT 编译 ................................................................................................................................312
8.2 将项目打包成 JAR 文件 .......................................................................................................314
8.3 创建 WAR 文件......................................................................................................................317
8.4 将应用作为守护进程运行 .....................................................................................................320
8.5 利用类型暗示减轻性能问题 .................................................................................................325
8.6 用原生 Java 数组进行快速数学运算 ....................................................................................328
8.7 用 Timbre 进行简单剖析 .......................................................................................................330
8.8 用 Timbre 记日志 ...................................................................................................................332
8.9 向 Clojars 发布库 ...................................................................................................................334
8.10 使用宏来简化 API 弃用 ......................................................................................................336
第 9 章 分布式计算 .......................................................................................................................341
9.0 简介 .........................................................................................................................................341
x | 目录
9.1 用 Storm 构建活动推送系统 .................................................................................................342
9.2 用抽取转换加载(ETL)管道来处理数据 ..........................................................................350
9.3 聚合大型文件 ........................................................................................................................354
9.4 测试 Cascalog 工作流 ............................................................................................................359
9.5 设置 Cascalog 任务的检查点 ................................................................................................361
9.6 解释 Cascalog 查询 ................................................................................................................363
9.7 在 Elastic MapReduce 上运行 Cascalog 任务 .......................................................................365
第 10 章 测试 ..................................................................................................................................367
10.0 简介 .......................................................................................................................................367
10.1 单元测试 ...............................................................................................................................368
10.2 用 Midje 测试 .......................................................................................................................372
10.3 通过随机输入进行彻底测试 ...............................................................................................375
10.4 寻找导致失败的值 ...............................................................................................................379
10.5 运行基于浏览器的测试 .......................................................................................................381
10.6 追踪代码执行 .......................................................................................................................386
10.7 用 core.typed 避免空指针异常 ..........................................................................................389
10.8 用 core.typed 验证 Java 互操作.........................................................................................392
10.9 用 core.typed 检查高阶函数 ..............................................................................................395
关于作者 ..............................................................................................................................................399
关于封面 ..............................................................................................................................................399


**** Hidden Message *****

Maple2d 发表于 2022-10-22 20:56:14

啥也不说了,感谢楼主分享哇!

hgzhou6 发表于 2022-10-23 16:16:26

啥也不说了,感谢楼主分享哇!

hellojackd 发表于 2022-10-24 21:53:09

啥也不说了,感谢楼主分享哇!

2500103423 发表于 2024-7-1 22:05:45

66666666666
页: [1]
查看完整版本: Clojure经典实例