forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventmodule.fs
More file actions
100 lines (75 loc) · 2.79 KB
/
Copy patheventmodule.fs
File metadata and controls
100 lines (75 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Control
open Microsoft.FSharp.Core
open Microsoft.FSharp.Control
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<RequireQualifiedAccess>]
module Event =
[<CompiledName("Map")>]
let map mapping (sourceEvent: IEvent<'Delegate, 'T>) =
let ev = new Event<_>()
sourceEvent.Add(fun x -> ev.Trigger(mapping x))
ev.Publish
[<CompiledName("Filter")>]
let filter predicate (sourceEvent: IEvent<'Delegate, 'T>) =
let ev = new Event<_>()
sourceEvent.Add(fun x ->
if predicate x then
ev.Trigger x)
ev.Publish
[<CompiledName("Partition")>]
let partition predicate (sourceEvent: IEvent<'Delegate, 'T>) =
let ev1 = new Event<_>()
let ev2 = new Event<_>()
sourceEvent.Add(fun x ->
if predicate x then
ev1.Trigger x
else
ev2.Trigger x)
ev1.Publish, ev2.Publish
[<CompiledName("Choose")>]
let choose chooser (sourceEvent: IEvent<'Delegate, 'T>) =
let ev = new Event<_>()
sourceEvent.Add(fun x ->
match chooser x with
| None -> ()
| Some r -> ev.Trigger r)
ev.Publish
[<CompiledName("Scan")>]
let scan collector state (sourceEvent: IEvent<'Delegate, 'T>) =
let mutable state = state
let ev = new Event<_>()
sourceEvent.Add(fun msg ->
let z = state
let z = collector z msg
state <- z
ev.Trigger(z))
ev.Publish
[<CompiledName("Add")>]
let add callback (sourceEvent: IEvent<'Delegate, 'T>) =
sourceEvent.Add(callback)
[<CompiledName("Pairwise")>]
let pairwise (sourceEvent: IEvent<'Delegate, 'T>) : IEvent<'T * 'T> =
let ev = new Event<'T * 'T>()
let mutable lastArgs = None
sourceEvent.Add(fun args2 ->
(match lastArgs with
| None -> ()
| Some args1 -> ev.Trigger(args1, args2))
lastArgs <- Some args2)
ev.Publish
[<CompiledName("Merge")>]
let merge (event1: IEvent<'Del1, 'T>) (event2: IEvent<'Del2, 'T>) =
let ev = new Event<_>()
event1.Add(fun x -> ev.Trigger(x))
event2.Add(fun x -> ev.Trigger(x))
ev.Publish
[<CompiledName("Split")>]
let split (splitter: 'T -> Choice<'U1, 'U2>) (sourceEvent: IEvent<'Delegate, 'T>) =
let ev1 = new Event<_>()
let ev2 = new Event<_>()
sourceEvent.Add(fun x ->
match splitter x with
| Choice1Of2 y -> ev1.Trigger(y)
| Choice2Of2 z -> ev2.Trigger(z))
ev1.Publish, ev2.Publish