Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Simple DSLs in Clojure
Search
Kent OHASHI
October 09, 2020
Programming
0
110
Simple DSLs in Clojure
Clojureで作る"simple"なDSL
Clojureらしい内部DSLの開発方法について簡単に紹介。
"simple"なアプローチでDSLを作ろう!
Kent OHASHI
October 09, 2020
Tweet
Share
More Decks by Kent OHASHI
See All by Kent OHASHI
関数型言語テイスティング: Haskell, Scala, Clojure, Elixirを比べて味わう関数型プログラミングの旨さ
lagenorhynque
1
92
純LISPから考える関数型言語のプリミティブ: Clojure, Elixir, Haskell, Scala
lagenorhynque
1
96
From Scala/Clojure to Kotlin
lagenorhynque
0
43
TDD with RDD: Changed Developer Experience through Clojure/Lisp REPLs
lagenorhynque
0
76
My Favourite Book in 2024: Get Rid of Your Japanese Accent
lagenorhynque
1
110
do Notation Equivalents in JVM languages: Scala, Kotlin, Clojure
lagenorhynque
0
79
map関数の内部実装から探るJVM言語のコレクション: Scala, Kotlin, Clojureコレクションの基本的な設計を理解しよう
lagenorhynque
0
73
Kotlin Meets Data-Oriented Programming
lagenorhynque
0
66
Introduction to Tree Representations in RDB 2024
lagenorhynque
0
98
Other Decks in Programming
See All in Programming
Vue・React マルチプロダクト開発を支える Vite
andpad
0
110
時間軸から考えるTerraformを使う理由と留意点
fufuhu
16
4.8k
[FEConf 2025] 모노레포 절망편, 14개 레포로 부활하기까지 걸린 1년
mmmaxkim
0
1.6k
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
1.5k
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
530
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
860
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
Amazon RDS 向けに提供されている MCP Server と仕組みを調べてみた/jawsug-okayama-2025-aurora-mcp
takahashiikki
1
110
AI時代のUIはどこへ行く?
yusukebe
18
8.9k
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
110
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
10
4.2k
The Past, Present, and Future of Enterprise Java with ASF in the Middle
ivargrimstad
0
120
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
303
21k
How STYLIGHT went responsive
nonsquared
100
5.8k
Fireside Chat
paigeccino
39
3.6k
Why Our Code Smells
bkeepers
PRO
339
57k
GraphQLの誤解/rethinking-graphql
sonatard
72
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Thoughts on Productivity
jonyablonski
70
4.8k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Transcript
Clojure で作る "simple" な DSL
lagénorhynque (defprofile lagénorhynque :id @lagenorhynque :reading "/laʒenɔʁɛ̃ k/" :aliases ["
カマイルカ "] :languages [Clojure Haskell English français] :interests [programming language-learning law mathematics] :commits ["github.com/lagenorhynque/duct.module.pedestal" "github.com/lagenorhynque/duct.module.cambium"] :contributes ["github.com/japan-clojurians/clojure-site-ja"])
Lisp 系⾔語で ( 内部 )DSL といえばマクロを使うもの ? マクロを定義すると コンパイル前にコード(AST) を⾃在に変換し
評価を制御することができる マクロ⾃体は rst-class object ではないので composability が損なわれる
Clojure コミュニティではマクロ定義は控えめ 「マクロ・クラブ」のルール 1. マクロを書くな 2. それがパターンをカプセル化する唯⼀の ⽅法ならば、マクロを書け cf. data
> functions > macros data-driven, data-oriented なものを好む 『プログラミングClojure 第2 版』
Clojure コミュニティでは "simple" であることを重視 ( のプレゼン) ⼀⾔でいえば、"simple" に設計しよう "simple" とは単⼀の役割/
責務のみを上 ⼿く果たすこと cf. UNIX 哲学 "complex" にするのを避けて"simple" なもの を組み合わせよう Simple Made Easy Rich Hickey
data-driven な DSL の例
のテンプレート : HTML React のJSX に相当するテンプレート Reagent (defn some-component []
[:div [:h3 "I am a component!"] [:p.someclass "I have " [:strong "bold"] [:span {:style {:color "red"}} " and red"] " text."]])
: SQL cf. ※開発予定 Salesforce のSQL ⾵⾔語 に対するDSL Honey SQL
(def sqlmap {:select [:a :b :c] :from [:foo] :where [:= :f.a "baz"]}) lagenorhynque/honeysoql SOQL
: ルーティング DSL bidi (def routes ["/" {"index.html" :index "articles/"
{"index.html" :article-index [:id "/article.html"] :article}}])
cf. : マクロベースのルーティング DSL Compojure (defroutes app (GET "/" []
"<h1>Hello World</h1>") (route/not-found "<h1>Page not found</h1>"))
: バリデーションスキーマ malli (def Address [:map [:id string?] [:tags [:set
keyword?]] [:address [:map [:street string?] [:city string?] [:zip int?] [:lonlat [:tuple double? double?]]]]])
data-driven だと何がうれしい ? Clojure でプログラム的に⾃由に操作できる ⾔語機能と標準ライブラリでそのまま扱える read (parse) も print
もそのままできる Clojure データはclojure.spec で仕様を記述で きる Clojure データとしてどのように表現し、ど のように変換/ 解釈するかだけに集中できる 問題も"simple" になる
参考 : edn 形式 Clojure リテラル( のサブセット) によるシリア ライゼーションフォーマット Clojure
にとってのS 式表現 ベクター、マップ、セットなどもある JavaScript にとってのJSON 相当 タグによる拡張も可能(extensible) Clojure とはedn 形式で表現されたデータを扱う ことに特化した⾔語ともいえる(?) edn (extensible data notation)
Further Reading Clojure/ClojureScript 関連リンク集 - Qiita Data > Functions >
Macros. But why? - LispCast