https://kotlinlang.org logo
#functional
Title
# functional
h

Hexa

10/17/2019, 8:46 PM
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

tschuchort

10/17/2019, 8:48 PM
Looks like a fold to me
☝🏼 1
p

pakoito

10/17/2019, 8:48 PM
the while(true) may be a tailrecM
h

Hexa

10/17/2019, 8:54 PM
@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

pakoito

10/17/2019, 8:56 PM
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

Hexa

10/17/2019, 9:01 PM
Okay
2 Views