by auggierose · 13y ago · 0 comments
I am currently porting a Clojure library of mine to Scala, and it is about 10% less code, and obviously more robust because of static type checking.
Scala.js: https://github.com/lampepfl/scala-js
(defn completion-items [grammar doc items-bin-index completed-bin-index items nonterminal value]
(loop [items items
completed '()]
(if-let [[^Item item previous-value] (first items)]
(let [alpha (expected-symbol grammar item)
dot (.dot item)]
(if (and alpha (= nonterminal alpha))
(let [rule-nonterminal (.nonterminal item)
ruleindex (.ruleindex item)
origin (.origin item)
value' (gen-nextvalue grammar doc origin items-bin-index completed-bin-index
rule-nonterminal ruleindex dot
previous-value value)]
(if (nil? value')
(recur (next items) completed)
(let [item' (->Item rule-nonterminal ruleindex (+ dot 1) origin)]
(recur (next items) (cons-item-value completed item' value')))))
(recur (next items) completed)))
(apply hash-map completed))))
--------------------------------------------------------
def complete_items(completed_binindex : Int, bin : ItemBin, nonterminal : Nonterminal, value : Value) : MoreItems = {
var completed_items : List[(Item, IntermediateValue)] = List()
for ((item, v) <- bin.items) {
val s = expectedSymbol(item)
if (s == Some (nonterminal)) {
grammar.nextValue(document, item.origin, bin.binindex, completed_binindex, nonterminal,
item.ruleindex, item.dot, v, value) match
{
case None =>
case Some(nextvalue) =>
completed_items = (item.inc -> nextvalue) ::
completed_items
}
}
}
completed_items
}1) Unnecessary type hint
2) No use of destructuring or field lookup by keyword
3) Alteration of record objects with positional constructors instead of update-in
4) Explicit loop with previous item instead of reduce over (partition 2 1 items)
5) Way too many parameters
6) Apply hash-map instead of into
7) (+ ... 1) instead of inc
8) Weird gen-nextvalue auxiliary function instead of a lazy sequence
I could keep going...
By the way, gen-nextvalue is a protocol function.
idiomatic == I am doing what I am told to do without understanding why it sometimes makes sense, and sometimes doesn't, and sometimes just doesn't matter.
def complete_items(...) = bin.items.flatMap((item, v) =>
expectedSymbol(item).map(nonterminal =>
grammar.nextValue(document, item.origin, bin.binindex, completed_binindex, nonterminal, item.ruleindex, item.dot, v, value)
.map(nextvalue => (item.inc -> nextvalue)
)
)Programming in Clojure I miss the following features from Scala most:
a) Pattern matching
b) Having machine-checked documentation (aka type system), traits.
c) A proper documentation generation tool. The Clojure one doesn't do protocols properly, although protocols are the only real way in Clojure to have something akin' to interfaces and Scala traits.
a) https://github.com/clojure/core.match
b1) https://github.com/clojure/core.typed
b2) I agree that Clojure's facilities for mixins is under-utilized and that it lacks a proper facility for delegation (ie implicit conversions in Scala), but see (doc extend) for how awesome "traits as data" can be. Here's an example: https://github.com/stuartsierra/clojure.walk2/blob/2250e04c7...
c1) I hate generated documentation, but I understand why it's necessary for Scala. When I did some scala programming, I was so pleased with ScalaDoc, since it really helps you navigate that complex scala.collections hierarchy. Feels like having a pretty decent substitute to a good IDE. In Clojure, I don't feel the need nearly as much... Also, I quite like reflective documentation with the doc macro.
c2) Already covered interfaces/traits in b2
In short: more robust
$ cat NotTypesafe.scala
object NotTypesafe {
def main(args: Array[String]) {
println("5".asInstanceOf[Int])
}
}
$ scalac NotTypesafe.scala
$ scala NotTypesafe
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
http://en.wikipedia.org/wiki/Type_safety