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
D言語に入門した話 / Introduction to D language
Search
chapati
April 19, 2022
Technology
0
320
D言語に入門した話 / Introduction to D language
D言語に入門した話です
Twitterでご指摘いただいたのですが
- スライド15ページに記載のCTFEの実行について,バイトコードへの変換は特に行っていないようです
chapati
April 19, 2022
Tweet
Share
More Decks by chapati
See All by chapati
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
210
Other Decks in Technology
See All in Technology
Perl歴約10年のエンジニアがフルスタックTypeScriptに出会ってみた
papix
1
180
CodePipelineのアクション統合から学ぶAWS CDKの抽象化技術 / codepipeline-actions-cdk-abstraction
gotok365
5
310
AWSで作るセキュアな認証基盤with OAuth mTLS / Secure Authentication Infrastructure with OAuth mTLS on AWS
kaminashi
0
190
Mastraに入門してみた ~AWS CDKを添えて~
tsukuboshi
0
340
白金鉱業Meetup_Vol.18_生成AIはデータサイエンティストを代替するのか?
brainpadpr
3
180
PostgreSQL Log File Mastery: Optimizing Database Performance Through Advanced Log Analysis
shiviyer007
PRO
1
140
Aspire をカスタマイズしよう & Aspire 9.2
nenonaninu
0
190
2025-04-24 "Manga AI Understanding & Localization" Furukawa Arata (CyberAgent, Inc)
ornew
2
290
watsonx.data上のベクトル・データベース Milvusを見てみよう/20250418-milvus-dojo
mayumihirano
0
130
AIコーディングの最前線 〜活用のコツと課題〜
pharma_x_tech
4
2.7k
SREからゼロイチプロダクト開発へ ー越境する打席の立ち方と期待への応え方ー / Product Engineering Night #8
itkq
2
1k
4/17/25 - CIJUG - Java Meets AI: Build LLM-Powered Apps with LangChain4j (part 2)
edeandrea
PRO
0
140
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
90
6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
21k
A Tale of Four Properties
chriscoyier
158
23k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.5k
Adopting Sorbet at Scale
ufuk
76
9.3k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
BBQ
matthewcrist
88
9.6k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Writing Fast Ruby
sferik
628
61k
Transcript
D言語に入門した話 チャパティ
自己紹介 チャパティ (Twitter:@_shouth_kit) 情報工学課程4回 プログラムを書くのが好き ちゃぱえもんと呼ばれることもある
D言語って 知ってますか?
None
None
D言語の簡単な歴史 - 1999年に開発開始 - 2001年に最初のバージョンがリリース - 2007年にバージョン1.0に到達 紆余曲折を得て現在はD1は廃止され 現行のバージョンはD2
プログラム例: Hello world import std.stdio; int main() { writeln("Hello world!");
}
プログラム例: 基本的な制御構造 import std.stdio; int main() { for (int i
= 1; i <= 100; i++) { if (i % 15 == 0) { writeln("FizzBuzz"); } else if (i % 3 == 0) { writeln("Fizz"); } else if (i % 5 == 0) { writeln("Buzz"); } else { writeln(i); } } }
ね,簡単でしょう?
メタプログラミング にっょぃ struct Vector(size_t D, T = double) if (__traits(isFloating,
T)) { T[D] num; alias num this; T dot(const Vector rhs) { T res = 0; static foreach (i; iota(D)) res += num[i] * rhs.num[i]; return res; } static if (D == 3) { Vector cross(const Vector rhs) { return Vector([ num[0] * rhs.num[1] - num[1] * rhs.num[0], num[1] * rhs.num[2] - num[2] * rhs.num[1], num[2] * rhs.num[0] - num[0] * rhs.num[2] ]); } } } void main() { auto v0 = Vector!3([ 1, 2, 3 ]); auto v1 = Vector!3([ 4, 5, 6 ]); writeln(v0.cross(v1)); writeln(v0.dot(v1)); } メタプログラミング にっょぃ ↓こんな感じで呼べる
UFCS最強 struct Vector(size_t D, T = double) if (__traits(isFloating, T))
{ T[D] num; alias num this; } T dot(size_t D, T = double) (const Vector!(D, T) lhs, const Vector!(D, T) rhs) { T res = 0; static foreach (i; iota(D)) res += lhs.num[i] * rhs.num[i]; return res; } Vector!(D, T) cross(size_t D, T = double) (const Vector!(D, T) lhs, const Vector!(D, T) rhs) if (D == 3) { return Vector!(D, T)([ lhs.num[0] * rhs.num[1] - lhs.num[1] * rhs.num[0], lhs.num[1] * rhs.num[2] - lhs.num[2] * rhs.num[1], lhs.num[2] * rhs.num[0] - lhs.num[0] * rhs.num[2] ]); } Uniform Function Call Syntax void main() { auto v0 = Vector!3([ 1, 2, 3 ]); auto v1 = Vector!3([ 4, 5, 6 ]); writeln(v0.cross(v1)); writeln(v0.dot(v1)); } ↓同じくこんな感じで呼べる
UFCS最強 import std.stdio; string repeat(string s, size_t times) { char[]
res; for (size_t i = 0; i < times; i++) res ~= s; return cast(string) res; } void main() { "Hello world\n".repeat(3).write(); } 要はこんなことができる
CTFE最強 Compile Time Function Execution コンパイル時にインタプリタで実行してその計算結果 をバイナリに埋め込む 割と任意の処理がコンパイル時に実行できる
CTFE最強 Compile Time Function Execution 標準ライブラリには正規表現の文字列をコンパイル 時に解析するヤツとかある 私はこの機能を使ってコンパイル時 brainfuckインタ プリタを書きました
CTFE最強 Compile Time Function Execution この機能を掘り下げるとちょっと面白い 1. D言語をバイトコードにコンパイル 2. バイトコードをインタプリタが実行
3. 実行結果をコンパイラがバイナリに埋め込む ...という風に実現されているようです
微妙だと思う点もある あくまで私の主観ですが ...
それ言語レベルで実現しちゃったのか - unittest - version - debug - foreach_reverse
それ言語レベルで実現しちゃったのか - unittest - version - debug - foreach_reverse int
multiply(int a, int b) { return a * b; } unittest { assert(multiply(2, 3) == 6); assert(multiply(7, 9) == 63); } Cでライブラリ毎にテスト方法がバラバラだった のをどうにかしたかったらしい
それ言語レベルで実現しちゃったのか - unittest - version - debug - foreach_reverse version
(Windows) { // Windows用のコード } else { // それ以外 } Cのマクロをどうにかしたかったらしい
それ言語レベルで実現しちゃったのか - unittest - version - debug - foreach_reverse debug
{ // デバッグ用コード } これもCのマクロをどうにかしたかったらしい
それ言語レベルで実現しちゃったのか - unittest - version - debug - foreach_reverse int[]
arr = [ 1, 2, 3, 4, 5 ]; foreach_reverse (i, e; arr) { // 逆向きに反復 } 「順方向」と「逆方向」を相補的なモノとして捉えたようだ
言語キメラ - Javaのclassに - C++のtemplateを合体させ - Cのマクロを分解して取り込んだ こんな感じの言語 決して悪いわけじゃないけど2022年基準で見ると微妙な気がする
質疑応答とか 一通り Origins of the D Programming Language を読み込んだので D言語の歴史とかも触りだけ話せます
参考 - Origins of the D Programming Language https://dl.acm.org/doi/pdf/10.1145/3386323 -
Dlang Tour https://tour.dlang.org