VList: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 16: Line 16:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 0 DEF FN CDR(X) = V(X):Q$ = CHR$ (34)
<syntaxhighlight lang="gwbasic"> 0 DEF FN CDR(X) = V(X):Q$ = CHR$ (34)
1 FOR A = 6 TO 1 STEP - 1:A$ = STR$ (A): PRINT "CONS "A$": ";: GOSUB 10: GOSUB 50: NEXT A
1 FOR A = 6 TO 1 STEP - 1:A$ = STR$ (A): PRINT "CONS "A$": ";: GOSUB 10: GOSUB 50: NEXT A
2 GOSUB 30: PRINT "LENGTH: "L
2 GOSUB 30: PRINT "LENGTH: "L
Line 43: Line 43:


REM PRINT ELEMENT WITH NEWLINE given A
REM PRINT ELEMENT WITH NEWLINE given A
90 P = A: GOSUB 70: PRINT : RETURN</lang>
90 P = A: GOSUB 70: PRINT : RETURN</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 60: Line 60:
</pre>
</pre>
=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 182: Line 182:
v_del(v);
v_del(v);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang="c">
<lang c>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
Line 350: Line 350:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 380: Line 380:


Two modules are provided - one for implementing and one for using VLists
Two modules are provided - one for implementing and one for using VLists
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE RosettaVLists;
MODULE RosettaVLists;
(** In 2002, Phil Bagwell published "Fast Functional Lists" which introduced VLists as alternatives to Functional Programming's
(** In 2002, Phil Bagwell published "Fast Functional Lists" which introduced VLists as alternatives to Functional Programming's
Line 566: Line 566:
NEW(nilBase); nilBase.NewBlock(1, '*')
NEW(nilBase); nilBase.NewBlock(1, '*')
END RosettaVLists.
END RosettaVLists.
</syntaxhighlight>
</lang>
Interface extracted from implementation:
Interface extracted from implementation:
<lang oberon2>
<syntaxhighlight lang="oberon2">
DEFINITION RosettaVLists;
DEFINITION RosettaVLists;


Line 594: Line 594:
END;
END;


END RosettaVLists.</lang>
END RosettaVLists.</syntaxhighlight>
Module that uses previous module:
Module that uses previous module:
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE RosettaVListsUse;
MODULE RosettaVListsUse;


Line 663: Line 663:


END RosettaVListsUse.
END RosettaVListsUse.
</syntaxhighlight>
</lang>
Execute: ^Q RosettaVListsUse.Use
Execute: ^Q RosettaVListsUse.Use
{{out}}
{{out}}
Line 682: Line 682:
=={{header|D}}==
=={{header|D}}==
{{trans|C}}
{{trans|C}}
<lang d>import core.stdc.stdio: fprintf, stderr;
<syntaxhighlight lang="d">import core.stdc.stdio: fprintf, stderr;
import core.stdc.stdlib: malloc, free, abort;
import core.stdc.stdlib: malloc, free, abort;


Line 817: Line 817:


v.free;
v.free;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>v.length = 10
<pre>v.length = 10
Line 842: Line 842:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 965: Line 965:
fmt.Println("show cdr releasing segment. 2 elements removed:", v)
fmt.Println("show cdr releasing segment. 2 elements removed:", v)
v.printStructure()
v.printStructure()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,001: Line 1,001:
A vlist preallocates storage in increasing powers of 2. The "head" of the vlist is in the last (largest) storage block. Here we define a class with methods <code>unshift</code> (which adds to the "head" of the vlist), <code>shift</code> (which removes from the "head" of the vlist), <code>size</code> (which tells us how many elements the vlist currently contains), and <code>get</code> (which retrieves the nth element from the vlist):
A vlist preallocates storage in increasing powers of 2. The "head" of the vlist is in the last (largest) storage block. Here we define a class with methods <code>unshift</code> (which adds to the "head" of the vlist), <code>shift</code> (which removes from the "head" of the vlist), <code>size</code> (which tells us how many elements the vlist currently contains), and <code>get</code> (which retrieves the nth element from the vlist):


<lang J>coclass 'vlist'
<syntaxhighlight lang="j">coclass 'vlist'


off=: 0
off=: 0
Line 1,040: Line 1,040:
end.
end.
r=. off{do current
r=. off{do current
}}</lang>
}}</syntaxhighlight>


Example use:
Example use:


<lang J> L=: conew'vlist'
<syntaxhighlight lang="j"> L=: conew'vlist'
size__L''
size__L''
0
0
Line 1,058: Line 1,058:
555
555
size__L''
size__L''
4</lang>
4</syntaxhighlight>


Caution: this is an accurate model of the vlist data structure but should not be used if performance is critical.
Caution: this is an accurate model of the vlist data structure but should not be used if performance is critical.
Line 1,073: Line 1,073:
languages with slower arrays.
languages with slower arrays.


<lang ruby>""" Rosetta Code task rosettacode.org/wiki/VList """
<syntaxhighlight lang="ruby">""" Rosetta Code task rosettacode.org/wiki/VList """


import Base.length, Base.string
import Base.length, Base.string
Line 1,226: Line 1,226:


testVList()
testVList()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
zero value for type. empty VList: []
zero value for type. empty VList: []
Line 1,328: Line 1,328:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


class VList<T : Any?> {
class VList<T : Any?> {
Line 1,443: Line 1,443:
println("Demonstrating cdr method again, 2 more elements removed: $v")
println("Demonstrating cdr method again, 2 more elements removed: $v")
v.printStructure()
v.printStructure()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,474: Line 1,474:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>type
<syntaxhighlight lang="nim">type


VSeg[T] = ref object
VSeg[T] = ref object
Line 1,579: Line 1,579:
v = v.cdr().cdr()
v = v.cdr().cdr()
echo "Show cdr releasing segment. Two elements removed: ", v
echo "Show cdr releasing segment. Two elements removed: ", v
v.printStructure()</lang>
v.printStructure()</syntaxhighlight>


{{out}}
{{out}}
Line 1,609: Line 1,609:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang objeck>class vList<T:Stringify> {
<syntaxhighlight lang="objeck">class vList<T:Stringify> {
@base : vSeg<T>;
@base : vSeg<T>;
@offset : Int;
@offset : Int;
Line 1,801: Line 1,801:
v->PrintStructure();
v->PrintStructure();
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,831: Line 1,831:
The ooRexx queue class is a vlist implementation.
The ooRexx queue class is a vlist implementation.
Here are some examples of usage:
Here are some examples of usage:
<syntaxhighlight lang="oorexx">
<lang ooRexx>
-- show how to use the queue class
-- show how to use the queue class
q = .queue~of(1, 2, 3, 4)
q = .queue~of(1, 2, 3, 4)
Line 1,852: Line 1,852:
say q[1] q[2] q[4]
say q[1] q[2] q[4]


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,862: Line 1,862:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">OFFSET</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (first spare slot [0=none])</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">OFFSET</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (first spare slot [0=none])</span>
Line 1,960: Line 1,960:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,997: Line 1,997:
{{trans|Go}}
{{trans|Go}}
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang perl6>class vList {
<syntaxhighlight lang="raku" line>class vList {


subset vEle of Any; # or Str
subset vEle of Any; # or Str
Line 2,074: Line 2,074:
$v := $v.cdr.cdr;
$v := $v.cdr.cdr;
say "show cdr releasing segment. 2 elements removed: ", $v;
say "show cdr releasing segment. 2 elements removed: ", $v;
$v.printStructure;</lang>
$v.printStructure;</syntaxhighlight>
{{out}}
{{out}}
<pre>zero value for type. empty vList: []
<pre>zero value for type. empty vList: []
Line 2,153: Line 2,153:
║ used. I.E.: 63.01 63.5 63.63 63.9 ║
║ used. I.E.: 63.01 63.5 63.63 63.9 ║
╚════════════════════════════════════════════════════════════════════╝
╚════════════════════════════════════════════════════════════════════╝
<lang rexx>/*REXX program demonstrates VList operations: add, update, delete, insert, show. */
<syntaxhighlight lang="rexx">/*REXX program demonstrates VList operations: add, update, delete, insert, show. */
/*could use instead: q = 1 2 3 4 */
/*could use instead: q = 1 2 3 4 */
call q 0, 1 2 3 4 /*populate the list with values 1 ── ►4*/
call q 0, 1 2 3 4 /*populate the list with values 1 ── ►4*/
Line 2,188: Line 2,188:
if j==ni then $=$ ! /*handle the "insert". */
if j==ni then $=$ ! /*handle the "insert". */
end /*j*/
end /*j*/
q=space($); return q /*elide superfluous blanks. */</lang>
q=space($); return q /*elide superfluous blanks. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,205: Line 2,205:
The Scala implementation shares a common root with the Clojure implementation, but is certainly not a port of it.</blockquote>
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.
A quote of Martin Odersky, his co-worker Phil Bagwell† invented the VList.
<lang Scala>object VList extends App {
<syntaxhighlight lang="scala">object VList extends App {


val emptyVlist1 = Vector.empty[Int]
val emptyVlist1 = Vector.empty[Int]
Line 2,222: Line 2,222:
assert(addedVlist1(3) == 10, "Wrong element accesed.")
assert(addedVlist1(3) == 10, "Wrong element accesed.")
println("Successfully completed without errors.")
println("Successfully completed without errors.")
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>class VSeg_ {
<syntaxhighlight lang="ecmascript">class VSeg_ {
construct new() {
construct new() {
_next = null
_next = null
Line 2,348: Line 2,348:
v = v.cdr().cdr()
v = v.cdr().cdr()
System.print("Demonstrating cdr method again, 2 more elements removed: %(v)")
System.print("Demonstrating cdr method again, 2 more elements removed: %(v)")
v.printStructure()</lang>
v.printStructure()</syntaxhighlight>


{{out}}
{{out}}