I think you just need to indent farther, e.g.

1
2
3
      let fa = Worker.filter func programs 
         |> Seq.sortBy (fun p1 -> p1.ProgramId) 
         |> Seq.take numberOfItemsToReturn 

should be

1
2
3
      let fa = Worker.filter func programs 
                 |> Seq.sortBy (fun p1 -> p1.ProgramId) 
                 |> Seq.take numberOfItemsToReturn 
By on 7/24/2009 4:31 PM ()

It's possible the diagnostic here is improving between Beta1 and Beta2, on my box at work today I get the diagnostic suggested below, example hopefully clarifies indentation rules here:

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
 

// one standard indentation
let F() =
    let x = [1;2;3]
            |> List.filter (fun _ -> true)
            |> List.filter (fun _ -> true)
    x

// another allowed indentation (can 'un-dent' width of infix operator + 1, 
// so that function name is inline with previous data (e.g. 'L' below '[')
let G() =
    let x = [1;2;3]
         |> List.filter (fun _ -> true)
         |> List.filter (fun _ -> true)
    x    

// any further left will fail to parse    
let H() =
    let x = [1;2;3]
        |> List.filter (fun _ -> true) // squiggle at "|>": Unexpected infix operator in binding. Expected incomplete structured construct at or before this point or other token.
        |> List.filter (fun _ -> true)
    x        

By on 7/24/2009 4:37 PM ()

Thanks for that, it fixed that issue! Now my code looks like this...

1
2
3
4
5
6
7
8
9
10
11
static member filter f l =
        seq { for a in l -> async { return f a } }
            |> Async.Parallel
            |> Async.RunSynchronously

static member FilterAndSort_async (programs: System.Collections.Generic.List, ratingFilter: string, numberOfItemsToReturn: int) =
        let func = fun (p: Program) -> p.Rating = ratingFilter
        let fa = Worker.filter func programs
                |> Seq.sortBy (fun p -> p.ProgramId)
                |> Seq.take numberOfItemsToReturn
        fa

But this gievs me an error on the "sortBy" line saying "The field, constructor or member 'ProgramId' is not defined."

Also, this doesn't seem to be returning a list of Program objects like I'm thinking it's supposed to. In my C# code that is calling this function, I get this error...

1
"Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<bool>' to 'int'"

The C# code is this:

1
List finalResults = new List(FilterSorterFS.Worker.FilterAndSort_async(programs, ratingFilter, pageSize));

Am I on the right track here?

By on 7/27/2009 8:25 AM ()

Well, your Worker.filter function does not actually filter; rather it returns an array of bools, which explains the .ProgramId diagnostic (type 'bool' has no such field) as well as the problem from C#.

Are you using Visual Studio? If so, hover the mouse over variables and functions to see their types.

It can also be helpful to add type annotations with the types you 'expect' to help debug type issues like these, for example here if you say

let fa : seq = ...

then the diagnostic will help point towards what is wrong.

By on 7/27/2009 8:51 AM ()

Yes, I am using VS 2010 Beta 1. I'm still getting used to the syntax of F#'s intellesense, but I see now where it's returning a "bool array". I guess, let me get back to my the basic idea of what I'm trying to accomplish. I currently have this method in an F# class...

1
2
3
4
5
6
7
static member FilterAndSort (programs: System.Collections.Generic.List, ratingFilter: string, numberOfItemsToReturn: int) =
        let results =
            List.of_seq programs
            |> List.filter (fun (p: Program) -> p.Rating = ratingFilter)
            |> List.sortWith (fun (p1: Program) (p2: Program) -> compare p1.ProgramId p2.ProgramId)
            |> Seq.take numberOfItemsToReturn
        results

I want to try to make the "filtering" happen asynchronously (assuming that's possible in this case of course). Obviously (I think), I can't sort my list or "take" the number of items that I want in an asynchronous fashion!?! So the only place that I can speed up this function is at the filtering step.

Again, I'm calling this from a C# console app, purely for performance testing reasons (to see how .Net 4.0 works), and also for the learning experience. It's just very hard to find good examples of F# right now that are compatible with the VS2010 B1. It seems like most of the examples are from a CTP or ealier version, and many of the keywords and methods that they use are no longer in the language!?!

By on 7/27/2009 11:30 AM ()

So I think maybe this will help

1
2
3
4
5
static member filter f l =
        seq { for a in l -> async { if f a then return Some(a) else return None } }
            |> Async.Parallel
            |> Async.RunSynchronously
            |> Seq.choose (fun x -> x)

but I confess I haven't even tried to compile it (or think deeply about if there is an easier/better way).

By on 7/27/2009 12:56 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