Category talk:Wren-llist: Difference between revisions
Content added Content deleted
(→Source code: Bug fix.) |
(→Source code: Added a 'nodes' iterator and changed a method name (swap -> exchange).) |
||
Line 269: | Line 269: | ||
// Exchanges the elements at indices 'i' and 'j' of the current instance. |
// Exchanges the elements at indices 'i' and 'j' of the current instance. |
||
exchange(i, j) { |
|||
var t = this[i] |
var t = this[i] |
||
this[i] = this[j] |
this[i] = this[j] |
||
Line 288: | Line 288: | ||
var i = start |
var i = start |
||
for (e in seq) { |
for (e in seq) { |
||
if (e == d) return i |
|||
i = i + 1 |
|||
} |
} |
||
return -1 |
return -1 |
||
Line 389: | Line 389: | ||
// Returns true if this instance contains NONE of the values, false otherwise. |
// Returns true if this instance contains NONE of the values, false otherwise. |
||
containsNone(ds) { !containsAny(ds) } |
containsNone(ds) { !containsAny(ds) } |
||
⚫ | |||
// Combines the elements of this instance plus those of another LinkedList object |
// Combines the elements of this instance plus those of another LinkedList object |
||
// into a new LinkedList and returns it. |
// into a new LinkedList and returns it. |
||
Line 409: | Line 409: | ||
iteratorValue(iterator) { iterator.data } |
iteratorValue(iterator) { iterator.data } |
||
// Iterates through the nodes of this instance and returns for each one |
|||
// a list containing the current and next data members. |
|||
nodes { |
|||
class N is Sequence { |
|||
construct new(head) { |
|||
_head = head |
|||
} |
|||
iterate(iterator) { |
|||
if (!iterator) { |
|||
return !_head ? false : _head |
|||
} |
|||
return iterator.next |
|||
} |
|||
iteratorValue(iterator) { |
|||
var n = iterator.next |
|||
var next = (n) ? n.data : null |
|||
return [iterator.data, next] |
|||
} |
|||
} |
|||
return N.new(_head) |
|||
} |
|||
// Prints the consecutive elements of the current instance to stdout |
// Prints the consecutive elements of the current instance to stdout |
||
// separated by a single space and followed by a new line. |
// separated by a single space and followed by a new line. |
||
print() { |
print() { |
||
for (e in this) System.write("%(e) ") |
for (e in this) System.write("%(e) ") |
||
System.print() |
System.print() |
||
} |
} |
||
Line 703: | Line 727: | ||
// Exchanges the elements at indices 'i' and 'j' of the current instance. |
// Exchanges the elements at indices 'i' and 'j' of the current instance. |
||
exchange(i, j) { |
|||
var t = this[i] |
var t = this[i] |
||
this[i] = this[j] |
this[i] = this[j] |
||
Line 900: | Line 924: | ||
} |
} |
||
return R.new(_tail) |
return R.new(_tail) |
||
} |
} |
||
⚫ | |||
// Iterates through the nodes of this instance and returns for each one |
|||
// a list containing the previous, current and next data members. |
|||
nodes { |
|||
class N is Sequence { |
|||
construct new(head) { |
|||
_head = head |
|||
} |
|||
iterate(iterator) { |
|||
if (!iterator) { |
|||
return !_head ? false : _head |
|||
} |
|||
return iterator.next |
|||
} |
|||
iteratorValue(iterator) { |
|||
var p = iterator.prev |
|||
var prev = (p) ? p.data : null |
|||
var n = iterator.next |
|||
var next = (n) ? n.data : null |
|||
return [prev, iterator.data, next] |
|||
} |
|||
} |
|||
return N.new(_head) |
|||
} |
|||
// Prints the consecutive elements of the current instance to stdout |
// Prints the consecutive elements of the current instance to stdout |
||
// separated by a single space and followed by a new line. |
// separated by a single space and followed by a new line. |
||
print() { |
print() { |
||
for (e in this) System.write("%(e) ") |
for (e in this) System.write("%(e) ") |
||
System.print() |
System.print() |
||
} |
} |
||
// As 'print' method but prints the elements in reverse. |
// As 'print' method but prints the elements in reverse. |
||
rprint() { |
rprint() { |
||
for (e in this.reversed) System.write("%(e) ") |
for (e in this.reversed) System.write("%(e) ") |
||
System.print() |
System.print() |
||
} |
} |
||