Serverless Kotlin with OpenFaaS

Serverless Kotlin with OpenFaaS

Cloud, Kotlin, Serverless, Tutorial
Serverless Kotlin on OpenFaaS With this article, my goal is to demonstrate how Serverless Kotlin can look like by introducing you to one of the coolest Serverless platforms: OpenFaaS. OpenFaaS is an open-source, community-owned project that you may use to run your functions and microservices on any public or private cloud. You can run your Docker image on OpenFaaS, which runs and scales it for you. As a result, you are free to choose any programming language as long as it can be packaged into a Docker image. Throughout this post, we want to learn about the concepts behind Serverless and Function as a Service (FaaS), and how we can deploy Serverless Kotlin functions to OpenFaaS. Serverless and Function as a Service Serverless Computing With Serverless Computing, we describe a…
Read More
Server as a function with Kotlin – http4k

Server as a function with Kotlin – http4k

Functional Programming, Java, Kotlin, Software Development, Tutorial, Web
Server as a function with Kotlin - http4k Have you ever heard about the concept of "Server as a Function"? The idea is that we write our server application based on just ordinary functions, which is based on a concept outlined in the paper Your Server as a Function written and published by Twitter/Marius Eriksen. In the Kotlin world, the most prominent implementation of this concept is http4k, which the maintainers describe as an "HTTP toolset written in Kotlin with a focus on creating simple, testable APIs". The best part about it is that http4k applications are just Kotlin functions that we can test straightforwardly. Take a look at this first example: First http4k server example val app: HttpHandler = { request: Request -> Response(OK).body(request.body) } val server = app.asServer(SunHttp(8000)).start()…
Read More
Default Map in Kotlin

Default Map in Kotlin

Functional Programming, Kotlin, Software Development, Teaching, Tutorial
Does Kotlin have a default Map? Have you ever used a default map or default dict before? If you know Python a bit, you probably saw its defaultdict in action at some point. Kotlin also comes with a similar tool which I want to demonstrate in this little article. You can find the Python defaultdict documentation and some examples here but the basic use case is shown in the following snippet: from collections import defaultdict d = defaultdict(int) print(d["someKey"]) //0 The defaultdict can also be used with other types and makes sure that you don't get a KeyError when running your code. Instead, it provides a default value for unknown keys, which can be really helpful for grouping and counting algorithms like the following one: from collections import defaultdict data…
Read More
Execute Kotlin Scripts with Gradle

Execute Kotlin Scripts with Gradle

Gradle, Kotlin, Software Development, Teaching, Tutorial
Organize Kotlin Scripts as Gradle tasks In this article, you will learn how you can organize multiple Kotlin scripts as Gradle tasks and make them easily executable this way. I've found a discussion about this here. Somebody wanted to execute Kotlin scripts with Gradle build scripts which is, of course, possible by using kotlinc as shown in this (Groovy) build script. This doesn't look very pretty though, and, as described in the corresponding thread, isn't very performant and manageable. Another solution would be to use Gradle scripts written with the Kotlin DSL and define custom tasks within a build.gradle.kts file, which obviously can hold and run Kotlin code naturally: // build.gradle.kts // // Execute Kotlin task with: gradle -q foo task("foo") { group = "com.kotlinexpertise" description = "my foo task"…
Read More
From Java Builders to Kotlin DSLs

From Java Builders to Kotlin DSLs

Android, Java, Kotlin, Tutorial
From Java Builders to Kotlin DSLs Introduction DSLs - Domain Specific Languages - are an ever trending topic in Kotlin circles. They allow us to flex some of the most exciting language features while accomplishing more readable and maintainable solutions in our code. Today I'd like to show you how to implement a certain kind of DSL - we're going to be wrapping an existing Java Builder in Kotlin. No doubt you've come across the builder pattern in Java before, for example if you're an Android developer, you must've used an AlertDialog.Builder, an OkHttpClient.Builder, or a Retrofit.Builder at some point. Wrapping a builder like this is a good exercise in just pure DSL design. All you have to worry about is designing the API you provide with your wrapper, since…
Read More
Android KTX – Android development with Kotlin

Android KTX – Android development with Kotlin

Android, Kotlin, Software Development, Teaching, Tutorial
Introduction Android KTX is an open source library or set of functionalities designed to make the Android development with Kotlin even more pleasant. You can find its website here. The abbreviation KTX stands for Kotlin Extensions, so this library is basically a set of extension functions, extension properties and other top-level functions. In this article, we take a look at what's inside this library and how we can take advantage of it. This library's goal is not to add new features to the existing Android APIs, but rather make those APIs easier to use by leveraging the features of the Kotlin language. Structure of Android KTX A very important thing to note at the beginning is that Android KTX provides functionalities which would be added to many individual projects by…
Read More
Kotlin Nullability Features

Kotlin Nullability Features

Functional Programming, Java, Kotlin, Software Development, Teaching, Tutorial
Null-Safe Programming - The Kotlin Way Disclaimer: This ktor article was originally published in the Dzone Java Guide 2018, which can be downloaded here. In this article, we will review the problems that may be caused by null pointers and how to avoid them in Java. After that, the article demonstrates how Kotlin nullability features work and how they improve your code. As Java developers, we're very accustomed to NullPointerExceptions (NPE) that are thrown at the runtime of an application. This almost always happens unintentionally in consequence of a bug, which is based on unrecognized references to null. The null reference is often used to indicate absent values, which isn't obvious to the programmer in many cases. Although Java relies on strong static typing, it doesn't let you distinguish between…
Read More
Why you should start contributing to StackOverflow

Why you should start contributing to StackOverflow

Kotlin, Software Development, Teaching, Tutorial
A little History A few years back, when I started getting into programming, I googled a lot of the problems I was facing during the day and mostly found my answers on StackOverflow. The place that every programmer is kind of dependent on. I still face problems and I still find my answers on StackOverflow. Something has changed, though. I don't only consume content on the website but also contribute to it. Until May 2017, I didn't even have an account on StackOverflow. At this time, I was getting interested in knowing how it's like to answer questions instead of just reading them. I enjoyed helping others to solve their Kotlin-related problems ever since I started learning the Kotlin programming language. This was my initial motivation for creating an account and…
Read More
Publish Kotlin Library on Bintray using Gradle Kotlin DSL and Travis CI

Publish Kotlin Library on Bintray using Gradle Kotlin DSL and Travis CI

Gradle, Java, Kotlin, Software Development, Tutorial
Distribute a Library on Bintray using Gradle Kotlin DSL In my latest blog post, published a few weeks back, I informed about the usage of the Gradle Kotlin DSL and how it helps with describing build scripts. In another earlier post, I introduced a small library that can be utilized for simplifying the creation of TLS/SSL sockets using a custom Kotlin DSL: SeKurity. In this post, we'll investigate how such a library can be made available to others that actually want to make use of it inside other projects. Ultimately, it should be possible to list the SeKurity library as a simple dependency in a build script like Maven or Gradle. Since the library itself is already backed by Gradle, I'll show a way of publishing the resulting artifacts at…
Read More
Kotlin Tutorial – Quick Reference – Getting Started with Kotlin

Kotlin Tutorial – Quick Reference – Getting Started with Kotlin

Functional Programming, Kotlin, Software Development, Tutorial
Introduction Disclaimer: This reference has originally been published as a DZone Refcard. Kotlin has become one of the most popular JVM languages in the past few months. One special reason is that it experienced a lot of attention in the Android community after Google made Kotlin an official language for Android development. Kotlin is being developed by JetBrains, who are responsible for the most famous IDEs out there, most notably IntelliJ IDEA. Nevertheless, it's an open source language, which can be found on GitHub. The language is said to be very concise, safe in terms of error frequency, interoperable with Java and also offers many features that enable functional programming, writing type-safe DSLs and much more. Beside the JVM, Kotlin can compile for most Android versions, down to machine code…
Read More