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
How Kotlin makes me a more productive software developer

How Kotlin makes me a more productive software developer

Functional Programming, Java, Kotlin, Software Development
How Kotlin makes me a more productive software developer I've been writing JVM code for more than seven years now, and I did so mainly using Java. This changed about two years ago when I picked up Kotlin. By now, I managed to drop the Java language more-or-less entirely in favor of Kotlin. I did this because I feel much more productive with the language. It lets me focus more on the business logic rather than forcing me to write boilerplate code over and over again. In this post, I tell you how Kotlin makes me a more productive developer. I certainly know Kotlin much better than I ever knew Java. FWIW, I had been certified as a Java expert by Oracle some years back. Still, Kotlin became my native…
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
Coping with Kotlin’s Scope Functions: let, run, also, apply, with

Coping with Kotlin’s Scope Functions: let, run, also, apply, with

Functional Programming, Kotlin, Software Development
Coping with Kotlin's Scope Functions Functions in Kotlin are very important and it's much fun() to use them. One special collection of relevant functions can be described as "scope functions" and they are part of the Kotlin standard library: let, run, also, apply and with. You probably already heard about them and it's also likely that you even used some of them yet. Most people tend to have problems distinguishing all those functions, which is not very remarkable in view of the fact that their names may be a bit confusing. This post intends to demonstrate the differences between the available scope functions and also wants to discuss relevant use cases. Finally, an example will show how to apply scope functions and how they help to structure Kotlin code in…
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
The Power of Gradle Kotlin DSL

The Power of Gradle Kotlin DSL

Functional Programming, Gradle, Kotlin, Software Development, Tutorial
-The following is based on Gradle 4.3.1- A few weeks ago I started migrating most of my Groovy-based gradle.build scripts to Kotlin-backed gradle.build.kts scripts using the Kotlin DSL. Why would I do that? Kotlin is my language of choice and I love the idea of using a single language to do all my work. I never learned programming with Groovy and only know the bloody basics, which always makes me think: "This can't be the best way to do things...". Kotlin, on the other hand, is a language I use on a daily basis and therefore I know how to use the language appropriately. Additionally, Kotlin is a statically-typed language, whereas Groovy isn't. IDEs are having hard times offering code completion and error detection at compile time when a Groovy…
Read More
Web Applications with Kotlin ktor

Web Applications with Kotlin ktor

Functional Programming, Kotlin, Software Development, Web
Introduction Disclaimer: This ktor article was originally published in the Dzone Web Development Guide, which can be downloaded here. When Google made Kotlin an official language for Android a few months ago at Google I/O, the language gained much popularity in the Android world quickly. On the server side though, Kotlin is not as broadly adopted yet, and some people still seem to be cautious when backend services are involved. Other developers say that Kotlin is mature enough and we can safely use it for any server application in which Java could play a role otherwise. If you want to develop web apps with Kotlin, you can choose from various web frameworks like Spring MVC/WebFlux, Vert.x, Vaadin and basically everything available for the JVM. Besides the mentioned frameworks there's also…
Read More
Kotlin Features I miss most in Java – Kotlin vs Java

Kotlin Features I miss most in Java – Kotlin vs Java

Functional Programming, Java, Kotlin, Software Development
PLEASE find an updated version of this on my Medium: https://medium.com/@s1m0nw1/the-7-features-i-miss-most-when-going-back-to-java-after-spending-time-with-kotlin-2b3a35e0b13f. Let's write an article that covers "Kotlin vs Java" topics - I want to tell you which Kotlin features I miss most when going back to Java. My Life as a Java Dev Although I'm a big supporter of the Kotlin programming language, I still do a lot of Java programming on a daily basis for my employer. Since I'm aware of the great functionalities of Kotlin, I'm often struggling with Java as it has some "pitfalls", requires additional boilerplate and misses many features. In this post, I'd like to describe which Kotlin features I miss most when coding in Java. new and Semicolon Ever since I'm doing Kotlin, there are two things I always forget when coding in…
Read More
Kotlin Reified Types in Inline Functions

Kotlin Reified Types in Inline Functions

Functional Programming, Kotlin, Software Development, Tutorial
Kotlin Reified Types in Inline Functions I've noticed that many people haven't ever heard of reified types or have problems understanding what they are and what they do. Therefore this little post is intended to bring some light into the darkness of Kotlin's reified types. Starting situation fun <T> myGenericFun(c: Class<T>) In an ordinary generic function like myGenericFun, you can't access the type T because it is, like in Java, erased at runtime and thus only available at compile time. Therefore, if you want to use the generic type as a normal Class in the function body you need to explicitly pass the class as a parameter like the parameter c in the above example. That's correct and works fine but makes it a bit unsightly for the caller. Inlined…
Read More