SlideShare une entreprise Scribd logo
1  sur  59
Télécharger pour lire hors ligne
入門Transducers
Clojure 1.7勉強会
@athos0220
Transducerとは?
Transducers are composable algorithmic transformations.
They are independent from the context of their input and
output sources and specify only the essence of the
transformation in terms of an individual element. Because
transducers are decoupled from input or output sources,
they can be used in many different processes - collections,
streams, channels, observables, etc.
http://clojure.org/transducers
なるほど分からん
つまり、、、
Transducerは合成可能なアルゴリズム的変換
入力源や出力先に依らず、各要素を変換する
…やっぱり分からん
Transducerが生まれた
経緯を知ると理解が深まる
Transducerが生まれた
経緯を知ると理解が深まる
…かも?
おさらい:シーケンス処理
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
各要素に1を足し
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
各要素に1を足し
奇数以外を取り除き
(->> (range 5)
(map inc)
(filter odd?)
(reduce + 0))
各要素に1を足し
奇数以外を取り除き
足し合わせる
0 1 2 3 4(range 5)
0 1 2 3 4
51 2 3 4
(range 5)
(map inc)
inc inc inc inc inc
0 1 2 3 4
51 2 3 4
51 3
(range 5)
(map inc)
(filter odd?)
inc inc inc inc inc
odd? odd? odd? odd? odd?
0 1 2 3 4
51 2 3 4
51 3
(range 5)
(map inc)
(filter odd?)
9
inc inc inc inc inc
odd? odd? odd? odd? odd?
(reduce + 0)
++ +
おさらい:Reducers
(->> (range 5)
(r/map inc)
(r/filter odd?)
(reduce + 0))
(range 5) 0 1 2 3 4
(range 5)
(r/map inc) 0
要素に1足して処理
1 2 3 4
0 1 2 3 4
Reducible
(range 5)
(r/map inc)
(r/filter odd?)
0
要素に1足して処理
1 2 3 4
0 1 2 3 4
0
要素に1足して処理
奇数ならば処理
1 2 3 4
Reducible
Reducible
(range 5)
(r/map inc)
(r/filter odd?)
(reduce + 0)
0
要素に1足して処理
1 2 3 4
0 1 2 3 4
9
0
要素に1足して処理
奇数ならば処理
1 2 3 4
要素に1足して処理
奇数ならば処理
+
Reducible
Reducible
おさらい:Reducers
Reducerでは、シーケンス処理と異なり、各
ステップで要素毎の中間データを作らない
reduceする段階で各要素に処理を適用する
(本来はさらにfork/joinを使った並列処理に発展するけどここでは割愛)
で、     って
具体的に何?
要素に1足して処理
(reduce + 0 (map inc s))
(reduce + 0 (map inc s))
(reduce + 0 (map inc s))
(reduce + 0 (map inc s))
「各要素を1足して、
先頭から順に要素を足し合わせる」
(reduce + 0 (map inc s))
「各要素を1足して、
先頭から順に要素を足し合わせる」
(reduce (fn [a x] (+ a (inc x))) 0 s)
「先頭から順に各要素に1足した値を足し合わせる」
これは以下と等価
(reduce (fn [a x] (+ a (inc x))) 0 s)
(reduce (fn [a x] (+ a (inc x))) 0 s)(fn [a x] (+ a (inc x)))
これが      の正体要素に1足して処理
(fn [a x] (+ a (inc x)))
同じように
(reduce + 0 (filter odd? s))
(reduce + 0 (filter odd? s))
(reduce + 0 (filter odd? s))
(reduce + 0 (filter odd? s))
「奇数以外の要素を取り除き、
先頭から順に要素を足し合わせる」
(reduce + 0 (filter odd? s))
「奇数以外の要素を取り除き、
先頭から順に要素を足し合わせる」
(reduce (fn [a x]
(if (odd? x) (+ a x) a))
0 s)
「先頭から順に奇数の要素を足し合わせる」
これは以下と等価
(reduce (fn [a x]
(if (odd? x) (+ a x) a))
0 s)
(reduce (fn [a x]
(if (odd? x) (+ a x) a))
0 s)
(fn [a x]
(if (odd? x) (+ a x) a))
これが      の正体奇数ならば処理
(fn [a x]
(if (odd? x) (+ a x) a))
一般的に
(reduce op z (map f s))
(reduce (fn [a x] (op a (f x))) z s)
=
一般的に
(reduce op z (filter f s))
(reduce (fn [a x] (if (f x) (op a x) a)
z s)
=
これらは各シーケンス処理の
本質的な部分を表すものとして
標準の関数から取得できるようになった
(map f) = (fn [op]
(fn [a x]
(op a (f x)))
(filter f) = (fn [op]
(fn [a x]
(if (f x) (op a x) a)))
これらは各シーケンス処理の
本質的な部分を表すものとして
標準の関数から取得できるようになった
(map f) = (fn [op]
(fn [a x]
(op a (f x)))
(filter f) = (fn [op]
(fn [a x]
(if (f x) (op a x) a)))
↑これがTransducer!!
= (map inc)要素に1足して処理
奇数ならば処理
= (filter odd?)
Transducerの実体はただの関数なので
関数合成により合成できる
要素に1足して処理
奇数ならば処理
+
要素に1足して処理
奇数ならば処理
(comp (map inc)
(filter odd?))
合成したものもTransducer
0
要素に1足して処理
奇数ならば処理
1 2 3 4
Reducible
0
要素に1足して処理
奇数ならば処理
1 2 3 4
Reducible
Reducible = Transducer + コレクション
0
要素に1足して処理
奇数ならば処理
1 2 3 4
Reducible
Reducible = Transducer + コレクション
逆に、Transducerとは
Reducibleからコレクションへの依存を除き
汎用的に使えるようにしたものといえる
さまざまなTransducer
多くの標準シーケンス処理関数が1.7以降Transducer
を返すようになる
map
mapcat
filter
remove
take
take-while
drop
drop-while
take-nth
replace
partition-by
partition-all
keep
keep-indexed
map-indexed
distinct
interpose
Transducerを使うときの注意点
関数合成の順序が通常と逆になる
内部状態をもつTransducerがあるため、
Transducerは使い回さない
Transducerを引数にとるAPI
(transduce xf f init s)
= (reduce (xf f) init s)
(into to xf from)
コレクションfromをtransducerで変換しながらコレ
クションtoに変換
(eduction xf coll)
transducerとコレクションからReducibleを生成
core.asyncでの利用
(def c (async/chan
(comp (map inc)
(filter odd?))))
(async/onto-chan c (range 5))
(async/<!! c) ;=> 1
(async/<!! c) ;=> 3
(async/<!! c) ;=> 5
対象のデータ構造に依らないためチャネルからも使える
まとめ
Transducerは対象とするデータ構造に依らな
いアルゴリズム的変換を記述する標準的な方法
中間データを生成しないため、従来のシーケン
ス処理よりも高速化できる可能性も

Contenu connexe

Tendances

冬のLock free祭り safe
冬のLock free祭り safe冬のLock free祭り safe
冬のLock free祭り safe
Kumazaki Hiroki
 

Tendances (20)

入門ClojureScript
入門ClojureScript入門ClojureScript
入門ClojureScript
 
HashMapとは?
HashMapとは?HashMapとは?
HashMapとは?
 
ホモトピー型理論入門
ホモトピー型理論入門ホモトピー型理論入門
ホモトピー型理論入門
 
圏とHaskellの型
圏とHaskellの型圏とHaskellの型
圏とHaskellの型
 
イベント駆動プログラミングとI/O多重化
イベント駆動プログラミングとI/O多重化イベント駆動プログラミングとI/O多重化
イベント駆動プログラミングとI/O多重化
 
Hack言語に賭けたチームの話
Hack言語に賭けたチームの話Hack言語に賭けたチームの話
Hack言語に賭けたチームの話
 
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭するCEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ
 
Tackling Complexity
Tackling ComplexityTackling Complexity
Tackling Complexity
 
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Domain Driven Design with the F# type System -- F#unctional Londoners 2014Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
 
すごい配列楽しく学ぼう
すごい配列楽しく学ぼうすごい配列楽しく学ぼう
すごい配列楽しく学ぼう
 
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くかDDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
 
Scalaで型クラス入門
Scalaで型クラス入門Scalaで型クラス入門
Scalaで型クラス入門
 
冬のLock free祭り safe
冬のLock free祭り safe冬のLock free祭り safe
冬のLock free祭り safe
 
GoF デザインパターン 2009
GoF デザインパターン 2009GoF デザインパターン 2009
GoF デザインパターン 2009
 
実践的な設計って、なんだろう?
実践的な設計って、なんだろう?実践的な設計って、なんだろう?
実践的な設計って、なんだろう?
 
関数プログラミング入門
関数プログラミング入門関数プログラミング入門
関数プログラミング入門
 
SolrとElasticsearchを比べてみよう
SolrとElasticsearchを比べてみようSolrとElasticsearchを比べてみよう
SolrとElasticsearchを比べてみよう
 
SECDマシン 実装と動きとその他もろもろについて
SECDマシン 実装と動きとその他もろもろについてSECDマシン 実装と動きとその他もろもろについて
SECDマシン 実装と動きとその他もろもろについて
 
Rpn and forth 超入門
Rpn and forth 超入門Rpn and forth 超入門
Rpn and forth 超入門
 

En vedette

Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なものClojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
sohta
 
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
Yoshitaka Kawashima
 
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
DataValueTalk
 
Conjugate the following verbs in the present tense
Conjugate the following verbs in the present tenseConjugate the following verbs in the present tense
Conjugate the following verbs in the present tense
ChiChiu Lam
 

En vedette (20)

Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on Heroku
 
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なものClojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
 
『はじめてのClojure』勉強会#3 第7章:テスト、テスト、テスト
『はじめてのClojure』勉強会#3 第7章:テスト、テスト、テスト『はじめてのClojure』勉強会#3 第7章:テスト、テスト、テスト
『はじめてのClojure』勉強会#3 第7章:テスト、テスト、テスト
 
Clojure入門
Clojure入門Clojure入門
Clojure入門
 
ClojureでElectronアプリを作ろう
ClojureでElectronアプリを作ろうClojureでElectronアプリを作ろう
ClojureでElectronアプリを作ろう
 
Clojure Language Update (2015)
Clojure Language Update (2015)Clojure Language Update (2015)
Clojure Language Update (2015)
 
docxをmdで書こう
docxをmdで書こうdocxをmdで書こう
docxをmdで書こう
 
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
渋谷JVM#1 Immutable時代のプログラミング言語 Clojure
 
プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例
 
From Java To Clojure
From Java To ClojureFrom Java To Clojure
From Java To Clojure
 
Guj
GujGuj
Guj
 
Semiotika: Nastroj pro lepsi uzivatelsky prozitek?
Semiotika: Nastroj pro lepsi uzivatelsky prozitek?Semiotika: Nastroj pro lepsi uzivatelsky prozitek?
Semiotika: Nastroj pro lepsi uzivatelsky prozitek?
 
Selling UX with Daniel Szuc
Selling UX with Daniel SzucSelling UX with Daniel Szuc
Selling UX with Daniel Szuc
 
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
Jan Wilhelm Koelnmesse - Address Quality as a pre-requisite for centralised i...
 
How to get ready for the fast future - #CCHUC15
How to get ready for the fast future - #CCHUC15How to get ready for the fast future - #CCHUC15
How to get ready for the fast future - #CCHUC15
 
Homenagem Aos Quartistas
Homenagem Aos QuartistasHomenagem Aos Quartistas
Homenagem Aos Quartistas
 
Monosílabos, interrogativos y exclamativos
Monosílabos, interrogativos y exclamativosMonosílabos, interrogativos y exclamativos
Monosílabos, interrogativos y exclamativos
 
【第17回八子クラウド座談会 LT】CloudConductor+VDCのご紹介
【第17回八子クラウド座談会 LT】CloudConductor+VDCのご紹介【第17回八子クラウド座談会 LT】CloudConductor+VDCのご紹介
【第17回八子クラウド座談会 LT】CloudConductor+VDCのご紹介
 
40 proven social media marketing tips for seafood industry
40 proven social media marketing tips for seafood industry40 proven social media marketing tips for seafood industry
40 proven social media marketing tips for seafood industry
 
Conjugate the following verbs in the present tense
Conjugate the following verbs in the present tenseConjugate the following verbs in the present tense
Conjugate the following verbs in the present tense
 

Similaire à 入門Transducers (6)

モナドハンズオン前座
モナドハンズオン前座モナドハンズオン前座
モナドハンズオン前座
 
たのしい関数型
たのしい関数型たのしい関数型
たのしい関数型
 
Tapl 5
Tapl 5Tapl 5
Tapl 5
 
Implicit Explicit Scala
Implicit Explicit ScalaImplicit Explicit Scala
Implicit Explicit Scala
 
Incanterの紹介
Incanterの紹介Incanterの紹介
Incanterの紹介
 
Material
MaterialMaterial
Material
 

入門Transducers