Here is my problem… I don’t understand why this isn’t working for me 🙂
To be more specific I have a get files function (not the problem but feedback is welcome):
type DirectoryOptions = Directory of string * Option<SearchOption>
type SearchOptions =
| SearchSubDirectories
| SearchCurrentDirectory
let WithExtensionIn extlist filename =
let fileext = Path.GetExtension filename
extlist |> Seq.exists (fun e -> e = fileext)
let GetFiles dir extlist =
match dir with
| Some diroptions ->
let directoryname, suboptions = diroptions
match suboptions with
| Some SearchSubDirectories | None ->
Directory.GetFiles(directoryname, "*.*", SearchOption.AllDirectories)
|> Seq.filter (WithExtensionIn extlist)
| Some SearchCurrentDirectory ->
Directory.GetFiles(directoryname, "*.*", SearchOption.TopDirectoryOnly)
|> Seq.filter (WithExtensionIn extlist)
| None ->
Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*", SearchOption.AllDirectories)
|> Seq.filter (WithExtensionIn extlist)
I want to compose this into a Get duplicate files function. I may not be able to but I am trying to get my head into the functional mindset. My current attempt, which based on my understanding should work, isn’t working. This means that my understanding is wrong and I would like some help/clarification on how to solve this. My understanding is that in function composition the inner most function can have n input parameters but only one output and the remaining functions that wrap it can only have one in and one out. I’m not entirely sure how the first function is interpreted (might be a bad word to use) in the context of composition in F# because there is no distinct input/output. I believe this is the direct affect of currying.
Here is my current attempt:
let GetDuplicateFiles =
let LengthAndExtension file =
//this is faked for simplicity
(12, ".htm")
let GroupSizeGreaterThanOne group =
let _, values = group
Seq.length values > 1
let content file =
//again faked
()
let groups items =
snd items
GetFiles
>> Seq.groupBy LengthAndExtension
>> Seq.filter GroupSizeGreaterThanOne
>> Seq.collect groups
>> Seq.groupBy content
>> Seq.filter GroupSizeGreaterThanOne
>> Seq.collect groups
This gives me a compile error on Seq.groupBy LengthAndExtension
the error is The type ”b -> seq’ is not compatible with the type ‘seq<‘a>’
Any thoughts/feedback is welcome. I think I’m looking for an ah ha moment if you know what I mean
Forward-composition (
>>) creates a new function, passing the output of the first as input to the second.The signature reveals the problem:
( >> ) : ('T1 -> 'T2) -> ('T2 -> 'T3) -> 'T1 -> 'T3It accepts two functions each taking a single argument. But
GetFilestakes two args. A quick solution is to changeGetFilesto take a tuple:let GetFiles (dir, extlist) = ....