Hi , is there a way to convert this to map based ...
# announcements
a
Hi , is there a way to convert this to map based instead of for()
Copy code
for (it in this) {

        if (!isSendToRepairOrRejected) {
            isSendToRepairOrRejected =
                (it?.state == SubActions.SEND_FOR_REPAIR.name || it?.state == StatusName.REJECTED.name)

            result.add(
                WorkFlowStateData(
                    stateName = it?.stateName ?: "",
                    displayName = it?.displayName ?: "",
                    isCompleted = it?.status.equals(
                        S2BConst.STATUS_CLOSED,
                        true
                    ) || it?.status.equals(
                        S2BConst.STATUS_IN_PROGRESS,
                        true
                    ),
                    // TODO try to evaluate propervalue
                    currentState = true
                )
            )
        } else {
            break
        }
    }
s
let's assume
this
is
Iterable
->
Copy code
this.asSequence().map {
  (it?.state == SubActions.SEND_FOR_REPAIR.name || it?.state == StatusName.REJECTED.name)
  to
  WorkFlowStateData(...)
}.takeWhile{it.first}.map{it.second}.toList()
❤️ 1
👍 1
there might be an off-by-one error here 🤔 Your original code still accepted the first element after
isSendToRepairOrRejected
toggled to false. Was that intended?
a
yes that was intended, either way i have now stumbled at different way of doing things from your snippet, that was an eye opener for my , thank you.
👍 1
s
just for completeness, this should work. (I also fixed the negation issue)
Copy code
var takeNextElem = true

this.asSequence().map {
  (it?.state == SubActions.SEND_FOR_REPAIR.name || it?.state == StatusName.REJECTED.name)
  to
  WorkFlowStateData(...)
}.takeWhile{ (isSendToRepairOrRejected,_)->
  takeNextElem.also { 
    takeNextElem = !isSendToRepairOrRejected
  }
}.map{it.second}.toList()