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
Anthropic Cookbook のおすすめレシピ
schroneko
7
980
2 週間で Twitter Bot を作ってみた
contour_gara
0
510
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
340
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
2
200
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
180
使ってみよう Azure AI Document Intelligence
kosmosebi
2
320
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
210
GitHub Copilotのススメ
marcy731
1
200
Java 22 Overview
kishida
1
180
Featured
See All Featured
Designing for Performance
lara
601
67k
How to train your dragon (web standard)
notwaldorf
73
5.2k
Art, The Web, and Tiny UX
lynnandtonic
289
19k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
187
16k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
322
20k
The Brand Is Dead. Long Live the Brand.
mthomps
49
29k
A Modern Web Designer's Workflow
chriscoyier
689
190k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
RailsConf 2023
tenderlove
4
540
The Invisible Customer
myddelton
114
12k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
659
120k
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