Koan Wars: Kotlin vs TypeScript - Hamming

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Why not compare the two languages?
Koans are a good way to compare and contrast the language syntax and the different ways you might approach the problem.
Also useful if you know one of the languages and would like to get to know the other 😀
A series of Koan Battles, using Kotlin and TypeScript. We'll complete a Koan in each language and then discuss the differences.
Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

This post is the first in a series of Koan Battles, using Kotlin and TypeScript, we'll complete a Koan in both languages and then discuss the differences.
By the end of this series, you'll have a good understanding of the key features and nuances of both Kotlin and TypeScript, and be able to apply this knowledge to your own programming projects.
Whether you're a beginner or an experienced programmer, Koans provide an excellent way to learn a new language or deepen your knowledge of an existing one.
Join me as we embark on this exciting journey of discovery, enlightenment and er fighting.
Calculate the Hamming Distance between two DNA strands.
Hamming Distance is the number of positions at which the corresponding symbols in two DNA strands are different. For example:
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^
They have 7 differences, and therefore the Hamming Distance is 7.
fun compute(left: String, right: String): Int {
require(left.length == right.length)
{ "DNA strands must be of equal length" }
return left.zip(right).count { it.first != it.second }
}
export function compute(left: string, right: string): number {
if (left.length !== right.length)
throw new Error("DNA strands must be of equal length");
return [...left].filter((l, i) => l !== right[i]).length;
}
As you can see, the basic logic is the same in both languages: we check that the two DNA strands have the same length, and then we zip up the two arrays so they are easy to compare for the Hamming count.
One key difference between the Kotlin and TypeScript implementations is the way they handle errors. In Kotlin, we use the require function to check that the two strings have the same length, and if they don't, we throw an exception.
In TypeScript, we use a regular if statement to check the length of the strings, and if they don't have the same length, we throw an error using the throw keyword.
Overall, the two implementations are quite similar. It's close but I'll have to give this battle to Kotlin. That's more down to the use of standard library functions zip and count than the language. We could have gotten the TypeScript closer to the Kotlin solution with something like lodash.
Kotlin 1, TypeScript 0