DEV Community

Kray-G
Kray-G

Posted on

Kinx Library - Integer

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.

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);
Enter fullscreen mode Exit fullscreen mode

Let's run it.

10000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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)