Kinx v0.22.0 Preview Released
Tags: kinx, ruby, javascript
Published: 2021-03-10 13:38:38

Hello everybody!

I published Kinx v0.22.0 preview release! Please see [Release Page](https://github.com/Kray-G/kinx/releases) of [Kinx](https://github.com/Kray-G/kinx)

# Introduction

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

* Reference
    * First motivation ... [The post of introduction](https://dev.to/krayg/kinx-as-a-script-language-4iko)
        * 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.

There are mainly 4 updates from v0.21.0.

* [Pipeline Operator](https://github.com/Kray-G/kinx/blob/master/docs/spec/statement/expression/pipeline.md)
* [Function Composition Operator](https://github.com/Kray-G/kinx/blob/master/docs/spec/statement/expression/function_composition.md)
* [Functional Library](https://github.com/Kray-G/kinx/blob/master/docs/spec/lib/basic/functional.md)
* [Clipboard](https://github.com/Kray-G/kinx/blob/master/docs/spec/lib/basic/clipboard.md)

## 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.

```javascript
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!"
```

Please see [Pipeline Operator](https://github.com/Kray-G/kinx/blob/master/docs/spec/statement/expression/pipeline.md) 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.

```javascript
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!"
```

Please see [Function Composition Operator](https://github.com/Kray-G/kinx/blob/master/docs/spec/statement/expression/function_composition.md) 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.

```javascript
obj |> { &(obj) => obj.map({ => _1 * 2 }) }
    |> { &(obj) => obj.method() }
    |> // ... and so on.
```

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.

```javascript
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.
```

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](https://github.com/Kray-G/kinx/blob/master/docs/spec/statement/expression/pipeline.md).
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.

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

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

Please see [Functional](https://github.com/Kray-G/kinx/blob/master/docs/spec/lib/basic/functional.md) for details.

## Clipboard

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

```javascript
var cb = new Clipboard();
cb.write(str);
var r = cb.read();
```

Please see [Clipboard](https://github.com/Kray-G/kinx/blob/master/docs/spec/lib/basic/clipboard.md) 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](https://github.com/Kray-G/kinx).

Many thanks.