Looks like such a made up complication to me. In order to reverse a sequence (enumerable) one have to turn it to a list first. Why is that?
Seq. groupBy takes a sequence and a function that generates a key from an element. The function is executed on each element of the sequence. Seq. groupBy returns a sequence of tuples, where the first element of each tuple is the key and the second is a sequence of elements that produce that key.
The list is created on declaration, but elements in the sequence are created as they are needed. As a result, sequences are able to represent a data structure with an arbitrary number of elements: > seq { 1I ..
F# Sequence Workflows yield and yield! (pronounced yield bang) inserts all the items of another sequence into this sequence being built. Or, in other words, it appends a sequence. (In relation to monads, it is bind .)
Since you can't walk through sequences backwards, lists are an ideal storage medium for reversing sequences. They even have the tendency of becoming reversed as you build them, go figure.
I have no idea why rev
isn't in the Seq module in the first place, though. Even LINQ has a Reverse()
operator. Fortunately, it is very easy to implement.
let rev xs = Seq.fold (fun acc x -> x::acc) [] xs
Another alternative is to just use LINQ's operator.
let rev = System.Linq.Enumerable.Reverse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With