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.
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.
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)
.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.
Thanks. I do think "Modular operations" is overranked. It takes sometime to get familiar with
Fin
and then work with its tricky definition, but the overall solution is usually not long: mine is about 40 lines and it's a lot simpler if you know themod
function as in andersk's solution. So I'd say it should be 2 kyu, for someone who knowsmod
maybe it should even be 3 kyu. The rank for "String formatting" is about right, it takes time to get used to defining functions that work on types and come with up that idea for the Kata as a learner.I am currently learning Agda. I completed the 1 kyu Kata "Modular operations on Finite data type" and then started to try this Kata becasue I haven't learned Cubical Agda but I learned about this in our math lessons so I thought this would be easier to start with. However, it took me a lot more time than expected.
I defined the Cantor pairing function and its inverse in the primitive with only addition, substraction, and recursion. Then proving the bijection using well-founded induction is a lot trouble and took a lot of time. I thought maybe I myself made the problem more complicated than it should be in this way, but I took a rough look at some other solutions and they all turned out to be just as lengthy.
So just a suggestion: maybe this Kata should be ranked 1 kyu instead of 2 kyu to be less misleading, considering that I spent much more time to complete this than a 1 kyu Kata, that all the solutions I see seem to quite lengthy, and that the total times this has been completed is actually less than 3 of the 4 currently existing 1 kyu Agda Katas. Or possibly in the other way, maybe "Modular operations on Finite data type" should be ranked down since I see some really short solutions there.
And a tip for those who think this should be easy like I did: it is not, especially if it's your first time to use well-founded induction! ðŸ˜
I'm currently learning functional programming and type theory and this has been a really great Kata to introduce me to the concept.
I took some time to add a Scala Translation. Please review.
This comment is hidden because it contains spoiler information about the solution