Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Now I see what "tagless" means.
It seems my solution is a hack which translates the terms into ordinary terms in ADT after I read others' solutions. 😂
It seems
compareTo
is not necessary either. I simplified it a bit. What about this then?In the
val
case its purpose can be adapted to also reduce the fraction. I believe this functional option is indeed necessary as I have explained in my first reply. So do you think it would be better if I make this function body aTODO()
?This is mainly because it's translated from the Java solution setup.
hashCode
is more a Java convention and I tested that the solution actually works even if you remove it. Theequals
issue remains in the solution setups in all other languages (C#, Haskell, Java, and Python). This is an inherited problem and if you think we should fix this maybe we should fix all other language versions first.And I don't think this is a vulnerability because this
equals()
works no matter theFraction
is reduced or not. It's just inefficient if theFraction
is always reduced.As you have said, this is your opinion. Immutability in the functional way is also an alternative opinion. See Immutability in Coding Conventions. I think both opinions should be allowed.
Let me show you the Haskell solution setup for example:
As you can see, there is a
fraction
function andinstance Eq Fraction
also allows the fraction to not be reduced.True. I had wasted a lot of time thinking what went wrong until I saw your comment. By convention, the minus sign is never placed in the denominator in a reduced fraction. I think this should be an issue rather than a suggestion.
These comments are pointless. Suggesting people to rewrite the code in a better way won't make them do so.
No, I think that you're making things uglier than they could be. Initial solution should provide a minimal setup for implementing the solution while you're providing:
hashCode
, andequalsTo
which allows the fractions to not be reduced and introduces a vulnerability as tests rely on its implementation which you do not control)IMO, it'd be much better to change everything to this:
It's true and that's why I added the following comment to the code:
Or a possible fixed and clearer version:
Your
invoke()
solution does work but IDEA shows the following warning:Besides, doing this is more tricky and less idiomatic, and it requires the coder to be more familiar with the Kotlin language and might make this Kata more difficult than necessary. I don't think this is what this Kata is intended for and there is a Tricky Kotlin collection specially intended for this.
Anyway, from what I understand, you are trying the make the solution setup simpler to understand, while I am trying the make the solution setup more cumbersome but it allows both beginner (those whose write Java-style Kotlin) and experienced Kotlin coders to work out their own solutions, both being reasonably difficult, and the latter one being more idiomatic. Both have their merits. If you insist on yours, we can make it so. Or we can decide by which one gets more votes.
But the initial solution is a
class
, notdata class
, so all this "if I implement, if I make" talk is meaningless. Either declareFraction
adata class
to do what you say and justify such design, or get rid of that function and let users do whatever they want in their code.Why can't you make you a
data class
like this?Idk the Kotlin's best practices, but this looks like a decent solution to me - it does the same thing but all the logic is defined in the class now.
See my solution and the Haskell version. If I implement the
Fraction
class to represent only reduced fractions whose both field are immutableval
s, and I also want to make the class a data class to eliminate the need to writehashCode
andequals
, this is necessary. This is more idiomatic as Kotlin promotes immutability and some other practical functional ideas. From the Haskell perspective, a data constructor is only directly called when its fields are explicitly given, and constructing data through other conversions should be defined with functions with explicit names, Kotlin is similar in this way. For example, to create aBigInteger
from as: String
,s.toBigInteger()
is preferred overBigInteger(s)
.There's no point in having a separate
fraction
function.Kotlin translation added. Please review.
https://www.codewars.com/kumite/6107bfd7bcc1ea0032702f92
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
I have thought of a quicker algorithm but there are so many cases to consider and they are realy cumbersome! I can never think of all of them without the test cases. This is basically test-driven programming.
There used to be a translations for Java, Kotlin, Dart, etc... But these languages tends to be trickier than Haskell and one cannot guarantee the users understands the purpose of this Kata (in particular the
unMaybe
function, Java users used a lot of hacks to make them work). So I think at least my attitude towards Scala would be negative, sorry.As you can see the Java, Dart, Kotlin translations are already deleted.
Loading more items...