-
Notifications
You must be signed in to change notification settings - Fork 118
Collection manipulation
Wang Renxin edited this page May 31, 2022
·
32 revisions
MY-BASIC supplies a set of LIST
, DICT
manipulation function libraries which offer creation, accessing, iteration, etc. as follow:
Name | Description |
---|---|
LIST | Creates a list |
DICT | Creates a dictionary |
PUSH | Pushes a value to the tail of a list, overridable for referenced usertype and class instance |
POP | Pops a value from the tail of a list, overridable for referenced usertype and class instance |
BACK | Peeks the value of a tail of a list, overridable for referenced usertype and class instance |
INSERT | Inserts a value at a specific position of a list, overridable for referenced usertype and class instance |
SORT | Sorts a list increasingly, overridable for referenced usertype and class instance |
EXISTS | Tells whether a list contains a specific value (not index), or whether a dictionary contains a specific key, overridable for referenced usertype and class instance |
INDEX_OF | Gets the index of a value in a list, overridable for referenced usertype and class instance |
GET | Returns the value of a specific index in a list, or the value of a specific key in a dictionary, or a member of a class instance, overridable for referenced usertype and class instance |
SET | Sets the value of a specific index in a list, or the value of a specific key in a dictionary, or a member variable of a class instance, overridable for referenced usertype and class instance |
REMOVE | Removes the element of a specific index in a list, or the element of a specific key in a dictionary, overridable for referenced usertype and class instance |
CLEAR | Clears a list or a dictionary, overridable for referenced usertype and class instance |
CLONE | Clones a collection, or a referenced usertype |
TO_ARRAY | Copies all elements from a list to an array |
ITERATOR | Gets an iterator of a list or a dictionary, overridable for referenced usertype and class instance |
MOVE_NEXT | Moves an iterator to next position for a list or a dictionary, overridable for referenced usertype and class instance |
Besides, it's also possible to apply some other generic function to collections:
Name | Description |
---|---|
TYPE | Gets the type string of a value, overridable for referenced usertype and class instance |
VAL | Returns the number type value of a string, or the value of a dictionary iterator, overridable for referenced usertype and class instance |
LEN | Gets element count of a collection, overridable for referenced usertype and class instance |
For example:
l = list(1, 2, 3, 4)
set(l, 1, “b”)
print exists(l, 2); pop(l); back(l); len(l);
d = dict(1, “one”, 2, “two”)
set(d, 3, “three”)
print len(d)
it = iterator(d)
while move_next(it)
print get(it);
wend
It's possible to implement other collection functions with built-in ones.
A REVERSED
function as:
def reversed(l)
if not l is type("list") then
return nil
endif
ret = list()
while len(l) <> 0
push(ret, pop(l))
wend
return ret
enddef
CAR
and CDR
:
def car(l)
if not (l is type("list")) then
return nil
endif
if len(l) = 0 then
return nil
endif
return l(0)
enddef
def cdr(l)
if not (l is type("list")) then
return nil
endif
if len(l) = 0 then
return nil
endif
ret = clone(l)
remove(ret, 0)
return ret
enddef
A JOIN_STR
function:
def join_str(lst, sep)
ret = ""
l = len(lst)
for i = 0 to l - 1
ret = ret + lst(i)
if i <> l - 1 then
ret = ret + sep
endif
next
return ret
enddef
Sorted iterator for dictionary, with usage:
class iterator_node
var key = nil
var value = nil
endclass
def sorted_iterator(d)
l = list()
i = iterator(d)
while move_next(i)
push(l, get(i))
wend
sort(l)
i = iterator(l)
n = new(iterator_node)
return lambda()
(
if not move_next(i) then
return nil
endif
k = get(i)
n.key = k
n.value = d(k)
return n
)
enddef
d = dict()
d(1) = "uno"
d(2) = "dos"
d(3) = "thres"
i = sorted_iterator(d)
p = i()
while p
print p.key, ", ", p.value;
p = i()
wend
It's also possible to visit each value in a list or each key in a dictionary using the FOR IN
statement, besides using iterators. For example:
l = list(1, 2, 3, 4, 5)
for i in l
print i;
next
d = dict(1, 2, 3, 4, 5, 6)
for i in d
print i, ", ", d(i);
next
Use the TO
statement to create a ranged list with integers as:
list(100 to 0)
list(0 to 100)
Read the Stack module page to get information about how to implement a stack collection.
- Principles
- Coding
- Data types
- Standalone shell
- Integration
- Customization
- More scripting API
- FAQ