FP in Kotlin
I’ll document particular insights about Kotlin functional designs I figure out.
This article keeps open to more sections to sub-publish.
Pipe Operator
There’s no support for the pipe operator |>
in Kotlin, so we have to come by
with a custom and clean implementation for this function.
Defining a Pipe Operator
Next, one consistent and clean implementation is given for a pipe operator in Kotlin.
Finding a Suitable Symbol
First, I wanted to use the |>
pipe symbol commonly used in functional
languages, but >
is not a supported character: Name contains illegal
characters: >
.
Then, I tried to use the other common |
pipe symbol with no full success:
Name contains characters which can cause problems on Windows: |
.
To avoid issues, I either had to design a simple symbol or use a verbose pipe
operator name.
So, I took my design from How I Standardized Hyphen and Pipe Symbols on File Names where I designed the standards for (among others) the pipe operator on file names. Notice that file systems also require simple symbols to work with, so the standard from my previous article was what I was looking for.
Now that the pipe operator symbol is established to ---
, the
implementation is next.
Language Features
It’s required to employ:
It’s useful for the given example that shows the operator usage:
Operator Definition
First, the ---
operator is defined:
Where:
---
is a special function name escaped within backticks “``” that denotes the “pipe operator” as described before.- Two generics
X
andY
are defined to represent the LHS and RHS of the operator. - The
infix
notation is used on the extension function---
defined for all \(x \in X\). There are two parts here:- The extension function.
- The infix which provides the syntax sugar.
- \(\forall x \in X, y \in Y\), the lambda
f: (X) -> Y
is defined as the operator argument. So, we have:- The constant (LHS) is given by \(X\).
- The function (RHS) to be applied is given by \(f:X \to Y\).
---
returnsY
—result of applying \(f\) to some \(x\).---
’s image is defined asf(this)
wherethis
is an element ofX
.
That was the definition of the pipe operator.
Usage Example
Now, to address a concrete example to use this new feature, I implemented a
basic DSL for an Article
domain type.
So, to test the code, I will add a user input title that is not cleaned, some content, and I’ll also define more functions with transformations, so we can pipe them.
Then, we’ll have some useful example transformations:
Finally, we can create an Article
with title
and content
:
With this, a title String
can be transformed into a Title
domain type by
piping it to the transformations declared:
- In
formatTitle
:clean
,uppercase
,markdownTitle
. - Then (in a higher-level view), applying
formatTitle
to the rawString
input and transforming this value into aTitle
via the constructortitle
:inputTitle
—formatTitle
—title
.
The program’s output is:
The example code is here.
Functional Language Design
Notice that we’re using backticks “``” to define the infix
operator like
functional languages like Purescript do and
employ this feature.
It’s also preferred to use normal identifier names (i.e., alphabetic) instead of
predefined symbols (e.g., +
) to avoid abusing the syntax123.
As said above, the pipe can be commonly denoted by |
or |>
. Since pipe
is
a universally abstract concept, it must be terse, so defining a
symbol for it is a good design. For user-specific languages, alphabetic
identifiers should be used, as said above.
Custom Pipe in Kotlin
This was the design of a custom pipe operator that can be used in Kotlin, and some insights about functional languages as well.
Options for a Pipe Operator in Kotlin
As developed before, we faced many constraints in Kotlin for getting a language design that enables us to use the pipe operator. So, we have open possibilities to add this feature to our codebase, and I developed one (“—”) that keeps consistent with the newest MathSwe standards I had defined before.
Designing Functional Languages in Kotlin
With Kotlin, DSLs can be designed via its functional language features that I keep updating in this article when I face them that build an engineering standard for this language so it can be employed for some of the development of mathematical software.