1 people like it.
Like the snippet!
Transform a sequence into a sequence-of-sequences
Take a sequence and make it into a sequence of sequences, where the inner sequences are of a specified length. (The last inner sequence may be shorter.)
Useful, for instance, for rending sequences into two-way HTML grids.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
|
open System
let toGrid groupSize items =
let noOfItems = items |> Seq.length
let noOfGroups = int(Math.Ceiling(float(noOfItems) / float(groupSize)))
seq { for groupNo in 0..noOfGroups-1 do
yield seq {
for tableNoInGroup in 0..groupSize-1 do
let absoluteIndex = int((groupNo * groupSize) + tableNoInGroup)
if (absoluteIndex < noOfItems) then
yield items |> Seq.nth absoluteIndex
}
}
// Examples:
let eleven = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]
// seq [seq [1; 2]; seq [3; 4]; seq [5; 6]; seq [7; 8]; ... seq [11]
let test2 = eleven |> toGrid 2
// seq [seq [1; 2; 3; 4; 5; 6]; seq [7; 8; 9; 10; 11]]
let test6 = eleven |> toGrid 6
// ASP.NET MVC3 example - produces a two-way grid of charts:
(*
@model IEnumerable<IEnumerable<myGroupedItems>>
@{
Layout = null;
}
<table>
@foreach (var group in Model) {
<tr>
@foreach (var item in group) {
<td>
@item.name
<br />
<img src= "@(Url.Action("MyChart", "MyCharts",
new { thingName = item.name, height = 100 }))" alt="chart"/>
</td>
}
</tr>
}
</table>
*)
|
namespace System
val toGrid : groupSize:int -> items:seq<'a> -> seq<seq<'a>>
Full name: Script.toGrid
val groupSize : int
val items : seq<'a>
val noOfItems : int
module Seq
from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int
Full name: Microsoft.FSharp.Collections.Seq.length
val noOfGroups : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type Math =
static val PI : float
static val E : float
static member Abs : value:sbyte -> sbyte + 6 overloads
static member Acos : d:float -> float
static member Asin : d:float -> float
static member Atan : d:float -> float
static member Atan2 : y:float * x:float -> float
static member BigMul : a:int * b:int -> int64
static member Ceiling : d:decimal -> decimal + 1 overload
static member Cos : d:float -> float
...
Full name: System.Math
Math.Ceiling(a: float) : float
Math.Ceiling(d: decimal) : decimal
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val groupNo : int
val tableNoInGroup : int
val absoluteIndex : int
val nth : index:int -> source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.nth
val eleven : int list
Full name: Script.eleven
val test2 : seq<seq<int>>
Full name: Script.test2
val test6 : seq<seq<int>>
Full name: Script.test6
More information