I conclude that there are two ways of constructing a software design: One way
is to make it so simple that there are obviously no deficiencies and the
other way is to make it so complicated that there are no obvious
deficiencies.
/** * This function should return the first position of character `c` in the * 2 dimensional vector `v`. */ deffindChar(c: Char, v: Vector[Vector[Char]]): Option[(Int, Int)] = ???
看起來真夠簡單,但想不出怎麼回傳第二層的 indexOf 值。第一版是這樣寫的:
1 2 3 4 5 6 7 8 9 10 11
deffindChar(c: Char, v: Vector[Vector[Char]]): Option[(Int, Int)] = { var y = -1 val x = v.indexWhere(vv => { y = vv.indexOf(c) y > -1 }) if (x > -1) Some((x, y)) else None }
這樣是不差啦,但就是多了個 var y。自己硬寫的結果是這樣:
1 2 3 4 5 6 7
deffindChar(c: Char, v: Vector[Vector[Char]]): Option[(Int, Int)] = { val x = v.indexWhere(_.indexOf(c) > -1) if (x > -1) Some((x, v(x).indexOf(c))) else None }
用了兩次 indexOf 實在有夠難看。最後忍不住 Google:
1 2 3 4 5 6 7
deffindChar(c: Char, v: Vector[Vector[Char]]): Option[(Int, Int)] = { val r = v.map(_ indexOf c).zipWithIndex.find(_._1 > -1) r match { caseSome((x, y)) => Some((y, x)) caseNone => None } }
jsonrpc can be used with HTTP by writing a suitable codec. Given
that there is no standard definition of jsonrpc over http, I think
that’s the best we can do.
type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 00:00:00 UTC. sec int64
// nsec specifies a non-negative nanosecond // offset within the second named by Seconds. // It must be in the range [0, 999999999]. nsec int32
// loc specifies the Location that should be used to // determine the minute, hour, month, day, and year // that correspond to this Time. // Only the zero Time has a nil Location. // In that case it is interpreted to mean UTC. loc *Location }