is there a more functional way to write this code?...
# functional
h
is there a more functional way to write this code?
Copy code
fun main(args : Array<String>) {
    val input = Scanner(System.`in`)
    while (true) {
    
      var currentMax =0;
      var iWithCurrentMax =0;
        for (i in 0 until 8) {
            val mountainH = input.nextInt()
            
            if(mountainH >currentMax){
               currentMax = mountainH;
               iWithCurrentMax = i;
            }
        }

        println(iWithCurrentMax) 
    }
}
t
Looks like a fold to me
☝🏼 1
p
the while(true) may be a tailrecM
h
@tschuchort fold? Let me try that first. I am not sure about tailrecM? @pakoito not sure I have come across that before. Could you provide example?
p
It’s on Arrow, it’s a function that takes a function
(Continue) -> IO<Either<Stop, Continue>>
, where Stop is the final state, and Continue is the intermediate one, and IO is what you use to wrap the
nextInt()
safely
it isn’t easy to translate, with the limitation of 8 each time
start with the fold first
for just 8
h
Okay