I don't understand your domain model, but you can define choosei in term of choose:

1
2
3
4
let choosei f l =
  let i = ref (-1) in
  let f' x = incr i; f !i x in
  List.choose f' l
By on 1/5/2008 2:25 PM ()

A perhaps simple solution without using mutables (which might be worth getting in the habbit of avoiding as they would break in a different situation when used with a seq that was enumerated twice) would be:

1
2
3
4
module Array =
    let choosei f = Array.mapi f >> Array.choose id
module List =
    let choosei f = List.mapi f >> List.choose id
By on 2/7/2013 5:00 AM ()

Ah, d2bg, that's very clever.

So, that snippet of code is just keeping a mutable counter (in this case, i) and incrementing it each time f' is called, which in turn calls f with the attached counter?

By on 1/5/2008 2:49 PM ()

If you can use Array2, it seems like the ideal data structure for this problem. Unfortunately, it doesn't behave nicely with the rest of the F# collections library (no of_*/to_* functions).

Pretty much how you did it is the way to go. One way to rewrite it to remove some of the convolusion is:

1
2
3
4
5
let rectangleList2 =
  [ for x in 0 .. (List.length A)
    for y in 0 .. (List.nth A x |> List.length)
    -> (x, y, List.nth (List.nth A x) y) ]
  |> List.choose AllowRectangleHere

This does exactly what you were doing, but uses a list comprehension instead of nested List.mapi/concat calls. This is similar to how Array2.map is implemented, although Array2 uses regular double-nested for loops, not a comprehension. It should be easy to turn this into a function, if you so desire.

Regards,

z.

By on 12/30/2007 9:03 PM ()

Wow, thanks z.

I'll have to look into the Array2 data structure. I keep discovering more cool (and undocumented? or at least hard-to-find) stuff :)

Thanks again.

By on 1/2/2008 8:45 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