Partial Function 便利だなーと思うとき
-
単純に domain でかい
- まあ普通
-
内部実装と外部に見せたいシグネチャで domain が異なる場合
- json みたいな自己参照ふくむ値コンストラクタ必要なデータに対する処理でよくやる
- Partial Function ないとコードの重複が生じちゃう
- open recursion 使えば部分関数実現できるので、頑張れば重複なくせると思うけどここまでシンプルにかけない
$ scala rpnparser.scala | |
1 1 + = 2 | |
3 1 - = -2 | |
3 2 * = 6 | |
8 2 / = 0 | |
2 (2 2 +) + = 6 | |
2 2 2 + + = 6 | |
(2 2 +) 2 + = 6 | |
(2 2 +) (4 2 +) * = 24 | |
((2 2 +) (4 2 +) *) 2 / = 0 |
!SLIDE | |
implicit parameter 入門 | |
---------------- | |
!SLIDE | |
implicit parameter って? | |
---------------- | |
* 「Type Classes がどうのこうの」 | |
* 「Context Bounds がどうのこうの」 |
Partial Function 便利だなーと思うとき
単純に domain でかい
内部実装と外部に見せたいシグネチャで domain が異なる場合
// ==UserScript== | |
// @name add link to jira | |
// @description replace SI-NNNN with link to correspond issue page. | |
// @version 0.1 | |
// @match https://github.com/scala/scala/commit/* | |
// @match https://github.com/scala/scala/commits/* | |
// ==/UserScript== | |
(function() { | |
var each = Array.prototype.forEach | |
var url = 'https://issues.scala-lang.org/browse/'; |
def toDate[Result](magnet: DateMagnet[Result]): Result = magnet.convert | |
trait DateMagnet[Result] { | |
def convert(): Result | |
} | |
object DateMagnet { | |
implicit def fromInt(tuple:(Int, Int, Int)) = new DateMagnet[java.util.Calendar] { | |
def convert() = { | |
val (year, month, date) = tuple |
import scala.reflect.macros.Context | |
import scala.util.matching.Regex | |
import java.util.regex.PatternSyntaxException | |
object Macros{ | |
implicit class RegexContext(val c: StringContext) { | |
def r(): Regex = macro regexImpl | |
} |
package com.qtamaki.typeclass | |
trait Hoge[A] { | |
def age(a: A): A | |
def sage(a: A): A | |
def hoge(a: A): A | |
} | |
object Hoge { | |
implicit def IntHoge: Hoge[Int] = |
以下の考察は openjdk のコードを元にしているため、oracle だとまた違うかもしれない。
map0 でめでたく native からヒープをがさっと取ってこれた場合、色々あった末に DirectBuffer を implements した DirectByteBuffer とやらが具象クラスとなる。 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/nio/DirectByteBuffer.java DirectBuffer は自身の確保したリソースを開放する役割を持つオブジェクト Cleaner を返すメソッドを持っており、clean の処理は Unmapper に委譲される。 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/nio/ch/FileChannelImpl.java#FileChannelImpl.Unmapper Cleaner は「オブジェクトが phantom reachable になった際に後処理を行いたい場合に利用するクラス」であり、ぶっちゃけ普通は出番ない。 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/misc/Cleaner.java
ここから推測。