3 people like it.

'until' function inspired by Scala

Create a list of int in range [init, upper).

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let until init upper =
    Seq.initInfinite (fun x -> x + init)
    |> Seq.takeWhile (fun x -> x < upper)
    |> Seq.toList

// usage
until 1 9
|> printfn "%A"
(* #=> val it : int list = [1; 2; 3; 4; 5; 6; 7; 8] *)
val until : init:int -> upper:int -> int list

Full name: Script.until
val init : int
val upper : int
module Seq

from Microsoft.FSharp.Collections
val initInfinite : initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val x : int
val takeWhile : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.takeWhile
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/fd
Posted:12 years ago
Author:Gab_km
Tags: collection