Clojureコレクションで探るimmutableでpersistentな世界

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Kent OHASHI
September 30, 2022

 Clojureコレクションで探るimmutableでpersistentな世界

関数型言語Clojureを通して不変で永続的なコレクションに触れてみよう!

Kent OHASHI

September 30, 2022

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

27 slides
  1. lagénorhynque 🐬 カマイルカ (defprofile lagénorhynque :id @lagenorhynque :readings ["/laʒenɔʁɛ̃ k/"

    "ラジェノランク"] :aliases ["カマイルカ" " 🐬 "] :languages [Java Japanese ; native languages Clojure Haskell ; functional languages English français] ; European languages :interests [programming language-learning law politics mathematics])
  2. 1. 改めて関数型プログラミング( 言語) とは? 2. Java とClojure のコレクションを調べてみよう 3. Clojure

    コレクションの全体像 4. 局所的にミュータビリティがほしいとき 5. 独自コレクションを実装しよう
  3. Java オブジェクト指向・非関数型言語 静的型付き言語 JVM 言語 関数型プログラミング関連機能が充実してきた // メソッドの定義 jshell> void

    hello(Object x) { ...> System.out.println("Hello, %s!".formatted(x)); ...> } | created method hello(Object) // メソッドの呼び出し jshell> hello("Java") Hello, Java!
  4. Clojure 非オブジェクト指向・関数型言語 動的型付き言語 JVM 言語 オブジェクト指向を嫌い関数型を志向したLisp ;; 関数の定義 user=> (defn

    hello [x] (println (str "Hello, " x "!"))) #'user/hello ;; 関数の呼び出し(適用) user=> (hello "Clojure") Hello, Clojure! nil
  5. 参考 : Scala オブジェクト指向・関数型言語 静的型付き言語 JVM 言語 オブジェクト指向に関数型が溶け込んだ言語 // メソッドの定義

    scala> def hello(x: Any): Unit = | println(s"Hello, $x!") def hello(x: Any): Unit // メソッドの呼び出し scala> hello("Scala") Hello, Scala!
  6. [Java] の実装のひとつ mutable でephemeral な可変長配列 java.util.ArrayList user=> (def xs (java.util.ArrayList.))

    #'user/xs user=> xs [] user=> (def ys ; ysに束縛 #_=> (doto xs #_=> (.add 1) ; 破壊的に要素追加 #_=> (.add 2) #_=> (.add 3))) #'user/ys user=> ys [1 2 3] user=> (identical? xs ys) ; オブジェクトの同一性を確認 true java.util.List
  7. [Java] unmodifiable (view) list user=> (def xs #_=> (doto (java.util.ArrayList.)

    #_=> (.add 1) #_=> (.add 2) #_=> (.add 3))) #'user/xs user=> xs [1 2 3] user=> (def ys (java.util.Collections/unmodifiableList xs)) ; 変 #'user/ys user=> (.add ys 4) ; 破壊的更新操作はできない Execution error (UnsupportedOperationException) at java.util.Col modifiableCollection/add (Collections.java:1067). null
  8. コレクション( ここではList) に対する変更不可能な ビュー もとのコレクションやその要素がmutable なら immutable ではない もとのコレクションが( 実質的に)

    immutable であ る、もしくはこのビューを介してのみ操作できる 状態であれば実質的にimmutable cf. user=> (.add xs 4) ; もとのリストは破壊的更新操作ができる true user=> xs [1 2 3 4] user=> ys ; 変更不可能なビューにも波及する [1 2 3 4] java.util.Collection
  9. [Clojure] の実装 immutable でpersistent な可変長配列 の一種 ベクター (vector) user=> (def

    xs []) ; 初期化 #'user/xs user=> xs [] user=> (def ys (conj xs 1 2 3)) ; 要素追加してysに束縛(破壊的更新操作 #'user/ys user=> ys [1 2 3] user=> (identical? xs ys) ; オブジェクトの同一性を確認 false clojure.lang.IPersistentVector HAMT (hash array mapped trie)
  10. 参考 : Scala 不変コレクションのトレイト Traversable Iterable Seq Set Map IndexedSeq

    LinearSeq SortedSet SortedMap BitSet ref. 可変コレクションおよび不変コレクション | Collections | Scala Documentation
  11. Clojure Clojure Clojure - Data Structures Clojure - Sequences Clojure

    - Transient Data Structures Clojure - Datatypes: deftype, defrecord and reify Clojure Applied: From Practice to Practitioner Clojure Performance Guarantees [Stefan Tilkov’s Blog] clojure/core.rrb-vector: RRB-Trees in Clojure
  12. Scala Java Scala 可変コレクションおよび不変コレクション | Collections | Scala Documentation 性能特性

    | Collections | Scala Documentation Java SE 17 & JDK 17 The Collections Framework (Java SE 17 & JDK 17)
  13. Kotlin JavaScript Python Kotlin/kotlinx.collections.immutable: Immutable persistent collections for Kotlin immutable-js/immutable-js:

    Immutable persistent data collections for Javascript which increase efficiency and simplicity. tobgu/pyrsistent: Persistent/Immutable/Functional data structures for Python

More Decks by Kent OHASHI

Other Decks in Programming