Skip to content

Instantly share code, notes, and snippets.

@bent-rasmussen
Created May 8, 2021 02:04
Show Gist options
  • Save bent-rasmussen/0feb70906a6358b68b485f3974beab07 to your computer and use it in GitHub Desktop.
Save bent-rasmussen/0feb70906a6358b68b485f3974beab07 to your computer and use it in GitHub Desktop.
FSharp.Data.Adaptive example in linqpad
<Query Kind="FSharpProgram">
<NuGetReference>FSharp.Data.Adaptive</NuGetReference>
<Namespace>FSharp.Data.Adaptive</Namespace>
<Namespace>FSharp.Data.Traceable</Namespace>
</Query>
// F# - what the F* is this sorcery?! :-)
// create a temporary directory
let foo = Path.Combine(Path.GetTempPath(), "foo")
let bar = Path.Combine(foo, "bar.txt")
if not (Directory.Exists(foo)) then
Directory.CreateDirectory(foo) |> ignore
// commands
let openFoo () = Process.Start("explorer", foo).Dispose()
let appendToBarTxt () = File.AppendAllLines(bar, [DateTime.Now.ToString("s", System.Globalization.CultureInfo.InvariantCulture)])
let deleteFoo () =
try
Directory.Delete(foo, true)
with ex -> ()
Util.HorizontalRun(true,
new Hyperlinq(openFoo,"Explore"),
"|",
new Hyperlinq(deleteFoo,"Delete"),
"|",
new Hyperlinq(appendToBarTxt,"AppendLine(bar.txt, now)")
) |> Dump
// calculate adaptive list of all lines in all files
let example1 () =
let dc = new DumpContainer()
dc.Dump("All lines in all files")
let content =
AdaptiveDirectory.GetFiles(foo, true)
|> ASet.sortBy (fun info -> info.Name)
|> AList.collect (fun info ->
AdaptiveFile.ReadAllLinesAList info.FullName
|> AList.map (fun line -> info.FullName + " => " + line) // cannot get line number - dunno how
)
content.AddCallback (fun oldState delta ->
let (newList, _) = IndexList.applyDelta oldState delta
dc.Content <- newList
) |> ignore
// calculate adaptive value of largest datetime from all lines in all files
let example2 () =
let dc = new DumpContainer()
dc.Dump("Largest date in all files")
let content =
AdaptiveDirectory.GetFiles(foo, true)
|> ASet.sortBy (fun info -> info.Name)
|> AList.collect (fun info -> AdaptiveFile.ReadAllLinesAList info.FullName)
|> AList.map (fun line -> DateTime.Parse(line))
|> AList.tryMax
content.AddCallback (fun value ->
dc.Content <- value
) |> ignore
// show examples
example2()
example1()
// keep adaptive computations running
Util.KeepRunning()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment