2 people like it.

Compress List

Compress List

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
open FsUnit
open Xunit

[<Fact>]
let ``Compress List``() =

    let compress = function
        | [] -> []
        | ls -> List.fold (fun (acc:list<'a>) e -> if acc.Head = e then acc else e::acc) [ls.Head] ls.Tail |> List.rev

    [] |> compress |> should equal []
    [1; 1; 2; 3; 3; 3; 2; 2; 3] |> compress |> should equal  [1; 2; 3; 2; 3]
    [[1; 2]; [1; 2]; [3; 4]; [1; 2]] |> compress |> should equal  [[1; 2]; [3; 4]; [1; 2]]
    "Leeeeeerrroyyy" |> List.ofSeq |> compress |> should equal "Leroy"
namespace FsUnit
val ( Compress List ) : unit -> unit

Full name: Script.( Compress List )
val compress : ('a list -> 'a list) (requires equality)
val ls : 'a list (requires equality)
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 fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val acc : 'a list (requires equality)
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val e : 'a (requires equality)
property List.Head: 'a
property List.Tail: 'a list
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val should : f:('a -> #NUnit.Framework.Constraints.Constraint) -> x:'a -> y:obj -> unit

Full name: FsUnit.TopLevelOperators.should
val equal : x:'a -> EqualsConstraint

Full name: FsUnit.TopLevelOperators.equal
val ofSeq : source:seq<'T> -> 'T list

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

More information

Link:http://fssnip.net/qX
Posted:9 years ago
Author:Pitch Rock
Tags: compress , list