lib for developers : libList

+- HP Forums (http://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: lib for developers : libList (/thread-7001.html)



lib for developers : libList - primer - 10-08-2016 03:37 PM

Hello,
The hpprime provide already many usefull list functions, mostly for math,
But I'm sharing with you my libList v1 that provide some basic list functions for programming.

API list : L_GET, L_SET, L_INS, L_DEL, L_MAP, ARR2LST and LST2VECT

L_INS(lst,pos,item)
Insert an item to a list. Retruns the new list
if pos is null, insert at the end
if pos<0, count -pos from right to insert at
ex :
Code:
L_INS({11,22,33},1, 99); // returns {99,11,22,33}
L_INS({11,22,33},0,99); // returns {11,22,33,99}
L_INS({11,22,33},-1,99); // returns {11,22,99,33}
L_INS({1,2,3},2,{7}); // returns {1,{7},2,3} -- insert any kind of object


L_SET(lst,pos,item)
Set (change) an item from a list. Retruns the new list.
note : it does not add item, list size does not increase

if pos is null, change the last item
if pos<0, count -pos from left to change
ex :
Code:
L_SET({11,22,33},1, 99); // returns {99,22,33}
L_SET({11,22,33},0,99); // returns {11,22,99}
L_SET({11,22,33},-1,99); // returns {11,99,33}
L_SET({1,2,3},2,{7});  // returns {1,{7},3}


L_DEL(lst,pos)
Remove an item from a list. Returns the new list
if pos is null, remove the last item
if pos<0, remove by counting -pos from the left
ex :
Code:
L_DEL({11,22,33},1); // returns {22,33}
L_DEL({11,22,33},0); // returns {11,22}
L_DEL({11,22,33},-1); // returns {11,33}


L_GET(lst,pos)
Get an item from a list. Returns the item
if pos is null, get last item
if pos<0, count -pos from left
if pos exceed list size, return last
ex :
Code:
L_GET({11,22,33,44},2); // returns 22
L_GET({11,22,33,44},0); // returns 44
L_GET({11,22,33,44},-1); // returns 33


L_MAP(lst,userfunction)
Call a function on each list item. Returns the list of function returns
ex :
Code:
EXPORT myfct(a) // a userfunction
RETURN a*2;
END;

L_MAP({1,2,3},"myfct"); // returns {2,4,6}


ARR2LST(array)
Convert vector or matrix to list
resulting list have embeded lists in case of matrix
ex :
Code:
ARR2LST([1,2,3]); // returns {1,2,3}
ARR2LST([[1,2,3]]); // returns {{1,2,3}}
ARR2LST([[1,2][3,4]]); // returns {{1,2},{3,4}}


LST2VECT(lst)
Convert a simple list to a vector
note : list items must be numbers
ex :
Code:
LST2VECT({1,2,3}); // returns [1,2,3]


SPLIT(string,separator)
Split a string into a list
separator can be any size.
ex :
Code:

SPLIT("1;2;3",";"); // returns {"1","2",3"}
SPLIT("123,abc,456",","); // returns {"123","abc",456"}
SPLIT("!xyz!!abcd","!"); // returns {"","xyz","",abcd"}
SPLIT("12131415","14"); // returns {"1213","15"}
SPLIT("12345","x"); // returns {"12345"}
download : [attachment=4043]