예제 #1
0
파일: RxRef.cs 프로젝트: Marwan0/tlplib
        /** Returns a new ref that is bound to this ref and vice versa. **/
        public static IRxRef <B> comap <A, B>(this IRxRef <A> rx, Fn <A, B> mapper, Fn <B, A> comapper)
        {
            var bRef = RxRef.a(mapper(rx.value));

            bRef.subscribe(b => rx.value = comapper(b));
            return(bRef);
        }
예제 #2
0
파일: RxRef.cs 프로젝트: Hengle/Tetris-1
        public IRxRef <B> comap <B>(Fn <A, B> mapper, Fn <B, A> comapper)
        {
            var bRef = RxRef.a(mapper(value));

            bRef.subscribe(b => value = comapper(b));
            return(bRef);
        }
예제 #3
0
 public static ObserverBuilder <Elem, IRxRef <Elem> > builder <Elem>(Elem value)
 {
     return(builder => {
         var rxRef = new RxRef <Elem>(value);
         builder(rxRef);
         return rxRef;
     });
 }
예제 #4
0
파일: RxVal.cs 프로젝트: Hengle/Tetris-1
 public static IRxVal <A> get(A value)
 {
     return(staticCache.get(value).getOrElse(() => {
         var cached = (IRxVal <A>)RxRef.a(value);
         staticCache.Add(value, cached);
         return cached;
     }));
 }
예제 #5
0
        public static IRxVal <A> extractFuture <A>(
            this Future <IRxVal <A> > future, A whileNotCompleted
            )
        {
            var rx = RxRef.a(whileNotCompleted);

            future.onComplete(rx2 => rx2.subscribe(v => rx.value = v));
            return(rx);
        }
예제 #6
0
 public static ObserverBuilder <Elem, IRxVal <Elem> > builder <Elem>(Elem value)
 {
     // Unity Mono doesn't quite understand that it is the same type :|
     // return RxRef.builder(value);
     return(builder => {
         var rxRef = new RxRef <Elem>(value);
         builder(rxRef);
         return (IRxVal <Elem>)rxRef;
     });
 }
예제 #7
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenFromSingleItem()
        {
            var rx = RxRef.a(3.some());
            var dst = new[] { rx }.anyDefined();

            dst.value.shouldBeSome(3);
            rx.value = Option <int> .None;
            dst.value.shouldBeNone();
            rx.value = 4.some();
            dst.value.shouldBeSome(4);
        }
예제 #8
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenFromSingleItemSearchForFalse()
        {
            var rx = RxRef.a(false);
            var dst = new[] { rx }.anyOf(searchFor: false);

            dst.value.shouldBeTrue();
            rx.value = true;
            dst.value.shouldBeFalse();
            rx.value = false;
            dst.value.shouldBeTrue();
        }
예제 #9
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenFromSingleItem()
        {
            var rx = RxRef.a(3);
            var rx2 = new[] { rx }.anyThat(i => i % 2 != 0);

            rx2.value.shouldBeSome(3);
            rx.value = 2;
            rx2.value.shouldBeNone();
            rx.value = 5;
            rx2.value.shouldBeSome(5);
        }
예제 #10
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenMultipleItemsSearchForFalse()
        {
            var rx1 = RxRef.a(false);
            var rx2 = RxRef.a(true);
            var dst = new[] { rx1, rx2 }.anyOf(searchFor: false);

            dst.value.shouldBeTrue();
            rx1.value = true;
            dst.value.shouldBeFalse();
            rx2.value = false;
            dst.value.shouldBeTrue();
            rx1.value = false;
            dst.value.shouldBeTrue();
        }
예제 #11
0
파일: RxList.cs 프로젝트: Marwan0/tlplib
 public virtual IRxVal <Option <A> > rxElement(int index)
 {
     return(elementRxRefs.get(index).fold(
                () => {
         var rxRef = RxRef.a(this.get(index));
         // The cast is mono bug.
         // ArrayTypeMismatchException: Source array type cannot be assigned to destination array type.
         // (wrapper stelemref) object:stelemref (object,intptr,object)
         elementRxRefs[index] = rxRef;
         return rxRef;
     },
                _ => _
                ));
 }
예제 #12
0
        public void ctor() => describe(() => {
            var mapperInvocations        = 0;
            var actionInvocations        = 0;
            var lastActionResult         = 0;
            IRxRef <Tpl <int, int> > src = null;
            IRxVal <int> rx = null;

            beforeEach += () => {
                mapperInvocations = 0;
                actionInvocations = 0;
                src = RxRef.a(F.t(10, 0));
                rx  = new RxVal <int>(
                    -11,
                    setValue => src.subscribeWithoutEmit(tracker, t => {
                    mapperInvocations++;
                    setValue(t._1 + t._2 + 1);
                })
                    );
                rx.subscribe(tracker, i => {
                    actionInvocations++;
                    lastActionResult = i;
                });
            };

            on["creation"] = () => {
                it["should create a subscription to source"] = () => src.subscribers.shouldEqual(1);
                it["should not invoke mapper"]                = () => mapperInvocations.shouldEqual(0);
                it["should have specified value"]             = () => rx.value.shouldEqual(-11);
                it["should invoke action"]                    = () => actionInvocations.shouldEqual(1);
                it["should invoke action with current value"] = () => lastActionResult.shouldEqual(-11);

                when["source changes"] = () => {
                    beforeEach += () => src.value = F.t(2, 3);

                    it["should invoke mapper"] = () => mapperInvocations.shouldEqual(1);
                    it["should update value"]  = () => rx.value.shouldEqual(6);
                    it["should invoke action"] = () => actionInvocations.shouldEqual(2);
                    it["should invoke action with recomputed value"] = () => lastActionResult.shouldEqual(6);

                    when["source changes, but transformation result is the same"] = () => {
                        beforeEach += () => src.value = F.t(3, 2);

                        it["should invoke mapper"]       = () => mapperInvocations.shouldEqual(2);
                        it["should keep the value same"] = () => rx.value.shouldEqual(6);
                        it["should not invoke action"]   = () => actionInvocations.shouldEqual(2);
                    };
                };
            };
        });
예제 #13
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenMultipleItems()
        {
            Fn <int, bool>          predicate      = i => i % 2 != 0;
            Fn <Option <int>, bool> matchPredicate = _ => _.exists(predicate);
            var rx1 = RxRef.a(3);
            var rx2 = RxRef.a(4);
            var dst = new[] { rx1, rx2 }.anyThat(predicate);

            dst.value.shouldMatch(matchPredicate);
            rx1.value = 2;
            dst.value.shouldBeNone();
            rx2.value = 5;
            dst.value.shouldMatch(matchPredicate);
            rx1.value = 1;
            dst.value.shouldMatch(matchPredicate);
        }
예제 #14
0
파일: RxValTest.cs 프로젝트: Marwan0/tlplib
        public void WhenMultipleItems()
        {
            var rx1 = RxRef.a(3.some());
            var rx2 = RxRef.a(Option <int> .None);
            var dst = new[] { rx1, rx2 }.anyDefined();

            dst.value.shouldBeSome(3);
            rx1.value = Option <int> .None;
            dst.value.shouldBeNone();
            rx2.value = 4.some();
            dst.value.shouldBeSome(4);
            rx1.value = 1.some();
            dst.value.shouldBeAnySome();
            rx2.value = Option <int> .None;
            dst.value.shouldBeSome(1);
        }
예제 #15
0
        /** Convert an enum of rx values into one rx value using a traversal function. **/
        public static IRxVal <B> traverse <A, B>(
            this IEnumerable <IRxVal <A> > vals, Fn <IEnumerable <A>, B> traverse
            )
        {
            Fn <IEnumerable <A> > readValues = () => vals.Select(v => v.value);
            var val = RxRef.a(traverse(readValues()));

            // TODO: this is probably suboptimal.
            Act rescan = () => val.value = traverse(readValues());

            foreach (var rxVal in vals)
            {
                rxVal.subscribe(_ => rescan(), emitCurrent: false);
            }
            rescan();

            return(val);
        }
예제 #16
0
        /* Returns first value that satisfies the predicate. */
        public static IRxVal <Option <A> > firstThat <A>(this IEnumerable <IRxVal <A> > vals, Fn <A, bool> predicate)
        {
            var val = RxRef.a(F.none <A>());

            // TODO: this is probably suboptimal.
            Act rescan = () => {
                foreach (var rxVal in vals.Where(rxVal => predicate(rxVal.value)))
                {
                    val.value = F.some(rxVal.value);
                    return;
                }
                val.value = F.none <A>();
            };

            foreach (var rxVal in vals)
            {
                rxVal.subscribe(_ => rescan(), emitCurrent: false);
            }
            rescan();

            return(val);
        }
예제 #17
0
파일: RxList.cs 프로젝트: Marwan0/tlplib
 public RxList()
 {
     _rxSize = RxRef.a(0);
 }
예제 #18
0
 public static ObserverBuilder <Elem, IRxVal <Elem> > builder <Elem>(Elem value)
 {
     return(RxRef.builder(value));
 }