DEV Community

Kray-G
Kray-G

Posted on

Kinx v0.22.0 Preview Released

Hello everybody!

I published Kinx v0.22.0 preview release! Please see Release Page of Kinx

Introduction

At the version 0.22.0 it newly supported a pipeline/composition operator and some functional methods.

There are mainly 4 updates from v0.21.0.

Pipeline Operator

This is based on the proposal of JavaScript. a |> b means exactly same as b(a). In the same as that, a <| b which means a(b) is also available.

Here is a simple example of pipeline.

function doubleSay(str) {
    return "%{str}, %{str}";
}
function capitalize(str) {
    return str.toUpper(0, 1);
}
function exclaim(str) {
    return str + '!';
}

var result = exclaim(capitalize(doubleSay("hello")));
System.println(result); // => "Hello, hello!"

var result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

System.println(result); // => "Hello, hello!"
Enter fullscreen mode Exit fullscreen mode

Please see Pipeline Operator for details.

Function Composition Operator

The functions can be composited as a new function. a +> b means { => b(a(_1)) }. And a <+ b means { => a(b(_1)) }.

Let me tell you something a little about the syntax for people who doesn't familiar with Kinx function syntax. { => b(a(_1)) } means function(arg) { return b(a(arg)); } in JavaScript like syntax. Yes, _1 is a first argument.

Here is also a simple example of a function composition operator.

function doubleSay(str) {
    return "%{str}, %{str}";
}
function capitalize(str) {
    return str.toUpper(0, 1);
}
function exclaim(str) {
    return str + '!';
}

var result = exclaim(capitalize(doubleSay("hello")));
System.println(result); // => "Hello, hello!"

// Generates a new composed function.
var doubleSayThenCapitalizeThenExclaim
   = doubleSay +> capitalize +> exclaim;

var result = "hello" |> doubleSayThenCapitalizeThenExclaim;
System.println(result); // => "Hello, hello!"
Enter fullscreen mode Exit fullscreen mode

Please see Function Composition Operator for details.

Functional Library

The Functional is a global instance to deal with functional methods. In general, when you use a method of some object in pipeline, you should do it as a following example.

obj |> { &(obj) => obj.map({ => _1 * 2 }) }
    |> { &(obj) => obj.method() }
    |> // ... and so on.
Enter fullscreen mode Exit fullscreen mode

The Functional instance will wrap the actual function object and pass it into the pipeline.

For example, you can write it as follows against the example above.

obj |> Functional.map { => _1 * 2 }
    |> Functional.method()
        // Note that it needs a `()` to use an actual function object
        // which receives an object at a first argument.
    |> // ... and so on.
Enter fullscreen mode Exit fullscreen mode

The Enumerable is also useful in pipeline. The Enumerable is not only as a module but also a global instance having functional enumerable methods which is useful in a pipeline.
For example, when multiple values are passed to the next function via pipeline, a method in Enumerable can fetch a value one by one and pass the value to a next function in order.

Therefore, Enumerable can handle an infinite sequence. This is an example below.

1..
    |> Functional.enumerable
    |> Enumerable.drop(10)
    |> Enumerable.filter { => _1 % 3 == 0 }
    |> Enumerable.take(10)
    |> Enumerable.toArray
    |> System.println;
Enter fullscreen mode Exit fullscreen mode

The Functional.enumerable is a magic function to make a sequence change to an item which Enumerable methods can use.

Please see Functional for details.

Clipboard

Reading/Writing of clipboard is supported. Here is a simple usage of reading/writing of clipboard.

var cb = new Clipboard();
cb.write(str);
var r = cb.read();
Enter fullscreen mode Exit fullscreen mode

Please see Clipboard for details.

Conclusion

I stopped implementing a new feature, and I am going to make Kinx become v1.0.0 soon. I am now working hard on the release!

I hope you will be a user of Kinx.

Many thanks.

Top comments (0)