-
-
Save melston/438f27a1e464be4617d4de3d963a657a to your computer and use it in GitHub Desktop.
Rx/CSharp Scan function example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Reactive.Linq; | |
using System.Text.RegularExpressions; | |
namespace SplitObservableTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<String> values = new List<string>() | |
{ | |
"1", "2", "3", | |
"a", "5", "6", "b", | |
"8", "9", "10", "11", | |
"a", "13", "14", "b", | |
"16", "17", "18", "19", | |
"a", "21", "22", "b", | |
"24" | |
}; | |
var toDel = "<del>"; | |
// If we find an 'a' then we begin marking for deletion (return 'toDel'). | |
// Once we find a 'b' we stop marking for deletion (return an emtpy string). | |
// If we are not deleting then return the value. Otherwise return acc | |
// (which should be 'toDel'). | |
Func<string, string, string> scanFunc = (acc, val) => | |
{ | |
if (val == "a") return toDel; | |
else if (val == "b") return ""; | |
else if (acc != toDel) return val; | |
else return acc; | |
}; | |
Func<string, bool> filterFunc = (val) => | |
{ | |
return val != toDel; // && val != ""; uncomment to get rid of empty lines | |
}; | |
var dd = values.ToObservable() | |
.Scan("", scanFunc) | |
.Where(filterFunc) | |
; | |
dd.Subscribe(s => Console.WriteLine(s)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment