Koan Wars: Kotlin vs TypeScript - Scrabble

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.
No comments yet. Be the first to comment.
A series of Koan Battles, using Kotlin and TypeScript. We'll complete a Koan in each language and then discuss the differences.
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 ...
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...

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 understanding of the key features and nuances of both Kotlin and TypeScript so that you can 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. Through these Koan Battles, we'll explore the similarities and differences between Kotlin and TypeScript, and discover the unique strengths of each language.
So join me as we embark on this exciting journey of discovery, enlightenment, and er fighting!
Given a word, compute the Scrabble score for that word.
You'll need these:
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
private fun scoreLetter(letter: Char): Int = when (letter) {
'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' -> 1
'D', 'G' -> 2
'B', 'C', 'M', 'P' -> 3
'F', 'H', 'V', 'W', 'Y' -> 4
'K' -> 5
'J', 'X' -> 8
'Q', 'Z' -> 10
else -> 0
}
fun score(word: String): Int =
word.uppercase().sumOf(::scoreLetter)
score FunctionThe score function takes a single argument, a string representing the word to be scored. The function first converts the entire word to uppercase using the uppercase() function to make it easier to compare each character with the rules defined in scoreLetter. The function then uses the sumOf function to calculate the total score of the word by applying the scoreLetter function to each character in the word, and summing the results.
scoreLetter FunctionThe scoreLetter function takes a single character as an argument and returns an integer value representing the point value of that character. The function uses a when expression to determine the point value of the letter based on its Scrabble score for that letter.
const SCORE_LETTER: { [key: string]: number } = {
A: 1, E: 1, I: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1,
D: 2, G: 2,
B: 3, C: 3, M: 3, P: 3,
F: 4, H: 4, V: 4, W: 4, Y: 4,
K: 5,
J: 8, X: 8,
Q: 10, Z: 10
}
export function score(word = ""): number {
return word.toUpperCase()
.split('')
.reduce((accScore, letter) =>
accScore + SCORE_LETTER[letter], 0);
}
SCORE_LETTER Map ObjectThe SCORE_LETTER object maps each letter to the number of points it is worth. For example, "A" is worth 1 point, "D" is worth 2 points, and so on.
score FunctionThe score function takes a single argument, a string representing the word to be scored. The function first converts the entire word to uppercase using the uppercase() function to make it easier to look up the score of each letter from the map defined in SCORE_LETTER.
Then, it uses the split method to convert the string of letters into an array of individual letters. It then calls the reduce method on this array to sum the score for each letter.
The main difference between the two:
Kotlin uses the when expression while TypeScript uses object mapping to score the letters.
Kotlin uses the sumOf function to get the overall score while TypeScript uses reduce.
I'm going to give this one to Kotlin, I love Kotlin's when expression and wish we had something as powerful in TypeScript. The sumOf is also easier on the eye than the reduce function. I see a pattern forming here, maybe the Kotlin standard library is just too strong!
Kotlin 2, TypeScript 0