2 people like it.

Rotate List

Rotate List

 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: 
open FsUnit
open NUnit.Framework

[<Test>]
let FsRotate() =

// splitAt available in F# 4 ...
(*
    let slice step ls = 
        let input = ls |> List.ofSeq
        match step < 0 with
        | false -> List.splitAt step input |> fun (x,y)-> List.append y x
        | _ -> List.splitAt (List.length input + step) input |> fun (x,y) -> List.append y x
*)

    let rotate step input =
        let  ls = input |> List.ofSeq
        List.fold (fun (s, c) e -> if s <> 0 then (s-1 , List.append c.Tail [e]) else (0, c)) (step, ls) ls
        |> fun (x,y) -> y |> List.ofSeq

    [1;2;3;4] |> rotate 1 |> should equal [2;3;4;1]
    "hello" |> rotate  2 |> should equal "llohe"

(*
    [1;2;3;4] |> slice 1 |> should equal [2;3;4;1]
    "hello" |> slice 2 |> should equal "llohe"
    "negative ok" |> slice -2 |> should equal "oknegative "
*)
namespace FsUnit
namespace NUnit
namespace NUnit.Framework
Multiple items
type TestAttribute =
  inherit Attribute
  new : unit -> TestAttribute
  member Description : string with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
TestAttribute() : unit
val FsRotate : unit -> unit

Full name: Script.FsRotate
val rotate : (int -> seq<'a> -> 'a list)
val step : int
val input : seq<'a>
val ls : 'a list
Multiple items
type List =
  new : unit -> List
  static member Map : actual:ICollection -> ListMapper

Full name: NUnit.Framework.List

--------------------
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<_>

--------------------
List() : unit
val ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val s : int
val c : 'a list
val e : 'a
val append : list1:'T list -> list2:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.append
property List.Tail: 'a list
val x : int
val y : 'a list
val should : f:('a -> #Constraints.Constraint) -> x:'a -> y:obj -> unit

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

Full name: FsUnit.TopLevelOperators.equal
Raw view New version

More information

Link:http://fssnip.net/qY
Posted:1 years ago
Author:Pitch Rock
Tags: rotate , list