Saturday, April 6, 2013

Sequence has neither indexer nor slice

F# sequence is a collection, but not a list. So sequence has neither indexer nor slice (F# list has indexer, but does not have slice while C# List has both).
module UnfoldVerbosely =
    let fibSeed (current, next) =
        let yieldValue = current
        let next'current = next
        let next'next = current + next
        Some(yieldValue, (next'current, next'next))
    let fib = Seq.unfold fibSeed (1L, 1L)
> UnfoldVerbosely.fib.[0];;

  UnfoldVerbosely.fib.[0];;
  ^^^^^^^^^^^^^^^^^^^^^^^

stdin(8,1): error FS0039: The field, constructor or member 'Item' is not defined
> UnfoldVerbosely.fib.[0..2];;

  UnfoldVerbosely.fib.[0..2];;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^

stdin(9,1): error FS0039: The field, constructor or member 'GetSlice' is not defined
There are alternative ways to do as indexer or slice.
> // Instead of indexer
- UnfoldVerbosely.fib |> Seq.nth 0;;
val it : int64 = 1L
> // Instead of slice
- UnfoldVerbosely.fib |> Seq.skip 0 |> Seq.take (2-0+1);;
val it : seq<int64> = seq [1L; 1L; 2L]

No comments:

Post a Comment