Hello, everybody!
The script language Kinx is published with the concept of Looks like JavaScript, Feels like Ruby, Stable like AC/DC(?).
This time it is Integer.
- Reference
- First motivation ... The post of introduction
- Kinx, I wanted a scripting language with a syntax of C family.
- Repository ... https://github.com/Kray-G/kinx
- I am waiting for pull requests.
- First motivation ... The post of introduction
The method bound to the Integer
object is a special method, and it can directly act on the integer value. Please refer to Kinx library - String for details about special methods and special objects.
Integer
special object
An example of function definition for Integer
object is as follows.
Integer.times100 = function(value) {
return value * 100;
};
var val = 100.times100();
System.println(val);
Let's run it.
10000
The receiver comes in the first argument.
Integer
Built-in special methods
Method | Meaning |
---|---|
Integer.times(val, callback) | As the range of i = 0 to (val-1), if callback exists, it is the result of callback(i) , and if it does not exist, creates an array with i and returns it. |
Integer.upto(val, max, callback) | Call callback(i) as an argument in the range of i = val to max. |
Integer.downto(val, min, callback) | Call callback(i) as an argument in the range of i = min to val. |
Integer.toString(val, base) | Converts val into a string. base only supports 10 and 16. |
Integer.toDouble(val) | Converts val into Double. |
Math Object Method
Integer objects have the same special methods as Math objects. See below for details.
When written in a concrete example, the following can be written, for example.
var a = 2.pow(10); // same as Math.pow(2, 10) => 1024
var b = (-10).abs(); // same as Math.abs(-10) => 10
Note that unary minus (-
) has lower precedence than function calls, so it must be enclosed in parentheses.
Special operator
unary *
operator
When the unary *
operator is applied to an integer value, the character string (1 character) corresponding to the character code is returned.
var a = *97; // => "a"
By the way, the reverse conversion (*a
) cannot be restored. Note that the unary *
operator for strings is an array, so *a
in the above example would be an array [97]
. To get the character code alone, use a[0]
.
Conclusion
It has a special method in Integer, which makes it feel like Ruby. I feel it is very good by being able to write it like 2.pow(10)
.
Look at Here, I will support the methods that are not supported right now, in the future.
See you next time.
Top comments (0)