1
let SeqOf2Nodes(node1:#Node, node2:#Node) =  List.to_seq( [ (node1 :> Node); (node2 :> Node) ] )

The return type is "seq<Node>", where Node is my custom type. I am wondering if there is a way to directly make these two nodes a sequence, rather than creating a list first.

Well, a "sequence" (represented as 'seq') in F# is really just an alias for the .NET System.Collections.Generic.IEnumerable<T> interface. Since, F# lists, implement IEnumerable<T>, it's a simple type-cast:

1
2
let SeqOf2Nodes(node1 : #Node, node2 : #Node) =
  [node1; node2] :> seq<Node>

That typecast is so common that the F# libraries provide a function to perform it:

1
let seq (x : #seq<_>) = (x :> seq<_>)

So, you can just write this:

1
2
3
4
5
6
let SeqOf2Nodes(node1 : #Node, node2 : #Node) =


  seq [node1; node2]

Nothing else is needed. If for some reason, you *really* want a lazy sequence, I suppose you could use a sequence expression:

1
2
3
4
5
6
7
let SeqOf2Nodes1(node1 : #Node, node2 : #Node) =
  seq {
        yield node1
        yield node2
      }

By on 3/26/2008 3:46 PM ()

Hmm. It's not more concised, but you can use Seq.cons to avoid creating a list:

1
2
let SeqOf2Nodes(node1:#Node, node2:#Node) =
 Seq.empty |> Seq.cons (node2 :> Node) |> Seq.cons (node1 :> Node)
By on 3/26/2008 3:55 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper