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
70
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
Learning Modern Web API Styles from IDL: REST, GraphQL, gRPC
lagenorhynque
0
29
Team Geek Revisited
lagenorhynque
0
21
Scala vs Clojure?: The Rise and Fall of Functional Languages in Opt Technologies
lagenorhynque
0
62
Exploring Immutable Persistent World with Clojure Collections
lagenorhynque
0
100
Introduction to French Grammar for English Learners
lagenorhynque
0
120
From Java through Scala to Clojure
lagenorhynque
0
1.2k
Introduction to Practical Etymology
lagenorhynque
0
85
Introduction to Metaprogramming
lagenorhynque
0
120
The World of Labour Law
lagenorhynque
0
29
Other Decks in Programming
See All in Programming
PostmanでAPIの動作確認が楽になった話
h455h1
0
170
GitHub Copilotのススメ
marcy731
1
200
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
Java 22 Overview
kishida
1
190
From Spring Boot 2 to Spring Boot 3 with Java 21 and Jakarta EE
ivargrimstad
0
210
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
210
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
7
1k
Elm Form Validation
bkuhlmann
0
510
PHP8.3の機能を振り返る / Review of PHP 8.3 features
seike460
PRO
1
110
使ってみよう Azure AI Document Intelligence
kosmosebi
2
330
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
1k
Milestoner
bkuhlmann
1
410
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
2
3.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Building Flexible Design Systems
yeseniaperezcruz
319
37k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
RailsConf 2023
tenderlove
4
540
Infographics Made Easy
chrislema
238
18k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
Optimising Largest Contentful Paint
csswizardry
8
2.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
241
1.2M
Writing Fast Ruby
sferik
621
60k
What's new in Ruby 2.0
geeforr
337
31k
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