closures and methodmissing are real

42
Closures and methodMissing are real G* ワークショップ Z 札幌 2014 2014/09/27

Upload: takahiro-sugiura

Post on 28-Nov-2014

400 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Closures and methodMissing are real

Closures and methodMissing

are realG* ワークショップ Z 札幌 2014

2014/09/27

Page 2: Closures and methodMissing are real

日本語で言うと

Page 3: Closures and methodMissing are real

クロージャも、methodMissingも、あるんだよG* ワークショップ Z 札幌 2014

2014/09/27

Page 4: Closures and methodMissing are real

背景を付けると、

Page 5: Closures and methodMissing are real

クロージャも、methodMissingも、あるんだよG* ワークショップ Z 札幌 2014

2014/09/27

Page 6: Closures and methodMissing are real

お前誰よ

• 名前:杉浦孝博 • twitter : @touchez_du_bois • 夜のお仕事: 深夜アニメ普及活動 提督業(艦隊これくしょん)

Page 7: Closures and methodMissing are real

It's not important,

but... (余談ですが)

Page 8: Closures and methodMissing are real

Do you know the DSL?

(DSLをご存じですか?)

Page 9: Closures and methodMissing are real

DSL• Domain Specific Languageの略。 • 特定の領域(ドメイン)の問題を解決するための言語。 • 専用のパーサーを作る「外部DSL」と、プログラミング言語を利用する「内部DSL」、といった分け方ができる。

Page 10: Closures and methodMissing are real

内部DSLの例• GrailsでSpring Beanの登録。

// grails-app/conf/spring/resources.groovy !import my.company.MyBeanImpl !beans = { myBean(MyBeanImpl) { someProperty = 42 otherProperty = "blue" } }

Page 11: Closures and methodMissing are real

GroovyでDSL• Builder • Category • Operator overloading • Command chains • Closure • Meta programming • AST Transformer ...

Page 12: Closures and methodMissing are real

Let’s make a DSL!

(DSLを作ってみよう!)

Page 13: Closures and methodMissing are real

お題• VoiceText Web API (β版) • https://cloud.voicetext.jp/webapi • 音声合成VoiceTextを簡単に使うことができるWeb API • このWeb APIを叩くためのDSLを作ってみる。

Page 14: Closures and methodMissing are real

お題• こんな感じのAPI

curl "https://api.voicetext.jp/v1/tts" \ -o "test.wav" \ -u "YOUR_API_KEY:" \ -d "text=おはようございます" \ -d "speaker=hikari"

Page 15: Closures and methodMissing are real

お題• パラメータ

text合成するテキスト。エンコーディングはUTF-8。Unicodeで200文字以内。

speaker 話者名

emotion 感情カテゴリ

emotion_level 感情レベル

pitch 音の高低

speed 話す速度

volume 音量

Page 16: Closures and methodMissing are real

VoiceText4J• @makingさん作のJava版ライブラリ • https://github.com/making/voicetext4j/ • 大いに参考にさせていただきました。

Page 17: Closures and methodMissing are real

VoiceText4J

import am.ik.voicetext4j.*; !System.setProperty("voicetext.apikey", “API_KEY"); !EmotionalSpeaker.HIKARI.ready() .pitch(105) .speed(105) .speak("おはようございます");

Page 18: Closures and methodMissing are real

I made a DSL.

(DSLを作ってみた)

Page 19: Closures and methodMissing are real

こんな感じimport org.jggug.magica.voicetext4g.VoiceTextFactory import org.jggug.magica.voicetext4g.config.Speaker !def voiceText = VoiceTextFactory.create("YOUR_API_KEY") !voiceText.speak { speaker Speaker.HIKARI pitch 105 speed 105 "おはようございます" }

Page 20: Closures and methodMissing are real

Demo. (でも)

Page 21: Closures and methodMissing are real

デモのソースimport org.jggug.magica.voicetext4g.VoiceTextFactory import org.jggug.magica.voicetext4g.config.Speaker !def voiceText = VoiceTextFactory.create("YOUR_API_KEY") !voiceText.speak { speaker Speaker.SHOW "どうも、奥さん、知ってるでしょ〜、大泉洋でございます。おい、パイ食わねえか?" }

Page 22: Closures and methodMissing are real

Points (ポイント)

Page 23: Closures and methodMissing are real

クロージャを使うvoiceText .speaker(Speaker.HIKARI) .pitch(105) .speed(105) .speak("おはようございます") ! ↓ !voiceText.speak { speaker Speaker.HIKARI pitch 105 speed 105 "おはようございます" }

Page 24: Closures and methodMissing are real

メソッド呼び出しのカッコ省略voiceText.speak( { speaker(Speaker.HIKARI) pitch(105) speed(105) "おはようございます" }) ! ↓ !voiceText.speak { speaker Speaker.HIKARI pitch 105 speed 105 "おはようございます" }

Page 25: Closures and methodMissing are real

return 省略voiceText.speak { speaker Speaker.HIKARI pitch 105 speed 105 return "おはようございます" } ! ↓ !voiceText.speak { speaker Speaker.HIKARI pitch 105 speed 105 "おはようございます" }

Page 26: Closures and methodMissing are real

クロージャの戻り値def speak(Closure c) { def text = c() // クロージャの戻り値の型がStringかGString以外の場合、 // 何もしない if (!(text instanceof String || text instanceof GString)) { return } ! // Web APIを叩き、音声を再生 }

Page 27: Closures and methodMissing are real

設定用のメソッド

class VoiceText { Config config def speak(Closure c) { } }

• VoiceTextクラスに、speakerやpitch等の設定用メソッドは定義しない。 • 設定用のクラス(Config)のオブジェクトをメンバ変数に持つ。

Page 28: Closures and methodMissing are real

delegateを変更class VoiceText { Config config def speak(Closure c) { c.delegate = this def text = c() } }

Page 29: Closures and methodMissing are real

methodMissingclass VoiceText { Config config def speak(Closure c) { c.delegate = this def text = c() } def methodMissing(String name, args) { config."${name}" = args[0] } }

Page 30: Closures and methodMissing are real

To wrap up (まとめ)

Page 31: Closures and methodMissing are real

まとめ• クロージャやmethodMissingなどを使うことで、自分だけのDSL(DSLっぽいもの)が作れる。 • オレオレDSLで差を付けよう!

Page 32: Closures and methodMissing are real

Get back on topic (閑話休題)

Page 33: Closures and methodMissing are real

2014 秋アニメ オススメ

Page 34: Closures and methodMissing are real
Page 35: Closures and methodMissing are real

PSYCHO-PASS 2

Page 36: Closures and methodMissing are real
Page 37: Closures and methodMissing are real

棺のチャイカ 第2期

AVENGING BATTLE

Page 38: Closures and methodMissing are real
Page 39: Closures and methodMissing are real

selector spread WIXOSS

Page 40: Closures and methodMissing are real
Page 41: Closures and methodMissing are real

蟲師 続章

Page 42: Closures and methodMissing are real

ご清聴 ありがとう

ございました。