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
わかる!Java to Clojure
Search
Shunsuke Tadokoro
October 05, 2017
Technology
6
1.7k
わかる!Java to Clojure
2017.10.4「教養としてのClojure」の発表資料です
Shunsuke Tadokoro
October 05, 2017
Tweet
Share
More Decks by Shunsuke Tadokoro
See All by Shunsuke Tadokoro
ある日「Webエンジニアなら、Webサーバーは作れますよね」と言われたら? ~ 3つのJVM言語で作って学ぶ
todokr
2
470
Onboarding Process for Scala Team
todokr
0
1.5k
Running Scala on AWS Lambda in a Snappy Way
todokr
7
4.1k
Scalaでつくる ちょっとしたCLIツール
todokr
0
840
新しいプログラミング言語の学び方 HTTPサーバーを作って学ぶ Java, Scala, Clojure
todokr
28
18k
Scala絵文字ライブラリに Macroなどを導入してみた
todokr
0
1.2k
Clojureに入門してHTTPサーバーをつくってみた話
todokr
3
1k
emojiconに行ってきました
todokr
2
680
Scalaリファクタリング入門「大改造!静的ビフォー・アフター」
todokr
3
1.2k
Other Decks in Technology
See All in Technology
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
520
Password-less Journey - パスキーへの移行を見据えたユーザーの準備 @ AXIES 2024
ritou
3
1.4k
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
120
2024年にチャレンジしたことを振り返るぞ
mitchan
0
130
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
250
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
250
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
140
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.1k
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
1
110
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
OpenShift Virtualizationのネットワーク構成を真剣に考えてみた/OpenShift Virtualization's Network Configuration
tnk4on
0
130
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
RailsConf 2023
tenderlove
29
940
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
4 Signs Your Business is Dying
shpigford
181
21k
Typedesign – Prime Four
hannesfritz
40
2.4k
Code Review Best Practice
trishagee
65
17k
Site-Speed That Sticks
csswizardry
2
190
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
We Have a Design System, Now What?
morganepeng
51
7.3k
Agile that works and the tools we love
rasmusluckow
328
21k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Transcript
Θ͔Δʂ+BWBUP$MPKVSF ڧཁڭཆͱͯ͠ͷ$MPKVSF EDMK !UPEPLS ָ͘͠
୭ͩ ాॴॣ༎!UPEPLS ීஈ4DBMBΤϯδχΞ ϓϥΠϕʔτͰ$MPKVSF$MPKVSF4DSJQUΛͪΐͪ͜ΐ͜৮Δॳ৺ऀ
None
͓͢͠Δ͜ͱ w 8IZ-JTQ 8IZ$MPKVSF w جຊతͳจ๏ʹ͍ͭͯ w +BWBͷΞϨΛ$MPKVSFͰॻ͍ͯΈΑ͏
8IZ-JTQ w େֶͰ৮͍ͬͯͨͷͰ w ࣄͷͨΊ w ʮܭࢉػϓϩάϥϜͷߏͱղऍʯΛಡΉͨΊ w &NBDTϢʔβʔͩͬͨͷͰ w
+BWB4DSJQU$ͷൽΛ͔Ϳͬͨ-JTQͩͱฉ͍ͯ w ϧϯό͕-JTQͰಈ͍͍ͯΔͱฉ͍ͯ w FUD
None
ϙʔϧɾάϨΞϜ 7JBXFCۀऀ :$PNCJOBUPSઃऀ
-JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ͏͖ͳΜͩɻ ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷʹཱͭ ϙʔϧɾάϨΞϜ 7JBXFCۀऀ :$PNCJOBUPSઃऀ
ී௨ͷౕΒͷ্Λߦ͚ɻ -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ͏͖ͳΜͩɻ ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷʹཱͭ ϙʔϧɾάϨΞϜ 7JBXFCۀऀ :$PNCJOBUPSઃऀ
ී௨ͷౕΒͷ্Λߦ͚ɻ -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ͏͖ͳΜͩɻ ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷʹཱͭ ϙʔϧɾάϨΞϜ 7JBXFCۀऀ :$PNCJOBUPSઃऀ :BIPPʹച٫
ී௨ͷౕΒͷ্Λߦ͚ɻ -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ͏͖ͳΜͩɻ ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷʹཱͭ ϙʔϧɾάϨΞϜ 7JBXFCۀऀ :$PNCJOBUPSઃऀ Θͨ͠ ී௨ͷϓϩάϥϚ -JTQͰΣϒαʔϏεΛͭ͘Δͱ :BIPPʹԯԁͰങͬͯΒ͑Δʂʂʂ
:BIPPʹച٫
ͳͥ$PNNPO-JTQ4DIFNFͰͳ͘ $MPKVSFʁ
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ϦονɾώοΩʔ
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ϦονɾώοΩʔ
ಡΈ͢͞ $MPKVSF-JTQͷ౷ΛࣺͯɺಡΈ͢͞ΛͱͬͨʢΒ͍͠ʣ ͦͷͨΊɺ$PNNPO-JTQ4DIFNFʹൺͯ ɾׅހ͕গͳ͍ ɾׅހͷछྨ͕ଟ͍
ಡΈ͢͞ ͨͱ͑ؔͷҾ $PNNPO-JTQ 4DIFNF $MPKVSF (defun square (x) (* x
x)) (define (square x) (* x x)) (defn square [x] (* x x))
ಡΈ͢͞ ͨͱ͑BTTPDJBUJWFͳσʔλߏʢ+BWBͰ͍͏.BQʣ (defparameter greet '(("ja" . "͜Μʹͪ") ("en" . "hello")
("fr" . "bonjour")) (define greet '(("ja" "͜Μʹͪ") ("en" "hello") ("fr" "bonjour")) $PNNPO-JTQ 4DIFNF (def greet {"ja" "͜Μʹͪ" "en" "hello" "fr" "bonjour"}) $MPKVSF
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ϦονɾώοΩʔ
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ϦονɾώοΩʔ
#FUUFS+BWBͰ͋Δ͜ͱ (.concat "hello" " world") ; -> "hello world" ;;
"hello".concat(" world"); (.getName String) ; -> "java.lang.String" ; String.getName (.. user getAddress getCity) ; -> "Shibuya" ; user.getAddress().getCity(); (.-x (new java.awt.Point 2 3)) ;; 2 ;; -> new java.awt.Point(2, 3).x Math/PI ;; 3.141592653589793 ;; Math.PI +BWBͷ"1*͕ͦͷ··ݺͼग़ͤΔ☕️ ˠ+BWBͰͰ͖Δ͜ͱͰ͖Δʂͱ͍͏҆৺ײ
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ϦονɾώοΩʔ
8IZ$MPKVSF w ಡΈ͢͞ w ׅހͷ͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w
ͳʹΑΓָ͍͠ʂ
ͳʹΑΓָ͍͠ʂ w ߴ֊ؔԆධՁͳͲͷؔܕϓϩάϥϛϯάελΠϧ w ঢ়ଶʹ͍ͭͯͷߟ͑ํɺ*EFOUJUZ 4UBUF 7BMVFͷ w γϯϓϧ͞ʹ͍ͭͯ ͍Ζ͍Ζܹత
IUUQCPYPGQBQFSTIBUFOBCMPHDPNFOUSZTJNQMF@NBEF@FBTZ
ͭ·Γ$MPKVSF ͱ͖͍ͬͭ͢ʂ ֶͼָ͕͍͋ͬͯ͠ʂ
+BWBUP$MPKVSFͯ͠ΈΑ͏ w )FMMPXPSME w ࢛ଇԋࢉ w มએݴ w ؔએݴ w
ແ໊ؔ
खݩͰࢼ͢ͳΒ5SZ$MPKVSF͕͓͢͢Ί w 5SZ$MPKVSF IUUQXXXUSZDMKDPN
·ͣγϯλοΫεʹ׳ΕΑ͏ʂ
-JTQͷγϯλοΫε ׅހͷ࢝·Γ͔ΒऴΘΓ·Ͱ͕୯Ґ (ԿΒ͔ͷॲཧ ॲཧͷରͳͲ…)
͏׳Ε·ͨ͠Ͷ
)FMMPXPSME System.out.println("Hello world"); +BWB $MPKVSF
)FMMPXPSME System.out.println("Hello world"); +BWB (println "Hello world") $MPKVSF
࢛ଇԋࢉ 12 + 40 10 - 1 2 * 3
5 / 2 +BWB $MPKVSF
࢛ଇԋࢉ 12 + 40 10 - 1 2 * 3
5 / 2 +BWB (+ 12 40) (- 10 1) (* 2 3) (/ 5 2) $MPKVSF
࢛ଇԋࢉ 12 + 40 10 - 1 2 * 3
5 / 2 +BWB (+ 12 40) (- 10 1) (* 2 3) (/ 5 2) ; -> 5/2 Λѻ͏Ratioܕ $MPKVSF
˞จࣈྻͷ݁߹TUS จࣈྻͷ݁߹ Ͱͳ͘TUSΛ͏ (+ "hoge" "fuga") ClassCastException java.lang.String cannot be
cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128) (str "hoge" "fuga") hogefuga
มએݴ int x = 10; +BWB $MPKVSF
มએݴ int x = 10; +BWB (def x 10) $MPKVSF
มએݴ MFUʹΑΔϩʔΧϧมఆٛʢϩʔΧϧଋറʣΛݟ͔͚Δ΄͏͕ଟ͍ (let [x 10] (+ x 3)) ; xletͷׅހ͚ͩͰࢀরͰ͖Δ
$MPKVSF
ؔએݴ public int f(int x) { return x + 1;
} +BWB $MPKVSF
ؔએݴ public int f(int x) { return x + 1;
} +BWB (defn f [x] (+ x 1)) $MPKVSF
ແ໊ؔ x -> x * 2 +BWB $MPKVSF
ແ໊ؔ x -> x * 2 +BWB (fn [x] (*
x 2)) $MPKVSF
ແ໊ؔ x -> x * 2 +BWB (fn [x] (*
x 2)) #(* % 2) ; ↑ͷ؆ܿͳه๏ $MPKVSF
ແ໊ؔΛͬͯΈΔ Arrays.asList(1, 2, 3).stream() .map(x -> x * 2) .collect(Collectors.toList());
// List(2, 4, 6) +BWB $MPKVSF
ແ໊ؔΛͬͯΈΔ Arrays.asList(1, 2, 3).stream() .map(x -> x * 2) .collect(Collectors.toList());
// List(2, 4, 6) +BWB (map (fn [x] (* x 2)) '(1 2 3)) $MPKVSF
ແ໊ؔΛͬͯΈΔ Arrays.asList(1, 2, 3).stream() .map(x -> x * 2) .collect(Collectors.toList());
// List(2, 4, 6) +BWB (map (fn [x] (* x 2)) '(1 2 3)) (map #(* % 2) '(1 2 3)) $MPKVSF
ͬͱ+BWBUP$MPKVSFͯ͠ΈΑ͏ w γʔέϯεͷจࣈྻ݁߹ w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ w *OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ
γʔέϯεͷจࣈྻ݁߹
γʔέϯεͷจࣈྻ݁߹ String[] words = { "a", "b", "c" }; StringBuilder
sb = new StringBuilder(); for(String word : words) { sb.append(word); } System.out.println(sb.toString()); // "abc" // Java8 System.out.println(String.join("", words)); +BWBͷ߹
γʔέϯεͷจࣈྻ݁߹ (def words '("a" "b" "c")) (clojure.string/join "" words) ;
͘͠ (apply str words) DMPKVSFͷ߹
γʔέϯεͷจࣈྻ݁߹ (def words '("a" "b" "c")) (clojure.string/join "" words) ;
͘͠ (apply str words) DMPKVSFͷ߹
w ՄมҾΛड͚औΔؔʹγʔέϯεΛ͍ͨ͠ͱ͖ʹ͏ w +BWB4DSJQUͷBQQMZͱҰॹ BQQMZ (str ["a" "b" "c"]) ;
-> ["a" "b" "c"] (apply str ["a" "b" "c"]) ; -> abc ; (str "a" "b" "c") ͱಉ͡
γʔέϯεͷจࣈྻ݁߹ (def words '("a" "b" "c")) (clojure.string/join "" words) ;
͘͠ (apply str words) DMPKVSFͷ߹
จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ
จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ public static boolean isBlank(final CharSequence cs) { int strLen;
if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } isBlank("hello!"); // false isBlank(null); // true isBlank(""); // true isBlank("\r\n\t") // true +BWBͷ߹
จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")
; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ߹
จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")
; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ߹
FWFSZ w ίϨΫγϣϯͷશͯͷཁૉʹରͯ͠ ड़ޠؔʢCPPMFBOΛฦؔ͢ʣΛద༻ w શͯUSVFͳΒUSVF (every? even? '(2 4
6)) ; true (every? even? '(1 2 3)) ; false
จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")
; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ߹
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ try(BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream("./test.txt"))))
{ StringBuilder sb = new StringBuilder(); String line; while((line = reader.readLine()) != null) { sb.append(line); } System.out.println(sb.toString()); } +BWBͷ߹
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println
(slurp bis))) $MPKVSFͷ߹
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println
(slurp bis))) $MPKVSFͷ߹
DMPKVSFKBWBJPJOQVUTUSFBN w ҾͰड͚औͬͨ'JMF 63* 63- 4PDLFU CZUFྻ 4USJOHʢͰදݱ͞ΕͨύεʣΛ*OQVU4USFBNʹม w มޙͷ*OQVU4USFBNͷσϑΥϧτ࣮
KBWBJP#VGGFSFE*OQVU4USFBN (require '[clojure.java.io :as io]) (io/input-stream "http://example.com")
XJUIPQFO w +BWBͰ͍͏USZXJUISFTPVSDFߏจɺ 4DBMBͰ͍͏-PBO1BUUFSOΛ࣮ݱ͢Δͷ w XJUIPQFOͰPQFOͨ͠ϦιʔεউखʹDMPTF͞ΕΔ (with-open [in (io/input-stream "./test.txt")]
(do-something in)) ;; already closed!
TMVSQ w 3FBEFS #VGGFSFE3FBEFS *OQVU4USFBN 'JMF 63* 63- 4PDLFU
CZUFྻ DIBSྻΛड͚औΓɺ จࣈྻʹͯ͠ฦ͢ w ରʹͳΔॻ͖ग़͠ͷͨΊͷؔTQJU (spit "./test.txt" "hello I/O!") (println (slurp "./test.txt")) ;; -> hello I/O!
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println
(slurp bis))) $MPKVSFͷ߹
*OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println
(slurp bis))) $MPKVSFͷ߹ ;; όοϑΝϦϯάͤͣʹϑΝΠϧ͔ΒಡΉ͚ͩͳΒˣͰOK (println (slurp "./test.txt"))
ࠓ+BWBUP$MPKVSFͨ͠ͷ w )FMMPXPSME w ࢛ଇԋࢉ w มએݴ w ؔએݴ w
ແ໊ؔ w γʔέϯεͷจࣈྻ݁߹ w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷఆ w *OQVU4USFBN͔ΒจࣈྻΛͯ͢ಡΉ
None