2014年12月24日水曜日

Patterns on Scala

Patterns on Scala
パターンズ・オン・スカーラ

Scala Advent Calendar 2014 の最終日の記事です。

さて theme を Scala の patterns としてみたのですが、十分な時間が確保できていないので、取り敢えず Scala に関する patterns を今集められるだけ以下に集めてみました。
重複していてもそのまま list してます。ただし GoF patterns をそのまま書いてあるものは外しておきました。

scala-lang.org 公式 Wiki

Scala Design Patterns

あんまり説明はないですね。

Structural patterns
  • Cake - Build components which are abstracted over their dependencies.
  • Dynamic scope - Make certain information available in a given context without explicitly passing it about.
  • Utility belt - Lightweight crosscutting resource sharing.
  • Reactive abstractions - Discussion of the relationship among Actors, Arrows, FRP, Pub-Sub, ....
  • Pimp my library - Extend existing classes with new methods
  • Duck typing done right - Utilise "duck typing" (structural typing) like a pro
  • Memoization - Memoize the result of a function
Behavioural patterns
  • Loan - Used primarily for resource disposal; equivalent to C++ RAII or C# 'using'.

論文
Type classes as objects and implicitsby Bruno C.d.S. Oliveira, Adriaan Moors, Martin Odersky
(2010)

http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf

Concept Pattern についてはこれ以外あまり出典がないです。

書籍
Scala Design Patterns: Patterns for Practical Reuse and Design
by John Hunt
(2013/12/6)


review はかなり酷評を受けていますが...。

Part II – Fundamental Patterns
  • Immutability
  • Singleton
  • Marker Trait
  • Delegation
  • Lazy Parameters
  • Partially Applied Functions
  • Trait Based Template Operation
  • Stackable Traits
  • Currying and Code Reuse
  • Cake Pattern
  • Structural Injection
  • Implicit Injection Pattern
  • Functor
  • Applicative Functor
  • Monoid Pattern
  • Monad Pattern
  • Foldable
  • Zipper
  • Lens Pattern
  • View Pattern
  • Arrow Pattern

書籍
Functional Programming Patterns in Scala and Clojure: Write Lean Programs for the JVMby Michael Bevilacqua-Linn
(2014/1/13)


まあまあの評価ですかね。

Replacing Object Oriented Patterns
  • Replacing Functional Interface
  • Replacing State Carrying Functional Interface
  • Replacing Command excerpt
  • Replacing Builder For Immutable Object
  • Replacing Iterator
  • Replacing Template Method
  • Replacing Strategy
  • Replacing Null Object
  • Replacing Decorator
  • Replacing Visitor
  • Replacing Dependency Injection

Functional Patterns
  • Tail Recursion excerpt
  • Mutual Recursion
  • Filter-Map-Reduce
  • Chain of Operations
  • Function Builder
  • Memoization
  • Lazy Sequence
  • Focused Mutability
  • Customized Control Flow
  • Domain-Specific Language

書籍
Reactive Enterprise with Actor Model: Application and Integration Patterns for Scala and Akka
by Vaughn Vernon
(2015/5/17)

これまだ売ってないですね。来年の 2015 年 5 月発売。でも reactive programing や Akka に特化しててなんか期待できそう。

Chapter 3. Performance Bent
  • Transistors Matter
  • Clock Speed Matters
  • Cores and Cache Matter
  • Scale Matters
  • Multi-Threading Is Hard
  • How Actor Model Helps
  • The Patterns
Chapter 4. Messaging with Actors
  • Message Channel
  • Message
  • Pipes and Filters
  • Message Router
  • Message Translator
  • Message Endpoint
  • Chapter 5. Messaging Channels
  • Point-to-Point Channel
  • Publish-Subscribe Channel
  • Datatype Channel
  • Invalid Message Channel
  • Dead Letter Channel
  • Guaranteed Delivery
  • Channel Adapter
  • Message Bridge
  • Message Bus
Chapter 6. Message Construction
  • Command Message
  • Document Message
  • Event Message
  • Request-Reply
  • Return Address
  • Correlation Identifier
  • Message Sequence
  • Message Expiration
  • Format Indicator
Chapter 7. Message Routing
  • Content-Based Router
  • Message Filter
  • Dynamic Router
  • Recipient List
  • Splitter
  • Aggregator
  • Resequencer
  • Composed Message Processor
  • Scatter-Gather
  • Routing Slip
  • Process Manager
  • Message Broker
Chapter 8. Message Transformation
  • Envelope Wrapper
  • Content Filter
  • Claim Check
  • Normalizer
  • Canonical Message Model (a.k.a. Canonical Data Model)
Chapter 9. Message Endpoints
  • Messaging Gateway
  • Messaging Mapper
  • Transactional Client / Actor
  • Polling Consumer
  • Event-Driven Consumer
  • Competing Consumers
  • Message Dispatcher
  • Selective Consumer
  • Durable Subscriber
  • Idempotent Receiver
  • Service Activator
Chapter 10. System Management and Infrastructure
  • Control Bus
  • Detour
  • Wire Tap
  • Message Metadata / History
  • Message Journal / Store
  • Smart Proxy
  • Test Message
  • Channel Purger

こんな感じでしょうか。
Scala に限らない一般的な patterns がちらほら。
さて全部の pattern の中身を追う時間はないので...

一番有名な pattern である Cake Pattern を改めて見てみましょう。結局?(笑) うんざりする人もいると思いますが。

歴史的には、
Odersky, Zenger (2005), "Scalable Component Abstractions" によって仕様提示され、 Scala Lounge mailing list (2007) で John Pretty によりこの名前が提案されました。

大雑把には Scala のみで dependency injection を可能とする pattern です。

ケイクパターン、ケーキパターン、どっちで読んでますか?
ケーキと読んだら、そう今日は Christmas。 Xmas cake など如何でしょうか。
それではここで Xmas cake pattern を書いた Gist をプレゼントします。例の snippet から中の名前が変わってるだけに見えるのは...その通りです (笑) これで cake に火を灯しましょう。

Loading code....

さてなんとか間に合った...のかな。

0 件のコメント:

コメントを投稿

Scala on VSCode

Scala on VSCode ===================== Docker を利用した VSCode remote development 上で Scala 開発をする方法 Links ========== ## Scala on VSCode remote...