I’ve been trying to wrap my head around computation expressions and query expressions. Whilst playing with the latter it got me thinking about their functionality
As an example
let files = @"c:\" |> Directory.GetFiles |> Seq.map (fun s -> FileInfo s)
let projection1 = fun (x: FileInfo) -> {|Fullname = x.FullName; DirectoryName = x.DirectoryName|}
let projection2 (input: FileInfo seq) = query{
for fi in input do
select (fi.FullName, fi.DirectoryName)
}
files.Select(projection1)
files |> projection2
files |> Seq.map projection1
In this basic example, the effective same result can be achieved by using LINQ or query expressions.
But I was wondering if query expressions can do things that LINQ cannot by virtue of their design ?
One idea was early termination. i.e. being able to return early (and gracefully, without use of exceptions) from a query if at some point ?
Keen to get your thoughts ?