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.
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
> is not a supported character:
Name contains illegal
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
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
implementation is next.
It’s required to employ:
It’s useful for the given example that shows the operator usage:
--- operator is defined:
---is a special function name escaped within backticks “``” that denotes the “pipe operator” as described before.
- Two generics
Yare defined to represent the LHS and RHS of the operator.
infixnotation 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) -> Yis 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\).
Y—result of applying \(f\) to some \(x\).
---’s image is defined as
thisis an element of
That was the definition of the pipe operator.
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
With this, a title
String can be transformed into a
Title domain type by
piping it to the transformations declared:
- Then (in a higher-level view), applying
formatTitleto the raw
Stringinput and transforming this value into a
Titlevia the constructor
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
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.