fsprojects / FSharp.Data.Adaptive

On-demand adaptive/incremental data for F# https://fsprojects.github.io/FSharp.Data.Adaptive/
MIT License
250 stars 24 forks source link

add (Try)GetNext and (Try)GetPrev to IndexList or ChanableIndexList #63

Closed luithefirst closed 4 years ago

luithefirst commented 4 years ago

For some use cases when manipulating a ChangeableIndexList, it would be convenient to explicitly query the Next and Prev element.

My uses case are:

Currently there are only the most generic methods IndexList.TryFind(value : 'a) and IndexList.Neighbours(index : Index) and need to be used like this:

let item = 5
let list = clist [1; 5; 10]

let prev = 
    match list.Value.TryFind(item) with 
    | Some ind ->
        let (prev, _, _) = list.Value.Neighbours ind
        prev
    | None -> None

All possible combinations that query by value and by index would be: IndexList.TryGetNext(index : Index) : Index * a' option IndexList.TryGetNext(value : a') : Index * a' option IndexList.TryGetNextValue(value : a') : a' option

Or at the changeable collection itself: ChangeableIndexList.TryGetNext(index : Index) : Index * a' option ChangeableIndexList.TryGetNext(value : a') : Index * a' option ChangeableIndexList.TryGetNextValue(value : a') : a' option

The highlighted one would the perfect solution for my use cases and was previously available on ChangeableOrderedSet, but I would not want to conclude on what the most general ones are based on them.

The ChangeableIndexList could also directly provide more forwarded methods of the IndexList like TryFind.

krauthaufen commented 4 years ago

Hey, i implemented the most basic ones (IndexList.TryGetNext(index : Index) : Index * a' option) and I don't think we should add the others, since their runtime would be O(N) and they can always be expressed via IndexList.toSeqIndexed. Furthermore they're not unique since the element may occur several times in the list.

As for the ChangeableIndexList we should maybe do a full cleanup-pass of all changeable types adding lots of missing methods...