VList: Difference between revisions

1,253 bytes added ,  9 years ago
Added Scala
(Updated D entry)
(Added Scala)
Line 770:
end /*j*/
q=space($); return q /*purify, return.*/</lang>
{{out}}
'''output'''
<pre style="overflow:scroll">4
4
Fred 4
Mike 1 3
1 Fred 4
1 Fred 3 3½ 4
number of items in Vlist: 5</pre>
 
</pre>
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
<blockquote cite="http://stackoverflow.com/questions/3107151/persistent-data-structures-in-scala">Two of Scala's 2.8 immutable data structures are vectors and hash tries, represented as 32-ary trees. These were originally designed by '''Phil Bagwell''', who was working with my team at EPFL, then adopted for Clojure, and now finally adopted for Scala 2.8. The Scala implementation shares a common root with the Clojure implementation, but is certainly not a port of it.</blockquote>
A quote of Martin Odersky, his co-worker Phil Bagwell† invented the VList.
<lang Scala>object VList extends App {
 
val emptyVlist1 = Vector.empty[Int]
val emptyVlist2 = Vector[Int]()
val emptyVlist3 = Vector()
 
val addedVlist1 = Vector(1, 2) :+ 6 :+ 10 :+ 12 :+ 42
 
assert((addedVlist1, addedVlist1.head) == (Vector(1, 2, 6, 10, 12, 42), 1), "Header or VList not OK")
 
val addedVlist2 = addedVlist1.head +: addedVlist1.tail.drop(1)
assert((addedVlist2, addedVlist2.head) == (Vector(1, 6, 10, 12, 42), 1), "First CDR not deleted.")
 
assert(addedVlist1.size == 6)
 
assert(addedVlist1(3) == 10, "Wrong element accesed.")
println("Successfully completed without errors.")
}</lang>
Anonymous user