Home > Blockchain >  return a new list from function in a for loop to pass in the updated list
return a new list from function in a for loop to pass in the updated list

Time:02-10

I have a method that take a list as a parameter that performs some operation on it and returns the new list. However, in my for..loop I would to keep passing in the updated list until the for..loop has completed.

Is there a way to do this?

fun main(args: Array<String>) {    
    val listOfSeatRows = (1..127).toList()
    
    // Just loop until all the listOfPass has completed.
    listOfPass.forEach { seatPass ->
        val seat = Seat.valueOf(seatPass.toString())
        // I want to pass in the new updated list not the same list 
        getListOfSeatRows(listOfSeatRows, seat)
    }
}

This method takes the list and return a updated list. However, in the for..loop above I would like to pass in the list that is returned from this method

private fun getListOfSeatRows(listOfSeat: List<Int>, seatPosition: Seat): List<Int> {
    return when(seatPosition) {
        Seat.F, Seat.L -> {
           listOfSeat.windowed(listOfSeat.count() / 2).first()
        }
        Seat.B, Seat.R -> {
            listOfSeat.windowed(listOfSeat.count() / 2).last()
        }
    }
}

enum class Seat(seat: Char) {
    F('F'),
    B('B'),
    L('L'),
    R('R')
}

CodePudding user response:

Either you mutate the variable:

fun main(args: Array<String>) {    
    var listOfSeatRows = (1..127).toList()
    
    // Just loop until all the listOfPass has completed.
    listOfPass.forEach { seatPass ->
        val seat = Seat.valueOf(seatPass.toString())
        // I want to pass in the new updated list not the same list 
        listOfSeatRows = getListOfSeatRows(listOfSeatRows, seat)
    }
}

or you mutate the list:

fun main(args: Array<String>) {    
    var listOfSeatRows = (1..127).toMutableList()
    
    // Just loop until all the listOfPass has completed.
    listOfPass.forEach { seatPass ->
        val seat = Seat.valueOf(seatPass.toString())
        // I want to pass in the new updated list not the same list 
        reduceListOfSeatRows(listOfSeatRows, seat)
    }
}

private fun reduceListOfSeatRows(listOfSeat: MutableList<Int>, seatPosition: Seat) {
    val half = listOfSeat.size / 2
    when(seatPosition) {
        Seat.F, Seat.L -> {
            while (listOfSeat.size > half) listOfSeat.removeLast()
        }
        Seat.B, Seat.R -> {
            while (listOfSeat.size > half) listOfSeat.removeFirst()
        }
    }
}

If you stick with mutating the property, your function can be simplified (and avoid wasteful creation of multiple intermediate lists) using take/takeLast:

private fun getListOfSeatRows(listOfSeat: List<Int>, seatPosition: Seat): List<Int> {
    return when(seatPosition) {
        Seat.F, Seat.L -> {
            listOfSeat.take(listOfSeat.size / 2)
        }
        Seat.B, Seat.R -> {
            listOfSeat.takeLast(listOfSeat.size / 2)
        }
    }
}

CodePudding user response:

recursion maybe that's will help with some enhancement depending on your code:

var ss = 1
val listOfPass = listOf<Char>('F', 'L','B','R')

fun main(args: Array<String>) {
    val listOfSeatRows = (1..127).toList()
    val answer = getListOfSeatRows(
        listOfSeatRows,
        listOfSeatRows.count() / 2,
        Seat.valueOf(listOfPass[0].toString())
    )
    println(answer)
}

private fun getListOfSeatRows(listOfSeat: List<Int>, count: Int, seatPosition: Seat): List<Int> {
    val tempList: List<Int> = when (seatPosition) {
        Seat.F, Seat.L -> {
            listOfSeat.windowed(count).first()
        }
        Seat.B, Seat.R -> {
            listOfSeat.windowed(count).last()
        }
        else -> listOfSeat
    }
if(count == 0 || count == 1) return listOfSeat

    if (listOfPass.size > ss) {
        val seat = Seat.valueOf(listOfPass[ss  ].toString())
        return getListOfSeatRows(tempList, count / 2, seat)
    }

    return listOfSeat
}
  • Related