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: 
42: 
43: 
44: 
45: 
open System

module Seq =

    // As Seq.filter but provide i as a parameter to the filter function.
    let filteri f s =
        s
        |> Seq.mapi (fun i elem -> i, elem)
        |> Seq.filter (fun (i, elem) -> f i elem)
        |> Seq.map (fun (_, elem) -> elem)

module Sample =

    let sample size seq =
        // Get sample length:
        let len = seq |> Seq.length
        // Handle request for sample from empty sequence:
        if size = 0 then
            Seq.empty
        else
            // Approximate an interval between samples:
            let interval = Math.Max(len / size, 1)
            // Get the sample, truncating in case rounding meant too many elements:
            seq
            |> Seq.filteri (fun i _ -> i % interval = 0)
            |> Seq.truncate size

open Sample
            
let eleven = [1..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

// seq [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]  
let test11 = eleven |> sample 11

// seq [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]  
let test12 = eleven |> sample 12
namespace System
module Seq

from Microsoft.FSharp.Collections
val filteri : f:(int -> 'a -> bool) -> s:seq<'a> -> seq<'a>

Full name: Script.Seq.filteri
val f : (int -> 'a -> bool)
val s : 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 map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val sample : size:int -> seq:seq<'a> -> seq<'a>

Full name: Script.Sample.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
Multiple items
module Seq

from Script

--------------------
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
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.Max(val1: decimal, val2: decimal) : decimal
   (+0 other overloads)
Math.Max(val1: float, val2: float) : float
   (+0 other overloads)
Math.Max(val1: float32, val2: float32) : float32
   (+0 other overloads)
Math.Max(val1: uint64, val2: uint64) : uint64
   (+0 other overloads)
Math.Max(val1: int64, val2: int64) : int64
   (+0 other overloads)
Math.Max(val1: uint32, val2: uint32) : uint32
   (+0 other overloads)
Math.Max(val1: int, val2: int) : int
   (+0 other overloads)
Math.Max(val1: uint16, val2: uint16) : uint16
   (+0 other overloads)
Math.Max(val1: int16, val2: int16) : int16
   (+0 other overloads)
Math.Max(val1: byte, val2: byte) : byte
   (+0 other overloads)
val truncate : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
module Sample

from Script
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
val test11 : seq<int>

Full name: Script.test11
val test12 : seq<int>

Full name: Script.test12

More information

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