Sounds like you're looking for takeWhile:

val takeWhile : ('T -> bool) -> seq<'T> -> seq<'T><summary>

Return a sequence that, when iterated, yields elements of the underlying sequence while the

given predicate returns 'true', and returns no further elements

For source, you'll want to download the CTP in addition to the Visual Studio 2010 Beta 1.

[link:www.microsoft.com]

There's a source directory in there.
</summary>

By on 6/15/2009 11:26 AM ()

Banshee,

Thank you also and look at my previous reply.

By on 6/15/2009 1:43 PM ()

You could replace the loop with a recursive function. Or, you could use a computation expresion: [link:tomasp.net]

I'm a little bit confused by the code though. Why have a seq expression for only one element?

I think the below code will have performance issues. But, it might not matter.

1
2
let rec first f s = if (s |> Seq.hd |> f) then Seq.hd s else first f (Seq.skip 1 s)

or:

1
2
let rec first f s = seq{ if (s |> Seq.hd |> f) then yield Seq.hd s else yield! first f (Seq.skip 1 s) }
By on 6/15/2009 11:22 AM ()

Thank you for the link, 'none'

About my code, it is just an example of possible implementation of Seq.first. And yes, it should not return a sequence! But that 's the old same "example effect', my main motivation was asking about breaking from within a loop.

However I think that your solutions fail on empty sequences.

By on 6/15/2009 1:42 PM ()

my main motivation was asking about breaking from within a loop.

See if any of these help (in order of sophistication):

By on 6/15/2009 4:40 PM ()
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