Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no Seq.rev function? [closed]

Tags:

f#

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?

like image 469
Trident D'Gao Avatar asked Sep 20 '13 04:09

Trident D'Gao


People also ask

What is SEQ in F#?

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.

What is the underlying difference between a sequence and a list in F #?

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 ..

What is yield in F#?

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 .)


1 Answers

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
like image 141
YotaXP Avatar answered Nov 14 '22 22:11

YotaXP