3 people like it.

Nest items of a sequence

A function that nests items of a sequence that do not match a specified predicate under the last item that matches the predicate. The input is a sequence of values and the result is a sequence of pairs where the second element is a list of nested items.

 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: 
module List

/// Partition a list into two groups. The first contains elements 
/// until an element matching a specified predicate is found and the
/// second group contains all remaining elements.
let partitionUntil p input = 
  let rec loop acc = function
    | hd::tl when p hd -> List.rev acc, hd::tl
    | hd::tl -> loop (hd::acc) tl
    | [] -> List.rev acc, []
  loop [] input


/// A function that nests items of the input sequence 
/// that do not match a specified predicate 'f' under the 
/// last item that matches the predicate. 
let nestUnderLastMatching f input = 
  let rec loop input = seq {
    let normal, other = partitionUntil f input
    match List.rev normal with
    | last::prev ->
        for p in List.rev prev do yield p, []
        let other, rest = partitionUntil (f >> not) other
        yield last, other 
        yield! loop rest
    | [] when other = [] -> ()
    | _ -> invalidArg "" "Should start with true" }
  loop input |> List.ofSeq
Multiple items
module List

--------------------
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val partitionUntil : p:('a -> bool) -> input:'a list -> 'a list * 'a list

Full name: List.partitionUntil


 Partition a list into two groups. The first contains elements
 until an element matching a specified predicate is found and the
 second group contains all remaining elements.
val p : ('a -> bool)
val input : 'a list
val loop : ('a list -> 'a list -> 'a list * 'a list)
val acc : 'a list
val hd : 'a
val tl : 'a list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val nestUnderLastMatching : f:('a -> bool) -> input:'a list -> ('a * 'a list) list (requires equality)

Full name: List.nestUnderLastMatching


 A function that nests items of the input sequence
 that do not match a specified predicate 'f' under the
 last item that matches the predicate.
val f : ('a -> bool) (requires equality)
val input : 'a list (requires equality)
val loop : ('a list -> seq<'a * 'a list>) (requires equality)
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

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

Full name: Microsoft.FSharp.Collections.seq<_>
val normal : 'a list (requires equality)
val other : 'a list (requires equality)
val last : 'a (requires equality)
val prev : 'a list (requires equality)
val p : 'a (requires equality)
val rest : 'a list (requires equality)
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val invalidArg : argumentName:string -> message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidArg
val ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq
Raw view New version

More information

Link:http://fssnip.net/a4
Posted:5 years ago
Author:Tomas Petricek
Tags: sequence , list , collections