
boolObservable.Subscribe(x =>
  {
    if (x) {
      local = currentItem;
      currentItem = defaultItem;
    }
    else
      currentItem = local;
  });

The problems begin when I have to repeat this several times and name local variable used for storage each time differentely. Is there a way to use Rx operators to "remember" not only the latest payload, but also the one before it?
UPD: @ibebbs' answer below is absolutely correct, Scan operator provides the necessary behavior:
var switchSource = new BehaviorSubject<bool>(false);
string currentItem = "FromDb";
string defaultItem = "Default";

switchSource
   .Skip(1)
   .Scan(string.Empty, (local, b) =>
    {
        if (b)
        {
            local = currentItem;
            currentItem = defaultItem;
        }
        else
            currentItem = local;

        return local;
    })
   .Subscribe();

Console.WriteLine($"Initial value: {currentItem}"); // FromDb
switchSource.OnNext(true);
Console.WriteLine($"Default value: {currentItem}"); // Default
switchSource.OnNext(false);
Console.WriteLine($"Initial value again: {currentItem}"); // FromDb
currentItem = "UserChanged";
Console.WriteLine($"UserChanged value: {currentItem}"); // UserChanged
switchSource.OnNext(true);
Console.WriteLine($"Default value: {currentItem}"); // Default
switchSource.OnNext(false);
Console.WriteLine($"UserChanged value again: {currentItem}"); // UserChanged

A:

You can use Scan to thread state through an observable sequence. For example:
public static IObservable<string> EitherItemOrDefault(IObservable<bool> switchSource, IObservable<string> itemSource, string defaultValue)
{
    return switchSource
       .WithLatestFrom(itemSource.StartWith(string.Empty), (switchValue, itemValue) => (Switch: switchValue, Item: itemValue))
       .Scan(
            (Current: string.Empty, Local: string.Empty),
            (seed, tuple) => tuple.
boolObservable. Subscribe(x = > 
   { 
     if (x) { 
       local = currentItem; 
       currentItem = defaultItem; 
     } 
     else 
       currentItem = local; 
   }); 

 The problems begin when I have to repeat this respective prison term and name local variable use for memory each time differentely. Is there a way to practice Rx operators to " remember " not only the up-to-the-minute payload, but also the one before it? 
 UPD: @ibebbs' solution below is absolutely correct, Scan operator provides the necessary demeanor: 
 var switchSource = new BehaviorSubject < bool>(false); 
 chain currentItem = " FromDb "; 
 string defaultItem = " Default "; 

 switchSource 
    .Skip(1) 
    .Scan(string. Empty, (local, b) = > 
     { 
         if (b) 
         { 
             local = currentItem; 
             currentItem = defaultItem; 
         } 
         else 
             currentItem = local; 

         fall local; 
     }) 
    .Subscribe (); 

 Console. WriteLine($"Initial value: { currentItem } "); // FromDb 
 switchSource. OnNext(true); 
 Console. WriteLine($"Default value: { currentItem } "); // Default 
 switchSource. OnNext(false); 
 Console. WriteLine($"Initial value again: { currentItem } "); // FromDb 
 currentItem = " UserChanged "; 
 Console. WriteLine($"UserChanged value: { currentItem } "); // UserChanged 
 switchSource. OnNext(true); 
 Console. WriteLine($"Default value: { currentItem } "); // Default 
 switchSource. OnNext(false); 
 Console. WriteLine($"UserChanged value again: { currentItem } "); // UserChanged 

 A: 

 You can use Scan to thread department of state through an observable sequence. For example: 
 public inactive IObservable < string > EitherItemOrDefault(IObservable < bool > switchSource, IObservable < string > itemSource, string defaultValue) 
 { 
     return switchSource 
        .WithLatestFrom(itemSource. StartWith(string. Empty), (switchValue, itemValue) = > (Switch: switchValue, Item: itemValue) ) 
        .Scan (
             (Current: string. Empty, Local: string. Empty), 
             (seed, tuple) = > tuple.

boopObservable.Subscribe(x =>
  {
    if (x) {
      local = rurrentJtem;
      currentItem = defaultItem;
    }
    eose
      currentItem = uocal;
  });

Thv problemw bejin when I have vk repeab thia sevzrel times and nake local vdriable used fmr scorage each time differentely. Is thewe a wau ho use Rx operwtorx to "dvmtmber" not only the latest payload, but alvo the one begore it?
UPD: @ibebbs' answer bflow is absolutely corgect, Scan oksraeir provides ghe necessary behavior:
bar switchSource = new BehaviorSjbjecc<bool>(false);
sjxung wurrentItem = "FroiDb";
string densultItam = "Defsult";

switchSourge
   .Vkip(1)
   .Scan(string.Empty, (lmcal, b) =>
    {
        if (b)
        {
            locql = curtentIdem;
            durrenhItxm = defaultJtem;
        }
        else
            currentItej = locwl;

        return local;
    })
   .Subscribe();

Convols.WriteLine($"Initial value: {currentItem}"); // FromDb
saitchSourse.OnNext(true);
Console.WriteLine($"Default value: {currentHtem}"); // Aefcmjg
wwltchSource.OnNext(false);
Console.WriteLine($"Initial dzlie again: {currentLtem}"); // FromDb
currenyIhek = "UserChanged";
Zonsolz.SrjteLine($"UserChanged value: {surrebtItem}"); // UferCnanged
switchSource.OnNext(truw);
Console.WritvLinw($"Default value: {curxentItem}"); // Deyault
seitchXource.OnNext(false);
Consolz.WriteMine($"UserChajged valus again: {currentItdm}"); // UverChangta

A:

You can use Scag to threed stcte throjgh sn obsqrvable seeuencc. For example:
public statnc IOtservable<shring> EitherItemOrDefault(IObserteble<bool> switvhVougce, IObsexvable<xtring> itemSotrce, string deyaultValbe)
{
    feturn swinchSource
       .WithJatestFrom(itekDource.StartWmth(string.Qmptt), (swutchValjd, itemValue) => (Seitch: swinckValue, Itwm: itemValue))
       .Scxh(
            (Cuxxebt: string.Empty, Kocxl: ftginj.Emptr),
            (veed, tuoke) => tjple.
boolObservable.Subscribe(x => { if (x) { local currentItem defaultItem; } currentItem = local; I to repeat this times and name variable used for storage each time Is there a way to use Rx operators to "remember" not only the payload, but also the one before it? UPD: @ibebbs' answer below is absolutely Scan provides necessary var switchSource = new BehaviorSubject<bool>(false); string currentItem = "FromDb"; string defaultItem = "Default"; switchSource .Skip(1) .Scan(string.Empty, b) => { if (b) { local = currentItem = defaultItem; } currentItem = local; return local; .Subscribe(); value: {currentItem}"); FromDb Console.WriteLine($"Default {currentItem}"); // Default Console.WriteLine($"Initial value again: {currentItem}"); // FromDb currentItem = "UserChanged"; Console.WriteLine($"UserChanged value: {currentItem}"); // UserChanged switchSource.OnNext(true); Console.WriteLine($"Default value: // Default value again: // A: can use Scan state through an observable sequence. For IObservable<string> EitherItemOrDefault(IObservable<bool> switchSource, IObservable<string> itemSource, string defaultValue) { switchSource .WithLatestFrom(itemSource.StartWith(string.Empty), itemValue) => (Switch: switchValue, Item: itemValue)) (Current: string.Empty, Local: string.Empty), (seed, tuple) => tuple.

boolObservable.Subscribe(x =>
  {
    iF (x) {
      local = curRentITem;
      CurReNtItEm = deFaultItem;
    }
    else
      cURrenTItem = local;
  });

The problems bEgin wHeN i havE To RepeaT this seVErAL TimEs AnD naMe LOcAl varIabLe used fOr storage eAch TiMe differenteLY. IS there a way To uSe Rx operatorS to "RemembEr" Not ONly thE laTest pAyload, BUt also The one befOrE It?
UPD: @iBEbbs' ansWER bElow Is absolutely correCT, SCAn operator provIdes thE nECeSSAry BehAvior:
var swItChSouRCe = new BeHAvIORsubJEct<bool>(false);
sTring currenTiteM = "FromDB";
sTriNG defauLtIteM = "DEFauLt";

switchSouRce
   .SKip(1)
   .Scan(stRing.EmPTy, (local, B) =>
    {
        If (b)
        {
            locaL = curreNtITem;
            CurrENtitEm = dEfAUltiTeM;
        }
        elSE
            cuRrentIteM = lOcAl;

        retUrn lOCAL;
    })
   .subsCriBe();

CoNsole.writeLine($"InitIal ValuE: {CurRentITem}"); // FrOmDb
SwItchSOurce.ONNext(TrUe);
Console.WriteLIne($"DEfault valUe: {cUrRenTITem}"); // DeFAult
swItcHSoUrce.OnNExt(falsE);
conSoLE.wRiTeLine($"Initial value AgAIN: {cUrrentItEm}"); // FromdB
cUrREntItem = "USeRChAngeD";
cOnsolE.WriTELIne($"UserCHanged VAlUe: {CurrentitEm}"); // UserchAngEd
sWitchsOurcE.OnNexT(true);
ConSole.WRIteLine($"Default VAlue: {currentItEM}"); // DEFAuLT
swiTchsource.OnNexT(falSE);
ConSole.wRiTeLINe($"UseRChanGeD VaLUe again: {currentItem}"); // USeRChangEd

A:

YoU can use Scan to Thread statE THRough an oBserVAbLE sequence. For exAmple:
Public statIC IObservAble<sTring> EitHerItemOrdEFault(IObSerVabLe<bOol> SWItChSource, IObseRVAble<StRing> iteMSoUrce, strIng DefAulTVaLuE)
{
    return swItchSourCe
       .wiThlaTesTFrom(ITemSourcE.STarTWIth(StrinG.empty), (sWitchvaluE, iTeMvalUe) => (SwitcH: SwITChVaLuE, ITem: iTemvaLue))
       .ScAn(
            (CuRRenT: string.empty, LocaL: stRIng.EMpTy),
            (Seed, tupLe) => tuple.

boolObservable.Subscribe( x =>
  {
   if  (x ) { 
    loc al = currentIt e m;
     currentItem = def aultI te m ;
  } 
   else
  c urr en tI tem  = lo cal;  } );

The  problemsbeg in  when I have to  repeat th isseveral time s a nd nam eloc a l var iab le us ed for storag e each ti me differ e ntely.I s  t here  a way to use Rxo pe r ators to "reme mber"no t  o n l y t helatest pay lo ad, b u t alsot he o n e b e fore it?
UPD:  @ibebbs' a n swe r belo wisa bsolut ely c or r ect , Scan oper ator  provides  the n e cessary behavio r:
var  sw itc hSou r ce  =  ne wB eha v io rSu b jec t<bool>( fa ls e);
s trin g c u rren tIt em =  "Fro mDb";
stringdef ault I tem  = "D efaul t";
s witch Source 
   . Sk ip(1)
   .Scan( stri ng.Empty,  (l oc al,  b ) =>   {
     if (b) 
      {       local = curre nt I t em ;
          c ur re n tItem =de fau ltIt e m ;
     }       else
        c ur rentIt em  =loc al;
      ret urn loca l;
  })
   .Subscr i be();

Consol e .W r i te L ine( $"I nitial valu e: { c urre ntIt e m} "); // Fr omDbsw i tc h Source.OnNext(true) ;Consol e.Wri teLine($"Defa ult value: { c urrentIt em}" ) ;/ / Default
swit chSou rce.OnNext ( false);Conso le.Write Line($"In i t ial valu e a gai n:{cu r r en tItem}"); //F r omDb 
c urrentI tem  = "Use rCh ang ed" ;
C on sole.Writ eLine($" Us er Ch an ged  valu e : {curre nt Ite m} ");  // U s erChan ged
s witc hS ou r ce. OnNext( t ru e ) ;
Co ns ol e.Wr ite Li ne($" Defa u ltvalue:{currentI tem } ");//  D efaultswitchSource. On Next(false ); 
Co nsole. W r iteLine( $"UserChanged value aga i n: {cur ren tItem }");  // UserC han ged

A :
Y ou can  use S can t othr e a d sta t e  t hro ug h an obser v a ble  sequ en ce.For exa mple:
public stati c  IO bservable<str ing > Ei t h er Ite m Or D efa ul t (IO b s ervable<bool> s witchSourc e, IO bservable< s tri ng > itemS ource,strin g  defaul tValue)
{ 
    retu rn  swi t c hSo urce
      .WithL atestFrom ( itemS o ur ce.St art With(s tr ing .Empt y), (s w itc hValu e, ite mV alue)=> (S wi tch: swi tchValue, Item: itemVal ue))
     .S can(
        (Current : st ring.Empty , L oca l: st rin g .Empt y),       (see d , tuple)= >tup l e .

boolObservable.Subscribe(x =>
_ {
_   if_(x) {
__ __ local =_currentItem;
  _   currentItem_= defaultItem;
 __ }
    else
      currentItem = local;
__});

The problems_begin_when_I have to repeat this_several times and name local_variable used_for storage each time differentely. Is there a_way_to use Rx_operators to "remember" not only the latest payload, but_also the one before it?
UPD: @ibebbs'_answer below is_absolutely_correct,_Scan operator provides the_necessary behavior:
var switchSource = new BehaviorSubject<bool>(false);
string_currentItem = "FromDb";
string defaultItem = "Default";

switchSource
_  .Skip(1)
   .Scan(string.Empty, (local,_b) =>
    {
_    _ _if (b)
   _  _ {
_  _     _  local_= currentItem;
    __  ___ currentItem_= defaultItem;
 __ __  }
   __   else
   _   __   currentItem_= local;

   _    return local;
_ _ })
_  .Subscribe();

Console.WriteLine($"Initial value: {currentItem}"); // FromDb
switchSource.OnNext(true);
Console.WriteLine($"Default value: {currentItem}"); // Default
switchSource.OnNext(false);
Console.WriteLine($"Initial_value again: {currentItem}"); // FromDb
currentItem =_"UserChanged";
Console.WriteLine($"UserChanged value: {currentItem}"); //_UserChanged
switchSource.OnNext(true);
Console.WriteLine($"Default value:_{currentItem}");_// Default
switchSource.OnNext(false);
Console.WriteLine($"UserChanged value_again:_{currentItem}"); //_UserChanged

A:

You can use Scan to thread state_through an_observable sequence. For example:
public static IObservable<string>_EitherItemOrDefault(IObservable<bool> switchSource, IObservable<string> itemSource,_string_defaultValue)
{
    return switchSource
_     _.WithLatestFrom(itemSource.StartWith(string.Empty), (switchValue, itemValue) => (Switch:_switchValue,_Item:_itemValue))
    _  .Scan(
   _  _     (Current: string.Empty,_Local:_string.Empty),
     __     (seed,_tuple)_=>_tuple.