0 people like it.

Take a sample of a sequence

Take a sample of a specified length from a sequence. The sample is guaranteed to be of the requested size (unless there are too few elements in the original sequence). Sample items will be taken at equal intervals. (This version with some simplifications and tidy-ups.)

 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: 
open System

let sample size seq =
    // Get sample length:
    let len = seq |> Seq.length
    // Handle request for empty sample:
    if size = 0 then
        Seq.empty
    else
        // Handle request for sample bigger than input seq:
        if size > len then
            seq
        else
            // Approximate an interval between samples:
            let interval = int(Math.Round(float(len) / float(size)))
            // Get most of the sample - may get one too few or one to many due to rounding:
            let basic = 
                seq
                |> Seq.mapi (fun i elem -> i, elem)
                |> Seq.filter (fun pair -> let i = fst(pair)
                                           i % interval = 0)
                |> Seq.map (fun pair -> snd(pair))
            // Either shorten or add to the sequence to get the exact required sample:
            let adjusted =
                if (basic |> Seq.length) >= size then
                    basic |> Seq.truncate size
                else
                    let last = seq |> Seq.nth (len-1)
                    Seq.append basic [last]
            adjusted

let eleven = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]  

// seq [1]
let test1 = eleven |> sample 1

// seq [1; 7]
let test2 = eleven |> sample 2

// seq [1; 5; 9]
let test3 = eleven |> sample 3
namespace System
val sample : size:int -> seq:seq<'a> -> seq<'a>

Full name: Script.sample
val size : int
Multiple items
val seq : seq<'a>

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val len : int
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
val interval : 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.Round(d: decimal) : decimal
Math.Round(a: float) : float
Math.Round(d: decimal, mode: MidpointRounding) : decimal
Math.Round(d: decimal, decimals: int) : decimal
Math.Round(value: float, mode: MidpointRounding) : float
Math.Round(value: float, digits: int) : float
Math.Round(d: decimal, decimals: int, mode: MidpointRounding) : decimal
Math.Round(value: float, digits: int, mode: MidpointRounding) : float
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<_>
val basic : seq<'a>
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val i : int
val elem : 'a
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val pair : int * 'a
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val adjusted : seq<'a>
val truncate : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
val last : 'a
val nth : index:int -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.nth
val append : source1:seq<'T> -> source2:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.append
val eleven : int list

Full name: Script.eleven
val test1 : seq<int>

Full name: Script.test1
val test2 : seq<int>

Full name: Script.test2
val test3 : seq<int>

Full name: Script.test3
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/aN
Posted:9 years ago
Author:Kit Eason
Tags: sequences