Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no Seq.partition in F#

Tags:

f#

In F# we have List.partition and Array.partition which return a tuple of lists and a tuple of arrays respectively.

so, why is there no Seq.partition returning a tuple of sequences?

here is a very simple implementation: F# Snippets

so... why isn't this part of the core?

like image 460
Luiso Avatar asked Jul 31 '15 16:07

Luiso


People also ask

Is there a partition_count in *_sequence?

If it is, please let us know via a Comment In 12.1 DBMS_METADATA.GET_DDL ('SEQUENCE',....) return the undocumented clause PARTITION/NOPARTITION. The column PARTITION_COUNT exists in all *_SEQUENCE views, and in the underlying table seq$ (PARTCOUNT).

What is the output of seq pairwise and seq window?

The output is as follows. Seq.pairwise creates a new sequence in which successive elements of the input sequence are grouped into tuples. Seq.windowed is like Seq.pairwise, except that instead of producing a sequence of tuples, it produces a sequence of arrays that contain copies of adjacent elements (a window) from the sequence.

What is the function of the SEQ module in F sharp?

The Seq module in the Microsoft.FSharp.Collections namespace contains functions for working with sequences. These functions work with lists, arrays, maps, and sets as well, because all of those types are enumerable, and therefore can be treated as sequences.

What is the use of the SEQ module in Python?

The Seq module provides support for manipulations involving sequences. A sequence expression is an expression that evaluates to a sequence. Sequence expressions can take a number of forms. The simplest form specifies a range. For example, seq { 1 .. 5 } creates a sequence that contains five elements, including the endpoints 1 and 5.


1 Answers

In F# 4.0 (Visual Studio 2015), the core libraries are a lot more uniform than before, but they still do not come with an implementation of Seq.partition. You can find more about this in the F# language design discussion: Regular functional operators producing two or more output collections.

The summary is that the Seq.partition function is quite tricky and a having it could introduce potential performance issues. There a couple of ways it can work:

  • It can iterate over the input collection twice (like the FsSnip version), which can cause issues if you have complex delayed computation (you're doing everything twice)

  • It can iterate over the input once, but then it would have to do some complex mutable state sharing (which could secretly allocate memory).

So, Seq.partition cannot be implemented reasonably while keeping all the good properties that you would expect about the seq<'T> type.

like image 132
Tomas Petricek Avatar answered Oct 05 '22 09:10

Tomas Petricek