Ceiling
Computes the ceiling of a number, which is the smallest integer greater than or equal to that number.
Parameters
Ceiling[n]
The function requires exactly one argument.
n: TheNumberto be rounded up.
Return Value
Returns a Number representing the smallest integer value that is greater than or equal to n.
No domain-specific errors are thrown by this function.
Examples
(* Rounds a positive number up *)
Ceiling[5.3]; (* 6 *)
(* Rounds a negative number up (towards positive infinity) *)
Ceiling[-5.8]; (* -5 *)
(* An integer input remains unchanged *)
Ceiling[4]; (* 4 *)
(* The ceiling of zero is zero *)
Ceiling[0]; (* 0 *)
Chars
Splits a string into a list of its constituent characters, returning each character as a single-character string.
This function is fully Unicode-aware and correctly handles multi-byte characters and complex graphemes (like emoji) as single units.
KSL does not have a distinct character data type; instead, each “character” is represented as a String.
Special characters, such as newlines, are also converted into their own single-character string elements.
Parameters
Chars[s]
The function requires exactly one argument.
s: TheStringto be split into characters.
Return Value
Returns a List of Strings. Each string in the list corresponds to a single character from the input string, in the order they appeared.
No domain-specific errors are thrown by this function.
Examples
(* Splits a simple ASCII string *)
Chars["abc"]; (* {"a", "b", "c"} *)
(* Correctly handles multi-byte Unicode characters, newlines, and emoji *)
Chars["你好👋,世界🌏!"];
(* {"你", "好", "👋", ",", "世", "界", "🌏", "!"} *)
Chars["你好
世界
🌍"];
(* {"你", "好", "\n", "世", "界", "\n", "🌍"} *)
Note: The
"\n"in the output represents the single newline character. In KSL source code, this character is created with a literal newline (as shown above) or by using its Unicode code point,#10. The string literal"\n"would be interpreted as a two-character string:"\"and"n".
Chn
Converts between a numeric Unicode code point and its corresponding string character.
This function acts as a bidirectional converter: it turns an integer code point into a character string, or a single-character string into its integer code point.
Parameters
Chn[value]
The function requires exactly one argument. The behavior depends on the type of the argument.
value:- If it is a
Number, it is interpreted as a decimal Unicode code point. - If it is a
String, it must contain exactly one Unicode character.
- If it is a
Return Value
- Number: Returns a single-character
Stringcorresponding to the provided code point. - String: Returns a
Numberrepresenting the decimal Unicode code point of the character.
Errors
An error will occur under the following conditions:
- If the input
Numberis not a valid Unicode scalar value. - If the input
Stringdoes not contain exactly one valid character.
Examples
Character to Number
(* Converts a Chinese character to its code point *)
Chn["你"]; (* 20320 *)
(* Converts a standard ASCII character *)
Chn["a"]; (* 97 *)
(* This will cause an error because the emoji sequence is not treated as a single scalar value *)
Chn["❤️"];
(* ?
Error[ksl::builtin::Chn]: Invalid single character: `❤️`.
*)
Number to Character
(* Converts a code point back to a character *)
Chn[97]; (* "a" *)
(* Converts a code point to a CJK Unified Ideograph *)
Chn[19198]; (* "䫾" *)
(* This will cause an error because the number is outside the valid Unicode range *)
Chn[1919812];
(* ?
Error[ksl::builtin::Chn]: Invalid unicode: `1919812`.
*)
CloseStream
Closes an active UDP Socket, releasing its underlying system resources.
Parameters
CloseStream[symbol]
The function requires exactly one argument.
-
symbol: The argument must be aSymbolthat is bound to an active UDP Socket object.A UDP Socket is a
NativeObjectwith the type tag#UDPSocket.
Return Value
Returns Unit[].
As a crucial side effect, this function rebinds the input symbol itself to Unit[].
This destructive action prevents dangling references, making it safe by ensuring the closed UDP Socket cannot be used again.
No domain-specific errors are thrown by this function.
Examples
(* create a UDP socket and binds it to s *)
OpenStream[s, "127.0.0.1:11453"];
(* Verify the type and value of s *)
GetType[s]; (* #UDPSocket *)
Print[s]; (* => Native[UDPSocket] *)
(* Close the UDP Socket *)
CloseStream[s]; (* () *)
(* After closing, the variable s itself now holds Unit[] *)
Print[s]; (* => () *)
Concat
Concatenates a sequence of strings into a single string, or a sequence of lists into a single list.
Parameters
Concat[item1, item2, ...]
The function requires at least one argument.
item1, item2, ...: A sequence of arguments that must be either allStrings or allLists.
Return Value
- If all arguments are
Strings, returns a newStringrepresenting their concatenation. - If all arguments are
Lists, returns a newListcontaining the elements of all input lists in order. The concatenation is shallow; nested lists are preserved and not flattened.
No domain-specific errors are thrown by this function.
Examples
String Concatenation
Print[Concat["hello,", #10, "world!"]];
(* =>
hello,
world!
*)
List Concatenation
Concat[{ 1 }, { 2, 3 }, { 4, { 5, 6 } }, { { 7 } }];
(* {1, 2, 3, 4, {5, 6}, {7}} *)
Error on Missing Arguments
Concat[];
(* ?
Error[ksl::builtin::Concat]: Expected at least 1 parameter, but 0 were passed.
*)
Cond
Evaluates a sequence of condition-result pairs
and returns the result corresponding to the first condition that evaluates to #t.
This function serves as a cleaner alternative to deeply nested If expressions
when there are multiple conditions to check.
Parameters
Cond[branches, fallback]
The function requires exactly two arguments.
-
branches: AListof pairs.Each pair must itself be a
Listcontaining exactly two elements:{ condition, result }.condition: An expression that evaluates to a booleanAtom(#tor#f).result: The value to return if theconditionis#t.
-
fallback: The value to return if none of the conditions inbranchesevaluate to#t.
Evaluation Logic
- The function iterates through the
brancheslist in order. - For each
{ condition, result }pair, theconditionis evaluated. - Short-Circuiting:
- If a
conditionevaluates to#t, the function immediately evaluates and returns the correspondingresult. Subsequent conditions are skipped. - If a
conditionevaluates to#f, the function proceeds to the next pair.
- If a
- If the end of the
brancheslist is reached without any condition being#t, thefallbackis evaluated and returned.
Return Value
Returns the result of the first matching branch, or the default_value.
Errors
An error will occur if:
branchesis not aList.- Any element within
branchesis not aListof two elements. - A
conditionexpression does not evaluate to a boolean.
Examples
Let[x, 8];
Cond[{
{ Less[x, 6], #low },
{ Less[x, 8], #mid }
}, #high]; (* #high *)
Let[x, 3];
Cond[{
{ Less[x, 6], #low },
{ Less[x, 8], #mid }
}, #high]; (* #low *)
Consume
Waits for a thread to complete its execution and retrieves its return value.
This function “consumes” the thread handle, meaning it can only be successfully called once for any given thread. It blocks the current execution flow until the target thread has finished.
Parameters
Consume[thread]
The function requires exactly one argument.
thread: AThreadobject, created by theThreadfunction.
Return Value
The return value depends on whether the thread has been previously consumed.
-
On the first successful call, it returns the value that was returned by the expression evaluated within the thread. The type of this value is determined by the thread’s final expression.
-
If the
threadhas already been consumed, the function returns aListindicating an error:{#err, "Removed thread."}
No domain-specific errors are thrown by this function.
Examples
The following example demonstrates creating two threads, consuming them, and then attempting to consume one a second time.
Block[
Thread[t1, { }, Block[
Sleep[1],
Print["Finished 1"],
1
]],
Thread[t2, { }, Block[
Sleep[0.5],
Print["Finished 2"],
2
]],
Print[Consume[t1]],
Print[Consume[t2]],
Print[Consume[t1]]
];
(* =>
Finished 2
Finished 1
1
2
{#err, "Removed thread."}
*)
Explanation of the output:
Finished 2appears first becauset2has a shorter sleep duration.
Finished 1appears next. The main thread was blocked byConsume[t1]untilt1completed.
1is the return value ofConsume[t1].
2is the return value ofConsume[t2].The main thread retrieves this value instantly as
t2had already finished.
{#err, "Removed thread."}is returned by the second attempt to consumet1.
Cos
Computes the cosine of an angle given in radians.
Parameters
Cos[n]
The function requires exactly one argument.
n: ANumberrepresenting the angle in radians.
Return Value
Returns a Number in the range $\left[-1, 1\right]$, which is the cosine of the input angle.
No domain-specific errors are thrown by this function.
Examples
(* The cosine of 0 is 1 *)
Cos[0]; (* 1 *)
(* The cosine of pi is -1 *)
Cos[3.1415926535898]; (* -1 *)
(* The cosine of pi/2 is almost 0 *)
Cos[1.5707963267949]; (* 6.12323399573677e-17 *)
Cos[-1]; (* 0.54030230586814 *)
Cosh
Computes the hyperbolic cosine of a number.
Parameters
Cosh[n]
The function requires exactly one argument.
n: ANumber.
Return Value
Returns a Number greater than or equal to 1, which is the hyperbolic cosine of the input.
No domain-specific errors are thrown by this function.
Examples
(* The hyperbolic cosine of 0 is 1 *)
Cosh[0]; (* 1 *)
(* Cosh is an even function, so Cosh[x] == Cosh[-x] *)
Cosh[1]; (* 1.54308063481524 *)
Cosh[-1]; (* 1.54308063481524 *)
Eq[Cosh[1], Cosh[-1]]; (* #t *)