String Library
The definitions provided by this library operate on strings to produce new strings or statistics about them.
Functions
N
(x: Numeric, accuracy -> 40) :> String
Transforms a numeric value into a decimal representation with the number of digits specified by the +accuracy+ option.
Examples
ns:math := "http://www.dallycot.net/ns/math/1.0#";
N(golden-ratio(), accuracy -> 5) = "1.6180"
N(pi()) = "3.141592653589793238462643383279502884197"
ends-with?
(string: String, pattern: String) → Boolean
Returns true if the string contains the pattern at the end.
Implementation
(value, substr) :> (
string-take(value, < length(value) - length(substr) + 1, length(value) >) = substr
)
hash
(string: String) → Numeric
Calculates the MD5 hash of the given string.
Examples
hash("Foo") = 25705895308379540894114195938181562973
hash("Bar") = 294773822637331873101822657838443808339
hash("The big bad bug bit the big black bear") = 307707364239674263206139995325741384263
Implementation
This is implemented natively.
number-string
(number: Numeric, base: Numeric = 10) → String
Returns a string representation of the given number in terms of the given base (defaults to base 10).
If the number is not an integer, then the string will consist of the numerator and denominator separated by a forward slash (/
).
Base must be between 2 and 64 inclusive.
Examples
number-string(1.23) = "123 / 100"
number-string(1.23, 5) = "443 / 400"
number-string(pi(10), 2) = "1011101101000000111001100100111 / 11101110011010110010100000000"
number-string(pi(10), 8) = "13550071447 / 3563262400"
number-string(pi(10), 16) = "5da07327 / 1dcd6500"
number-string(pi(10), 64) = "BdoHMn / dzWUA"
Implementation
This is implemented natively.
starts-with?
(string: String, pattern: String) → Boolean
Returns true if the string starts with the pattern.
Implementation
(value, substr) :> (
string-take(value, 1..length(substr)) = substr
)
string-contains?
(string: String, pattern: String) → Boolean
Returns true if the string contains the pattern.
Examples
string-contains?("Foo", "oo") = True
string-contains?("Bar", "Ar") = False
Implementation
This is implemented natively.
string-drop
(s: String, count: Numeric) :> String
Returns a new string representing the content of the given string after removing the initial number of characters indicated by the given count.
Examples
string-drop("The big bad bug bit the big black bear", 20) = "the big black bear"
string-drop("The big bad bug bit the big black bear", 1234) = ""
Implementation
This is implemented natively.
string-join
(joiner: String, s: [String]) :> [String]
Transforms a stream of strings into a stream containing the accumulation of stream elements joined by the given string.
Examples
Implementation
This is implemented natively. The behavior mimics the following definition.
(joiner, stream) :>
foldl1({
#1 ::> joiner ::> #2
}/2, stream)
string-split
(s: String, pattern: String, limit: Numeric) → [String]
Splits a string. The pattern and limit are optional.
If no pattern is provided, then the string is split on whitespace. Otherwise, the string is split on every occurence of the pattern. The pattern is not returned.
If a pattern is provided, an optional limit may also be provided. The string will not be split into more pieces than the limit.
Examples
string-split("The big black bear") = <<The big blank bear>>
string-split("The big black bear", "b") = <"The ", "ig ", "lack ", "ear">
string-split("The big black bear", "b", 3) = <"The ", "ig ", "lack bear">
Implementation
This is implemented natively.
string-take
(s: String, spec: Numeric|[Numeric]) :> String
Returns part of the given string based on the form and value of the specification.
-
Numeric
Returns the character at the given location as a string.
-
[Numeric]
Returns a new string consisting of the characters from the string at the locations indicated in the list of numbers.
Examples
string-take("The big bad bug bit the big black bear", 1..10) = "The big ba"
string-take("The big bad bug bit the big black bear", 5) = "b"
string-take("The big bad bug bit the big black bear", 3..6) = "e big "
string-take("The big bad bug bit the big black bear", odds) = "Tebgbdbgbttebgbakba"
Implementation
This is implemented natively. The behavior mimics the following definition.
string-take(string, positions) :> (
string-join("",
y-combinator(
(self, s, p, l) :> (
(length(s) = 0 or length(p) = 0) : [ ]
(p' = location ) : [ s', self(self, s..., p..., l + 1) ]
( ) : self(self, s..., p , l + 1)
)
)(string, positions)
)
)