Language-ext: Feature request: Extend functionality of sorted collections.

Created on 16 Oct 2019  路  6Comments  路  Source: louthy/language-ext

Over time I鈥檝e realized that I really dislike dealing with mutable collections, but there are a few features of the sorted collections in the C5 library in particular that I use a lot, and haven鈥檛 found a simple replacement for in this libraries:

  • TryPredecessor(item/key): This function returns the closest item/key-value pair in the collection which is less than the argument. If no such item is found it returns nothing. TryWeakPredecessor works identically except it looks for an item which is less than or equal to the argument. And of course TrySuccessor and TryWeakSuccessor are the same thing but the other way around.

  • RangeFromTo/Slice(min, max): Works similarly to the FindRange(keyFrom, keyTo) in Map except it returns a slice of the Map with key-value pairs intact, instead of just the values in the range. Having such a function in Set would be useful as well.

  • FindMin()/FindMax(): Returns the item/key-value pair which is sorted lowest or highest respectively.

Also I just want to say that that I am very excited to begin using this library! I鈥檝e been interested in learning functional programming for a while, and it is very appealing to me to do so within the familiarity of C#.

enhancement

Most helpful comment

@bender2k14 Map and Set are ordered. I鈥檝e already written the code for this request, just need to get the unit tests written

All 6 comments

Can you add links to the documentation of these functions in the C5 library?

Seems reasonable. I'll stick it on the list.

Can you add links to the documentation of these functions in the C5 library?

All of the mentioned functions are documented in the C5 manual from page 91 to 93. The manual can be downloaded here: https://www.itu.dk/research/c5/latest/ITU-TR-2006-76.pdf

Those methods are defined on an interface called ISorted<>. I don't recall if Language Ext contains any data structure that guarantees order like that. I just did a quick search and only found functions that return IOrderedEnumerable<>. Using that sorted order, those functions can be implemented more efficiently than if there was no guarantee about order.

On what input do you want those functions defined?

Another potential issue is that ISorted<> permits any type parameter even if that type is not comparable (such as Guid). My understanding of functional programming is that there is a general preference for using the compiler to help eliminate issues at runtime. On the other hand, Language Ext's Record<> claims that it is comparable without including any type constraint to ensure that its type constraint is comparable.

@bender2k14 Map and Set are ordered. I鈥檝e already written the code for this request, just need to get the unit tests written

@bent95 released in v3.3.29

Map<K, V> and Map<OrdK, K, V>

```c#
Map Slice(K keyFrom, K keyTo);
Option<(K Key, V Value)> FindPredecessor(K key);
Option<(K Key, V Value)> FindExactOrPredecessor(K key);
Option<(K Key, V Value)> FindSuccessor(K key);
Option<(K Key, V Value)> FindExactOrSuccessor(K key);
Option<(K Key, V Value)> Max;
Option<(K Key, V Value)> Min;

## `Set<A>` and `Set<OrdA, A>`
```c#
    IEnumerable<A> FindRange(A keyFrom, A keyTo);
    Set<A> Slice(A keyFrom, A keyTo);
    Option<A> FindPredecessor(A key);
    Option<A> FindExactOrPredecessor(A key);
    Option<A> FindSuccessor(A key);
    Option<A> FindExactOrSuccessor(A key);
    Option<A> Max;
    Option<A> Min;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

HistoricallyCorrect picture HistoricallyCorrect  路  6Comments

khtan picture khtan  路  4Comments

TysonMN picture TysonMN  路  4Comments

martasp picture martasp  路  3Comments

MrYossu picture MrYossu  路  5Comments