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
75
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
Introduction to Tree Representations in RDB 2024
lagenorhynque
0
12
Boundary between Mutability and Immutability
lagenorhynque
0
17
Learning Modern Web API Styles from IDL: REST, GraphQL, gRPC
lagenorhynque
0
48
Team Geek Revisited
lagenorhynque
0
33
Scala vs Clojure?: The Rise and Fall of Functional Languages in Opt Technologies
lagenorhynque
0
91
Exploring Immutable Persistent World with Clojure Collections
lagenorhynque
0
110
Introduction to French Grammar for English Learners
lagenorhynque
0
130
From Java through Scala to Clojure
lagenorhynque
0
1.2k
Introduction to Practical Etymology
lagenorhynque
0
85
Other Decks in Programming
See All in Programming
開発部に不満を持っていたCSがエンジニアにジョブチェンしてわかった「勝手に諦めない」ことの大切さ
sakuraikotone
28
16k
ピグパーティにおけるMongoDB CommunityバージョンからAtlasへの移行事例
10969hotaka
0
130
Jetpack for KMP
fornewid
1
290
Ruby メモリ管理 プログラミング
megmogmog1965
0
130
APIのない大学ログインWebサービスをWKWebViewとJavaScriptでアプリ化した話
akidon0000
1
330
HMSコンペ 11th Solution (team : kansai-kaggler)
t88
1
680
CSC307 Lecture 09
javiergs
PRO
1
500
小さな開発会社を作った理由
polidog
0
1.9k
Harnessing Large Language Models for Training-free Video Anomaly Detection
tereka114
1
1.3k
20240706_CDKConf
takuyay0ne
0
1.2k
リハビリmruby
kishima
1
160
Terraformテスト入門
msato
0
520
Featured
See All Featured
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
35
6.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
44
4.7k
Git: the NoSQL Database
bkeepers
PRO
423
64k
A designer walks into a library…
pauljervisheath
201
24k
Atom: Resistance is Futile
akmur
261
25k
The Language of Interfaces
destraynor
151
23k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
224
21k
Build The Right Thing And Hit Your Dates
maggiecrowley
28
2.2k
Stop Working from a Prison Cell
hatefulcrawdad
266
20k
Building a Scalable Design System with Sketch
lauravandoore
458
32k
Building an army of robots
kneath
301
42k
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