HP Forums
lib for developers : libList - Printable Version

+- HP Forums (https://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 09: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.
[attachment=4022] see next post for version 2.

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 right 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 right
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 right
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]



RE: lib for developers : libList - primer - 10-16-2016 09:46 AM

version 2
added : the split() command


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]


RE: lib for developers : libList - Guenter Schink - 10-16-2016 07:46 PM

(10-08-2016 09:37 PM)primer Wrote:  if pos<0, count -pos from left to insert at

I guess it should read "count -pos from right to insert ..."

I'd suggest that "-1" should be the first element from right, like "1" does address the first element from left, for consistency.

Günter


RE: lib for developers : libList - primer - 09-17-2017 05:10 PM

(10-16-2016 07:46 PM)Guenter Schink Wrote:  I guess it should read "count -pos from right to insert ..."
Hello Günter, you are right, I fixed it on first post.

(10-16-2016 07:46 PM)Guenter Schink Wrote:  I'd suggest that "-1" should be the first element from right, like "1" does address the first element from left, for consistency.
It makes sense, really good idea.
But I don't know if it worth to break API compatibility.


RE: lib for developers : libList - StephenG1CMZ - 10-02-2017 09:02 PM

L_INS({1,2,3},0,99) = {1,2,3,99} ?? (From your example)

I am unsure how an index of 0 should be parsed in this case.
When a value is read, 0 refers to the last value, and when it is written, the new value about to be added.

Does this mean it should be inserted before the value yet to be written (i.e. appended at the end)?
Or does it mean that it should be inserted before {1,2,3}(0) i.e. 3, giving {1,2,99,3}?

Given that {1,2,3,99} is trivial
{1,2,3}(0):=99
Perhaps {1,2,99,3} is more useful?
Of course, it should also be offset by 1 from your -1 position.

For comparison:
In Java and Python, lists are indexed from 0, so "insert before position 0" would be {99,1,2,3}
Although (0) is a useful syntax in PPL, here it is less obvious to me what it should refer to.