Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at an F# Trill sample #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions TrillSamples/HelloWorldFS/HelloWorldFS.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Reactive" Version="4.1.2" />
<PackageReference Include="Trill" Version="2018.12.13.2" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions TrillSamples/HelloWorldFS/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
open System
open System.Data.SqlTypes
open System.Linq.Expressions
open System.Reactive.Linq
open Microsoft.StreamProcessing

module Streamable =
open Microsoft.StreamProcessing

let ofObservable observable =
Streamable.ToStreamable(
observable,
DisorderPolicy.Throw(),
FlushPolicy.FlushOnPunctuation,
PeriodicPunctuationPolicy.None(),
OnCompletedPolicy.EndOfStream)

let join mapping left right =
Streamable.Join(left, right, fun a b -> mapping a b)

let multicast (mapping:IStreamable<'k,'a> -> IStreamable<'k,'b>) source =
Streamable.Multicast(source, mapping)

let toStreamEventObservable (source:IStreamable<Empty,'a>) =
Streamable.ToStreamEventObservable(source)

let alterEventLifetime (duration:int64) startTimeSelector source =
Streamable.AlterEventLifetime(source, (fun x -> startTimeSelector x), duration)

let filter predicate source =
Streamable.Where(source, fun e -> predicate e)

let map mapping source =
Streamable.Select(source, fun e -> mapping e)

type SensorRange = { Time: int64; Low: int; High: int }
type SensorReading = { Time: int64; Value: int }

let historicData =
[ 0; 20; 15; 30; 45; 50; 30; 35; 60; 20 ]
|> Seq.mapi (fun i v -> { Time = (int64)(i + 1); Value = v } )

let historicStream =
historicData
|> Observable.ToObservable
|> Observable.map (fun r -> StreamEvent.CreateInterval(r.Time, r.Time + 1L, r))
|> Streamable.ofObservable

let createStream isRealTime =
historicStream

let ranges threshold input =
let above = input |> Streamable.filter (fun s -> s.Value > threshold)
let below = input |> Streamable.alterEventLifetime 1L ((+) 1L) |> Streamable.filter (fun s -> s.Value < threshold)
let (|><|) = Streamable.join (fun a b -> { Time = a.Time; Low = b.Value; High = a.Value })
below |><| above

[<EntryPoint>]
let main argv =
let ranges =
createStream false
|> Streamable.multicast (ranges 42)
|> Streamable.toStreamEventObservable
Observable.ForEachAsync(ranges, fun e -> printfn "%A" e).Wait()
printfn "Done. Press ENTER to terminate"
Console.ReadLine() |> ignore
0
13 changes: 13 additions & 0 deletions TrillSamples/TrillSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryWritingGuide", "QueryWritingGuide\QueryWritingGuide.csproj", "{C4A9AC70-6189-445D-BBFA-64AB1958C8C6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FSharp", "FSharp", "{F3335D3B-A406-4FA8-B6B7-E904E28A77FA}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "HelloWorldFS", "HelloWorldFS\HelloWorldFS.fsproj", "{9B513F30-B201-4DAC-BE0D-32611D02651A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -188,6 +192,14 @@ Global
{C4A9AC70-6189-445D-BBFA-64AB1958C8C6}.Release|Any CPU.Build.0 = Release|Any CPU
{C4A9AC70-6189-445D-BBFA-64AB1958C8C6}.Release|x64.ActiveCfg = Release|Any CPU
{C4A9AC70-6189-445D-BBFA-64AB1958C8C6}.Release|x64.Build.0 = Release|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Debug|x64.ActiveCfg = Debug|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Debug|x64.Build.0 = Debug|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Release|Any CPU.Build.0 = Release|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Release|x64.ActiveCfg = Release|Any CPU
{9B513F30-B201-4DAC-BE0D-32611D02651A}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -196,6 +208,7 @@ Global
{526AC0AF-4079-4581-ADCA-36913FDEBC68} = {87014F67-F6AE-4AA6-89F2-27E0469A31B0}
{1F0E35C8-03CA-4B23-92A8-57572AD919C4} = {87014F67-F6AE-4AA6-89F2-27E0469A31B0}
{C580BAC3-23A0-42B0-804B-E882AAF55BDE} = {87014F67-F6AE-4AA6-89F2-27E0469A31B0}
{9B513F30-B201-4DAC-BE0D-32611D02651A} = {F3335D3B-A406-4FA8-B6B7-E904E28A77FA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {529E43AF-C26A-4A3A-BF6F-9C267775E28F}
Expand Down