I am trying to work with SharePoint 2010 objects with F# (just as experiement).
So I write this code
let getSPDomainUsers (spWeb : SPWeb) =
seq {
for r in spWeb.RoleAssignments do
match r.Member with
| :? SPUser as user ->
for b in r.RoleDefinitionBindings do
if (user.IsDomainGroup) then yield (spWeb.Url.ToLower(), user, b.Name.ToLower())
| :? SPGroup as group ->
for u in group.Users do
for b in r.RoleDefinitionBindings do
if (u.IsDomainGroup) then yield (spWeb.Url.ToLower(), u, b.Name.ToLower())
| _ -> ()
}
However with all these for loops and if conditions my code looks very imperative.
instead of for loop I want to do something like List.map However most of these objects like
RoleDefinitionBindings or RoleAssignments are returning me Collections which are not Lists or Arrays or Sequences so I am forced to write loops.
Can you tell me a way in which I can avoid the loops when the return type is a custom collection object like SPRoleAssignmentCollection and SPRoleDefinitionBindingCollection.
SPRoleDefinitionBindingCollectioninheritsSPBaseCollectionwhich implementsIEnumerable(but notIEnumerable<T>, i.e.,seq<'T>). You can useSeq.castto convert instances ofIEnumerableinto aseq<'T>.Once you have a
seq<'T>you can use the usual functions in the F#Seqmodule.Here’s a cleaned-up version of your code. I don’t have SharePoint so I can’t compile/test it; you may need to explicitly cast the custom collection types to
IEnumerablebefore piping them intoSeq.cast.