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
290
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
200
Other Decks in Technology
See All in Technology
Tech Blogを書きやすい環境づくり
lycorptech_jp
PRO
0
120
飲食店予約台帳を支えるインタラクティブ UI 設計と実装
siropaca
6
1.4k
Datadogとともにオブザーバビリティを布教しよう
mego2221
0
130
Kubernetes x k6 で負荷試験基盤を開発して 負荷試験を民主化した話 / Kubernetes x k6
sansan_randd
2
730
開発者が自律的に AWS Security Hub findings に 対応する仕組みと AWS re:Invent 2024 登壇体験談 / Developers autonomously report AWS Security Hub findings Corresponding mechanism and AWS re:Invent 2024 presentation experience
kaminashi
0
190
Data-centric AI入門第6章:Data-centric AIの実践例
x_ttyszk
1
370
組織貢献をするフリーランスエンジニアという生き方
n_takehata
1
1k
ハッキングの世界に迫る~攻撃者の思考で考えるセキュリティ~
nomizone
12
4.5k
君も受託系GISエンジニアにならないか
sudataka
1
370
PL900試験から学ぶ Power Platform 基礎知識講座
kumikeyy
0
110
現場で役立つAPIデザイン
nagix
29
10k
Ask! NIKKEI RAG検索技術の深層
hotchpotch
13
2.8k
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Code Review Best Practice
trishagee
66
17k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
99
18k
Optimizing for Happiness
mojombo
376
70k
GraphQLとの向き合い方2022年版
quramy
44
13k
Navigating Team Friction
lara
183
15k
Thoughts on Productivity
jonyablonski
69
4.5k
Side Projects
sachag
452
42k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Gamification - CAS2011
davidbonilla
80
5.1k
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