How to construct this without intermediate mutable Buffer?

Is there a way to accomplish this without using the intermediate Buffer?

	case class Folder(id: Long, name: String)
	case class FolderWithParentId(id: Long, name: String, parentId: Option[Long])

	val folderList = mutable.Buffer[FolderWithParentId]()
	List(Folder(1, "Top folder"), Folder(2, "Subfolder"))
		.scanLeft(Option.empty[Long]) { case (parentFolderId, item) =>
			folderList += FolderWithParentId(item.id, item.name, parentFolderId)
			Some(item.id)
		}

Thanks.


Andreas

ChatGPT:

val folderList = List(Folder(1, "Top folder"), Folder(2, "Subfolder"))
	.foldLeft((Option.empty[Long], List.empty[FolderWithParentId])) {
		case ((parentId, acc), folder) =>
			val folderWithParent = FolderWithParentId(folder.id, folder.name, parentId)
			(Some(folder.id), folderWithParent :: acc)
	} match {case (_, reversedResult) => reversedResult.reverse}