- endsWith(string, suffix) PD_endsWith.java
True if string ends with suffix.
=> endsWith("this is a test", "test");
~< Result: true >~
=> endsWith("this is a test", "not a test");
~< Result: false >~
=> endsWith("this is a test", "");
~< Result: true >~
=> endsWith("", "");
~< Result: true >~
- matches(string, regex) PD_matches.java
Returns a boolean to indicate if string matches the regular expression regex.
=> matches("this is a test", ".*test.*");
~< Result: true >~
=> matches("this is a test", ".*xtest.*");
~< Result: false >~
=> matches("this is a test", "\\s");
~< Result: false >~
=> matches("this is a test", ".*\\s.*");
~< Result: true >~
=> matches("this is a test", "^this.*\\s.*");
~< Result: true >~
// One must escape "$":
=> matches("this is a test", "^this.*xxx\$");
~< Result: false >~
- replaceAll(string, regex, replacement) PD_replaceAll.java
Replace all occurrences of regex with replacement.
=> replaceAll("this is a string"," ", "|");
~< Result: "this|is|a|string" >~
=> replaceAll("this is a string"," ", "");
~< Result: "thisisastring" >~
=> replaceAll("this is a string","", "-");
~< Result: "-t-h-i-s- -i-s- -a- -s-t-r-i-n-g-" >~
- replaceFirst(string, regex): PD_replaceFirst.java
Replace the first occurrence of regex with replacement.
=> replaceFirst("this is a string"," ", "|");
~< Result: "this|is a string" >~
- splitRx(string, regex) : PD_splitRx.java
Split string with regular expression regex.
=> splitRx("this is a string","\\s");
~< Result: ["this", "is", "a", "string"] >~
=> splitRx("this is a string","[ts]+");
~< Result: ["", "hi", " i", " a ", "ring"] >~
=> splitRx("this is a string","[ir]+");
~< Result: ["th", "s ", "s a st", "ng"] >~
=> splitRx("this is a string","[^this]+");
~< Result: ["this", "is", "st", "i"] >~
=> splitRx("this is a string","^this\\s");
~< Result: ["", "is a string"] >~
=> splitRx(";",";");
~< Result: ["", ""] >~
=> splitRx(";","");
~< Result: [";"] >~
- startsWith(string, prefix) : PD_startsWith.java
True if string starts with prefix.
=> startsWith("this is a string","this");
~< Result: true >~
=> startsWith("this is a string","string");
~< Result: false >~
- toLower(string) : PD_toLower.java
Returns a lower case version of string.
=> a:="Håkan"; b:=toUpper("håkan"); c:=toLower(b); print([a,b,c]);
["Håkan", "HÅKAN", "håkan"]
- toUpper(string) : PD_toUpper.java
Returns an upper case version of string.
(see toLower() above)
- trim(string) : PD_trim.java
Return a trimmed version of string (both left and right trim).
=> trim(" this is a string ");
~< Result: "this is a string" >~
=> trim("this is a string ");
~< Result: "this is a string" >~
=> trim(" this is a string");
~< Result: "this is a string" >~
- shuffle(string_or_list) : PD_shuffle.java
Returns a shuffled list or string.
=> shuffle("random string");
~< Result: "rma stgodnnir" >~
=> shuffle("random string");
~< Result: "rmiroadng snt" >~
=> shuffle("random string");
~< Result: "amtgnrsinr do" >~
=> shuffle([1..10]);
~< Result: [8, 2, 3, 1, 6, 10, 5, 7, 4, 9] >~
=> shuffle([1..10]);
~< Result: [5, 4, 9, 2, 6, 1, 8, 10, 3, 7] >~
=> shuffle([[1..3],[4..5], [6..8]]);
~< Result: [[4, 5], [6, 7, 8], [1, 2, 3]] >~
// Only lists and strings
=> shuffle({1..3});
Error in "shuffle({1 .. 3})":
argument '{1, 2, 3}' is not a list or string.
- sort(string_or_list) : PD_sort.java
As with shuffle(), handles both strings and lists:
=> sort("random string");
~< Result: " adgimnnorrst" >~
=> sort("");
~< Result: "" >~
=> sort("abc");
~< Result: "abc" >~
=> sort([4,3,2,1]);
~< Result: [1, 2, 3, 4] >~
=> sort([1..10]);
~< Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >~
=> sort([]);
~< Result: [] >~
=> sort({1..3});
Error in "sort({1 .. 3})":
argument '{1, 2, 3}' is not a list or string.
=> t1 := now(); s:=shuffle([1..1000000]); t2:=now(); print("shuffle took $(t2-t1)/1000.0$s"); s2 :=sort(s); print("sort took $(now()-t2)/1000.0$s");
shuffle took 0.629s
sort took 0.744s
- matchesCapture(string, regex) : PD_matchesCapture.java
A version of matches
that returns a list of the captures of a regex (using Java's regex engine). It is to be considered highly experimental...
=> matchesCapture("this is a string","^([^\\s]+)\\s+([^\\s]+)");
~< Result: ["this", "is"] >~
=> matchesCapture("abbccc","^(a+)(b+)(c+)\$");
~< Result: ["a", "bb", "ccc"] >~
=> matchesCapture("a repeating string string","(.+)(\\1)");
~< Result: ["ing str", "ing str"] >~
// returns Java exception on error
=> matchesCapture("abbccc","^(a+)(b+)(c+\$");
java.util.regex.PatternSyntaxException: Unclosed group near index 13
^(a+)(b+)(c+$
^
~< Result: om >~
- npow(n, set) : : PD_npow.java
Returns all subsets of set of size n.
=> npow(2,{1..3});
~< Result: {{1, 2}, {1, 3}, {2, 3}} >~
=> npow(3,{1..6});
~< Result: {{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 2, 6}, {1, 3, 4}, {1, 3, 5}, {1, 3, 6}, {1, 4, 5}, {1, 4, 6}, {1, 5, 6}, {2, 3, 4}, {2, 3, 5}, {2, 3, 6}, {2, 4, 5}, {2, 4, 6}, {2, 5, 6}, {3, 4, 5}, {3, 4, 6}, {3, 5, 6}, {4, 5, 6}} >~
=> npow(3,{});
~< Result: {} >~
=> n := 12; s:= {1..n}; [#npow(i,s) : i in [0..n]];
~< Result: [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1] >~
- even(n) : : PD_even.java
Returns true if n is even, else false.
=> [[i,even(i)] : i in [1..8]];
~< Result: [[1, false], [2, true], [3, false], [4, true], [5, false], [6, true], [7, false], [8, true]] >~
=> even("asdf");
Error in "even("asdf")":
argument '"asdf"' is not an integer.
- odd(n) : : PD_odd.java
Returns true if n is odd, else false.
=> [[i,odd(i)] : i in [1..8]];
~< Result: [[1, true], [2, false], [3, true], [4, false], [5, true], [6, false], [7, true], [8, false]] >~
=> odd("asdf");
Error in "odd("asdf")":
argument '"asdf"' is not an integer.
- span(string, pattern) : : PD_span.java
SNOBOL string scanning function : removes and returns all initial characters in string that are in pattern.
=> a:="abcdef ghijk"; b:= span(a, "abcABC"); print([a,b]);
["def ghijk", "abc"]
=> a:="abcdef ghijk"; b:= span(a, "ABC"); print([a,b]);
["abcdef ghijk", ""]
- breaks(string, pattern) : : PD_breaks.java
SNOBOL string scanning function : removes and returns all initial characters in string that are NOT in pattern.
=> a:="abcdef ghijk"; b:= breaks(a, " "); print([a,b]);
[" ghijk", "abcdef"]
=> a:="abcdef ghijk"; b:= breaks(a, "abc"); print([a,b]);
["abcdef ghijk", ""]
- any(string, pattern) : : PD_any.java
SNOBOL string scanning function : removes and returns first initial characters in string that are in pattern.
=> a:="abcdef ghijk"; b:= any(a, "abcABC"); print([a,b]);
["bcdef ghijk", "a"]
=> a:="abcdef ghijk"; b:= any(a, "ABC"); print([a,b]);
["abcdef ghijk", ""]
- notany(string, pattern) : : PD_notany.java
SNOBOL string scanning function : removes and returns first characters in string if it's NOT in pattern.
=> a:="abcdef ghijk"; b:= notany(a, " "); print([a,b]);
["bcdef ghijk", "a"]
=> a:="abcdef ghijk"; b:= notany(a, "abc"); print([a,b]);
["abcdef ghijk", ""]
- len(string, n) : : PD_len.java
SNOBOL string scanning function : removes and returns first n characters in string
=> a:="abcdef ghijk"; b:= len(a, 4); print([a,b]);
["ef ghijk", "abcd"]
=> a:="abcdef ghijk"; b:= len(a, 14); print([a,b]);
["abcdef ghijk", ""]
=> a:="abcdef ghijk"; b:= len(a, 1); print([a,b]);
["bcdef ghijk", "a"]
- matchs(string, pattern) : : PD_matchs.java
SNOBOL string scanning function : removes and returns pattern if it match the initial part of string.
=> a:="abcdef ghijk"; b:= matchs(a, "abc"); print([a,b]);
["def ghijk", "abc"]
=> a:="abcdef ghijk"; b:= matchs(a, "a"); print([a,b]);
["bcdef ghijk", "a"]
=> a:="abcdef ghijk"; b:= matchs(a, "abd"); print([a,b]);
["abcdef ghijk", ""]
=> a:="abcdef ghijk"; b:= matchs(a, "ABC"); print([a,b]);
["abcdef ghijk", ""]
- lpad(string, n) : : PD_lpad.java
SNOBOL string scanning function : returns the string padded to length n with space to left.
=> a:="123456789"; b:= lpad(a, 14); print("<$b$> len:$#b$");
< 123456789> len:14
=> a:="123456789"; b:= lpad(a, 4); print("<$b$> len:$#b$");
<123456789> len:9
- rpad(string, n) : : PD_rpad.java
SNOBOL string scanning function : returns the string padded to length n with space to right.
=> a:="123456789"; b:= rpad(a, 4); print("<$b$> len:$#b$");
<123456789> len:9
=> a:="123456789"; b:= rpad(a, 14); print("<$b$> len:$#b$");
<123456789 > len:14
- ltrim(string) : : PD_ltrim.java
SNOBOL string scanning function : returns the string with initial space removed
=> a:=" 123456789"; b:= ltrim(a); print("<$b$>");
<123456789>
=> a:=" 123456789 "; b:= ltrim(a); print("<$b$>");
<123456789 >
- rtrim(string) : : PD_rtrim.java
SNOBOL string scanning function : returns the string with trailing space removed
=> a:=" 123456789 "; b:= rtrim(a); print("<$b$>");
< 123456789>
=> a:=" 123456789 "; b:= rtrim(a); print("<$b$>");
< 123456789>
- next_permutation(list) : : PD_next_permutation.java
Returns next permutation of list. Returns om if last permutation.
=> a:= [1,2,3]; print(a); while (a != om) { a:= next_permutation(a); print(a); }
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
> t1 := now(); a:= [0..9]; while (a != om) { a:= next_permutation(a); } print("It took $(now()-t1)/1000.0$s");
It took 1.7s
- isPrime(n) : : PD_isPrime.java
Returns true if n is a prime, false otherwise.
=> [[i,isPrime(i)] : i in [1..10]];
~< Result: [[1, false], [2, true], [3, true], [4, false], [5, true], [6, false], [7, true], [8, false], [9, false], [10, false]] >~
=> isPrime(2**31+5);
~< Result: true >~
=> n := 2**41; [ [n+i,isPrime(n+i)] : i in [1..5]];
~< Result: [[2199023255553, true], [2199023255554, false], [2199023255555, true], [2199023255556, false], [2199023255557, true]] >~
- join(list, sep) : : PD_join.java
Returns a string with all elements in list separated by sep.
=> join([1..14], " ");
~< Result: "1 2 3 4 5 6 7 8 9 10 11 12 13 14" >~
> join(strSplit("abcde",""), "+");
~< Result: "a+b+c+d+e" >~