Kotlin / kotlinx.collections.immutable

Immutable persistent collections for Kotlin
Apache License 2.0
1.15k stars 58 forks source link

Missing overloads of stdlib functions #12

Open jmfayard opened 7 years ago

jmfayard commented 7 years ago

I tested the library with a simple TodoMVC/Redux/ImmutableJS-like exercice here

Here is my redux-like reducer

fun todoReducer(store: Store, event: Event): Store {
    val todos = when (event) {
        DeleteCompleted -> store.todos.mutate { list->
            list.removeIf(Todo::completed)
        }
        is AddTodo -> store.todos + Todo(event.id, event.text, false)
        is RemoveTodo -> {
            val todo = store.todo(event.id) ?: return store
            store.todos - todo
        }
        is ToggleCompletedTodo -> {
            val todo = store.todo(event.id) ?: return store
            store.todos.mutate { list ->
                list -= todo
                list += todo.copy(completed = !todo.completed)
            }
        }
        is EditTodo -> {
            val todo = store.todo(event.id) ?: return store
            store.todos.mutate { list ->
                list -= todo
                list += todo.copy(text = event.text)
            }
        }
    }
    return store.copy(todos = todos)
}

Had I use a List, I could have written

DeleteCompleted -> store.todos.filterNot(Todo::completed)

But that won't work because there is no overload of List.filter

DeleteCompleted -> store.todos.filterNot(Todo::completed).toImmutableList()

Not sure that this would be very efficient.

Without overloads of the stdlib extension functions, kotlinx.collections.immutable feels IMHO like a step backward

ilya-g commented 7 years ago

Relates to #10