Questions tagged [kotlin]

Kotlin is a statically typed programming language that compiles to JVM bytecode, JavaScript, or native binaries.

Filter by
Sorted by
Tagged with
12 votes
4 answers
309 views

The Best™ class

Have you ever wanted to get just not one "best" element from a list, but all of them, or a random element of the best ones? That's what the Best class is here for. Opinion-based? Definitely not! ...
Simon Forsberg's user avatar
10 votes
3 answers
3k views

Max heap implementation in Kotlin

I wrote this class a while ago to learn Kotlin and heaps, so any tips (on either my code style or my heap algorithm) would be appreciated! ...
Andrew Sun's user avatar
10 votes
1 answer
8k views

Event-driven finite state machine DSL in Kotlin

I wrote a DSL for defining a finite state machine. My robotics team wants to use a state machine to manage the state of the subsystems on our robot, like the arms, drive train, lifter, etc. Each ...
Roth's user avatar
  • 103
9 votes
2 answers
923 views

Swift-function which counts the letters, numbers, spaces, special chars in a given string

Task description: Write a function, which counts the letters, spaces, number and special characters in string. My implementation: ...
michael.zech's user avatar
  • 4,282
9 votes
1 answer
2k views

Arithmetic Parser in Kotlin

I don't really have a problem. But, it's my first time using Kotlin for any project, so I want to know if there is any problem in my code or my code can be made cleaner. This is an arithmetic parser ...
programmer pro's user avatar
9 votes
2 answers
390 views

Optimize a tile rendering algorithm for Swing Canvas

I have an algorithm that I use to render a text GUI using Swing's Canvas, it looks like this in practice: My goal is to reach 60 frames per second for a full HD ...
Adam Arold's user avatar
8 votes
3 answers
250 views

Produce an arithmetic progression

I have made a function which takes in an end point and a step, then produces a list of integers with the step in mind. For example, if the end point is 10 and the step is 2, the function should return ...
SpawnTheTronix's user avatar
8 votes
2 answers
341 views

Constructing a DSL with properties instead of functions, for use in a type-safe pseudo-builder

As one step in a larger builder setup, I need to build a small collection of objects, each with a small number of required attributes, and each of those with a small set of possible values. The actual ...
Mike M.'s user avatar
  • 223
8 votes
3 answers
10k views

Kotlin: Comparing enums

I am currently converting a project from Java to kotlin and re-writing the code in the process to make the most out of kotlin's capabilities. The project contains a Battle System which uses elements ...
AgentKnopf's user avatar
7 votes
3 answers
4k views

Count the number of duplicated chars in a string

It's an exercise, which I found on Codewars. Instructions: Write a function which returns the count of distinct case-insensitive alphabetic characters and numeric digits which occur more then once ...
michael.zech's user avatar
  • 4,282
7 votes
2 answers
443 views

Quiz for random boolean expressions

Inspired by the various quiz programs on this site, as well as Simon Tatham's puzzle collection, I thought I'd write a quiz that constructs its questions automatically and randomly. A typical session ...
Roland Illig's user avatar
  • 21.3k
7 votes
1 answer
210 views

Reduce amount of calls to database for authentication

I'm currently having a bit of code, which I just know can be improved a lot. I am just blind to it. All of my code looks quite neat to me, except these parts... The login + authentication process is ...
Ivaro18's user avatar
  • 173
7 votes
1 answer
138 views

Loading-content-error implementation for a tiny Kotlin library

I have a sealed interface that I use in pretty much every one of my Android projects. It implements the loading-content-error pattern, often called a Resource, <...
Eva's user avatar
  • 1,115
6 votes
1 answer
682 views

Number guessing game in Kotlin

Is this code missing any "Kotlinisms"? I've just started learning Kotlin so I'm not yet familiar with the Kotlin way of doing things. The program will guess a number you are thinking of. ...
JNB's user avatar
  • 191
6 votes
2 answers
963 views

Detect charset from raw bytes in Kotlin

I needed in my project to import .txt files without knowing their encoding, but knowing, they will most likely be in Czech or Slovak language. Sadly there is bunch of possible encodings so I decided ...
K.H.'s user avatar
  • 2,618
6 votes
1 answer
738 views

TODO app in Kotlin

I'm learning Kotlin and I made a todo program (in command line) with Kotlin and Maven as my build tool. Full project. Description The idea of my program is to read a file (path defined in a config ...
l-lin's user avatar
  • 161
6 votes
1 answer
103 views

Dangers of increasing fishing on a fish population simulation

Setup The following code implements the algorithm described in this and this paper. The first paper describes how the evolution of a fish population can be simulated, while the second paper introdues ...
Sito's user avatar
  • 384
6 votes
1 answer
340 views

Kotlin sieve using sequences

While learning about functional programming the simple sieve example was brought up in Haskell. I wanted to try a Kotlin implementation using sequences. Are there any other quick wins without ...
ersin-ertan's user avatar
6 votes
0 answers
194 views

Converting between cooking measurements

I've made an Android app that converts between cooking measurements, taking into account the type of product you use (that way it can convert between mass and volume by using the density). What are ...
Jeroen Vannevel's user avatar
5 votes
3 answers
588 views

Mapping named parameters to indices for PreparedStatement

I needed something to map named SQL parameters into indices for JDBC queries so I wrote the ParameterMap class that takes an SQL query, searchers for parameters ...
t3chb0t's user avatar
  • 44.2k
5 votes
3 answers
185 views

Performing complex operation before calling the primary constructor in Kotlin

Semester is a simple enum class. ...
KYHSGeekCode's user avatar
5 votes
2 answers
274 views

Return the indices of the two numbers such that they add up to a specific target

(...as I learn Kotlin) I want to get the indices of the two numbers such that they add up to a specific target from a given array of integers. Solution: ...
x80486's user avatar
  • 217
5 votes
1 answer
317 views

Code for sorting items in recyclerView

I'm have recyclerView which is needed for displayed for show list of my audio records. I added the ability to sort my audio records. Please take a look at the code that I use for sorting and tell me ...
Destroyer's user avatar
  • 477
5 votes
1 answer
520 views

Christmas Tree in Kotlin

Please review my code for printing a Christmas Tree. The idea was to merge some functional programming ideas (immutability, streams) and OOP (every thing is a small object). I'm looking for a review ...
Piotr Aleksander Chmielowski's user avatar
5 votes
1 answer
3k views

Calculator App with Kotlin and Android Studio

Hello I am an absolute beginner with Kotlin and Android Studio. I want to hear what I could do better for the next time or any flaws my code has. The code implements a four-function calculator (+ - ✕ ÷...
cp54lory's user avatar
5 votes
1 answer
146 views

Finding the number of pairs in an integer Array

This function returns the number of pairs of the same elements in the array It takes, array length and the array as the parameter n: Length of array ar: Array of integers for example ...
Atiq's user avatar
  • 153
5 votes
1 answer
156 views

Double LinkedList Deep Copy in Kotlin with Generics and Thread Safety

Goal Return a deep copy of a double LinkedList. Each node also contains an additional random pointer, potentially to any node or null. Code to start ...
AdamHurwitz's user avatar
5 votes
1 answer
122 views

Design that avoids undesired dependencies in Kotlin for a chess engine

I'm trying to come up with a design for a chess engine in Kotlin that hides implementation details, and that ensures that such an implementation cannot accidentally become spaghetti. Client side ...
Klaas van Aarsen's user avatar
5 votes
1 answer
454 views

Compute PI in Kotlin on a T-shirt

I have written code in Kotlin with the objective of computing Pi in few enough lines so that it looks good on a t-shirt. Can be cut and paste into http://try.kotlinlang.org under "My Programs" and ...
Stan Kurdziel's user avatar
5 votes
1 answer
1k views

Credit card / IMEI check digit in Kotlin

I recently had the need to implement Luhn's Algorithm in a Java/Kotlin application. I needed a function that would add the check digit to the string of a number like a credit card or IMEI. It could be ...
rodolfosrg's user avatar
5 votes
2 answers
977 views

Maximise adjacent numbers in a functional style (PE#11)

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the [provided] 20×20 grid? [source] This solution works, and works effectively ...
CAD97's user avatar
  • 1,944
5 votes
1 answer
483 views

Stateless 2048 Game

I created a little Kotlin clone of the 2048 game that is playable in the console. My goals were readability and statelessness for no other reason than practice. Consider it a code kata wherein I didn'...
IAE's user avatar
  • 765
5 votes
2 answers
3k views

Java/Kotlin JSON parsing improvement

I consume an API which gives me this type of JSON: ...
Ultimo_m's user avatar
  • 161
4 votes
2 answers
2k views

Compiling and throwing simple dynamic exceptions at runtime for JVM

I've been using my Dynamic Exception with C# for quite some time already and it saved me a lot of time. This means, I don't have to create a new exception ...
t3chb0t's user avatar
  • 44.2k
4 votes
3 answers
392 views

Removing repeating rows and columns from 2d array

This is a real problem I had to fix recently. Imagine having generic array where you can have duplicate columns and rows. You want to remove those duplicates as long as they are next to each other. <...
K.H.'s user avatar
  • 2,618
4 votes
2 answers
624 views

Android Tic-Tac-Toe

I have made an Android Tic-Tac-Toe adaptation as a weekend-project. Here's the Kotlin-code. MainActivity: ...
michael.zech's user avatar
  • 4,282
4 votes
2 answers
253 views

Are these Unit tests OK or am I implementing an antipattern or not following best practices

I am testing a Spring boot service using JUnit 5 and Mockito. I haven't spent much time on unit testing in the past so I'm not sure if I'm accidentally implementing an anti-pattern or not following ...
user2094257's user avatar
4 votes
2 answers
4k views

Observer pattern using a set of observers and delegated properties

The idea is to implement the observer pattern, with a separate notification method for each observed property of the observed object. Let me start with a simplified use case: ...
Thomas's user avatar
  • 208
4 votes
2 answers
240 views

Rock-Paper-Scissor-Lizard-Spock in Kotlin

I have made a simple Kotlin application called Rock-Paper-Scissor-Lizard-Spock. The application takes in user input and check if belongs to given set of array. If it belongs then it compares user ...
Kishore kichu's user avatar
4 votes
2 answers
204 views

Music Theory: The Basics - a Ring

I've recently started building an API that allows a consumer to create and manipulate musical entities such as notes, intervals, scales and chords. The first step is to create a a foundation of base ...
dfhwze's user avatar
  • 13.9k
4 votes
1 answer
879 views

Red Black Tree Implementation in Kotlin

I know that there are enough Red Black Tree implementations in the Java libraries, like e.g. java.util.TreeMap. Yet I wanted to create my own for learning purposes ...
gil.fernandes's user avatar
4 votes
3 answers
289 views

Word-guessing game

The objective of the game is to guess a five-lettered word in five guesses or less. When the user (player) enters a word, the application will inform the user whether their guess was correct or not. ...
Brad's user avatar
  • 41
4 votes
2 answers
741 views

Given an EAN-8 or an EAN-13 code, tell if that code is valid

I have the requirement to validate product codes which include EAN-8, EAN-13 and UPC-A encoded as EAN-13. I have implemented a use-case to perform this check by comparing the actual check digit with ...
Droidman's user avatar
  • 153
4 votes
1 answer
364 views

Fragment to control and show camera images

I have never tried this before. Just started with Kotlin and Android CameraX. Here is a fragment I would like some feedback on so I can writer safer and better Kotlin code. ...
El_Loco's user avatar
  • 169
4 votes
1 answer
2k views

Correct flow between Kotlin, Realm and ViewModels using Coroutines

I'm stepping back into Android after being away from it for about a year. Trying to get an out of date app of mine back on its feet, and continuing work on it. The app was written using Java, MVP, ...
BHogan's user avatar
  • 41
4 votes
1 answer
776 views

Android + Kotlin advanced color picker (HSV and RGB)

For almost the past year, I have been working on a pixel art editor for Android. My pixel art editor was relying on an external library for its color picker. For 0.2.0, I wanted to change this by ...
thebluepandabear's user avatar
4 votes
1 answer
80 views

A Concurrent Tree implementation with consistent snapshot capability

I have a tree in my application from which I need to periodically take a snapshot. I've been using locks so far, but after running into a few deadlock issues I decided to create a lock-free ...
Adam Arold's user avatar
4 votes
1 answer
244 views

Representation of data record for an employee (generic method vs design pattern)

preface: I have a part of my code that is repeated more than once and I want to create a generic method or follow a design pattern to make the code cleaner. What I'm trying to achieve is: I have a ...
Remon Shehatta's user avatar
4 votes
1 answer
143 views

Finding the type of number entered by the user

I am making a fractions calculator that can calculate fractions, whole numbers and mixed fractions. So there is a String fraction, which is the input. Also, the user will enter the fraction in a ...
DeathVenom's user avatar
4 votes
1 answer
93 views

Constructing constrained/validated objects

I'm trying to do some rudimentary DDD in validating my objects to ensure they are of a particular type. I.e. an Email object would always look like "[email protected]" ...
Druckles's user avatar
  • 176

1
2 3 4 5 6