package com.adealink.weparty.util

public inline fun <T> List<T>.takeLastIf(n: Int, predicate: (T) -> Boolean): List<T> {
    if (isEmpty())
        return emptyList()
    val iterator = listIterator(size)
    val list = mutableListOf<T>()
    while (iterator.hasPrevious()) {
        val previous = iterator.previous()
        if (predicate(previous)) {
            list.add(previous)
            if (list.size == n)
                break
        }
    }
    return list.reversed()
}