Example #1
0
 public void Just_Bind_ReturnsNothing()
 {
     var just = new Just<string>("4");
     var just2 = just.Bind<string>(a => new Nothing<string>());
     var result = just2.FMap<int>(a => Int32.Parse(a));
     Assert.IsInstanceOf<Nothing<int>>(result);
 }
Example #2
0
        public void Just_FMap_ReturnsNothing()
        {
            var just   = new Just <string>("Hello");
            var result = just.FMap <int>(a => Int32.Parse(a));

            Assert.IsInstanceOf <Nothing <int> >(result);
        }
Example #3
0
 public void Just_FMap_ReturnsJust()
 {
     var just = new Just<string>("42");
     var result = just.FMap<int>(a => Int32.Parse(a));
     Assert.IsInstanceOf<Just<int>>(result);
     var actualResult = result as Just<int>;
     Assert.AreEqual(42, actualResult.Value);
 }
Example #4
0
        public void throw_exception_when_function_is_null_if_has_value()
        {
            var mayBe = Just <string> .Of("some value");

            Action action = () => mayBe.IfHasValue(null);

            action.ShouldThrow <ArgumentNullException>();
        }
Example #5
0
        public void bind_with_value()
        {
            var mayBe = Just <string> .Of("some value");

            var result = mayBe.Bind(x => x.Contains("some"));

            result.ShouldBeOfType <Just <bool> >();
        }
Example #6
0
        public void Just_Bind_ReturnsNothing()
        {
            var just   = new Just <string>("4");
            var just2  = just.Bind <string>(a => new Nothing <string>());
            var result = just2.FMap <int>(a => Int32.Parse(a));

            Assert.IsInstanceOf <Nothing <int> >(result);
        }
Example #7
0
        public void execute_if_has_value()
        {
            var executed = false;
            var mayBe    = Just <string> .Of("some value");

            mayBe.IfHasValue(x => executed = true);

            executed.ShouldBeTrue();
        }
Example #8
0
        public void return_value_when_has_value()
        {
            const string value = "some value";
            var          mayBe = Just <string> .Of(value);

            var result = mayBe.ValueOr("another value");

            result.ShouldBe(value);
        }
Example #9
0
        public void throws_exception_when_function_with_Maybe_is_null()
        {
            var mayBe = Just <string> .Of("some value");

            Func <string, IMaybe <bool> > function = null;

            Action action = () => { mayBe.Bind(function); };

            action.ShouldThrow <ArgumentNullException>();
        }
Example #10
0
        public void return_value_in_bindding_with_function_with_Maybe()
        {
            var mayBe = Just <string> .Of("some value");

            Func <string, IMaybe <bool> > function = (value) => Maybe <bool> .Of(value.Contains("some"));

            var result = mayBe.Bind(function);

            result.IfHasValue(value => value.ShouldBeTrue());
        }
Example #11
0
        public void throw_exception_when_fuction_is_null_in_returning_value()
        {
            var mayBe = Just <string> .Of("some thing");

            Func <string> function = null;

            Action action = () => mayBe.ValueOr(function);

            action.ShouldThrow <ArgumentNullException>();
        }
Example #12
0
        public void Just_FMap_ReturnsJust()
        {
            var just   = new Just <string>("42");
            var result = just.FMap <int>(a => Int32.Parse(a));

            Assert.IsInstanceOf <Just <int> >(result);
            var actualResult = result as Just <int>;

            Assert.AreEqual(42, actualResult.Value);
        }
Example #13
0
 public static IEnumerable <T> Flatten <T>(this IEnumerable <Maybe <T> > sequence)
 {
     foreach (var maybe in sequence)
     {
         Just <T> just = maybe as Just <T>;
         if (just != null)
         {
             yield return(just.Value);
         }
     }
 }
Example #14
0
    void UpdateTiming()
    {
        long numSamples;
        int  tempOut;

        if (!playback_.GetNumPlayedSamples(out numSamples, out tempOut))
        {
            numSamples = -1;
        }
        isNearChanged_ = false;
        isJustChanged_ = false;
        currentSample_ = (int)numSamples;
        if (currentSample_ >= 0)
        {
            just_.Bar  = (int)(currentSample_ / samplesPerBar_);
            just_.Beat = (int)((currentSample_ - just_.Bar * samplesPerBar_) / samplesPerBeat_);
            just_.Unit = (int)((currentSample_ - just_.Bar * samplesPerBar_ - just_.Beat * samplesPerBeat_) / samplesPerUnit_);
            just_.Fix();
            if (numBlockBar_ > 0)
            {
                while (just_.Bar >= numBlockBar_)
                {
                    just_--;
                }
            }

            timeSecFromJust_ = (double)(currentSample_ - just_.Bar * samplesPerBar_ - just_.Beat * samplesPerBeat_ - just_.Unit * samplesPerUnit_) / (double)SamplingRate;
            isFormerHalf_    = (timeSecFromJust_ * SamplingRate) < samplesPerUnit_ / 2;

            near_.Copy(just_);
            if (!isFormerHalf_)
            {
                near_++;
                near_.LoopBack(numBlockBar_);
            }

            isJustChanged_ = (just_.Equals(oldJust_) == false);
            isNearChanged_ = (near_.Equals(oldNear_) == false);

            CallEvents();

            oldNear_.Copy(near_);
            oldJust_.Copy(just_);
        }

        if (DebugText != null)
        {
            DebugText.text = "Just = " + Just.ToString() + ", MusicalTime = " + MusicalTime_;
            if (BlockInfos.Count > 0)
            {
                DebugText.text += System.Environment.NewLine + "block[" + currentBlockIndex_ + "] = " + CurrentBlock_.BlockName + "(" + numBlockBar_ + "bar)";
            }
        }
    }
Example #15
0
File: Main.cs Project: dohjo/NTPR
 void _SyslogDaemon_SyslogMessageReceived(object sender, Just.EventArguments.GenericEventArgs<SyslogProtocol> e)
 {
     string[] values = new string[6]
     {
         e.Value.Header.Facility.ToString(),
         e.Value.Header.Severity.ToString(),
         e.Value.Header.Timestamp.ToString(),
         e.Value.Header.Hostname,
         e.Value.Header.Appname,
         e.Value.Message
     };
     AddToGridView(values);
 }
Example #16
0
 static Maybe<Distance> Distance(Just<Location> l1, Just<Location> l2)
 {
     var R = 6371; // Radius of the earth in km
     var dLat = Deg2rad(l1.Value.Latitude - l2.Value.Latitude);  // deg2rad below
     var dLon = Deg2rad(l1.Value.Longitude - l2.Value.Longitude);
     var a =
       Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
       Math.Cos(Deg2rad(l2.Value.Latitude)) * Math.Cos(Deg2rad(l1.Value.Longitude)) *
       Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
       ;
     var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
     var d = R * c; // Distance in km
     return new Just<Distance>(new Distance(d, "km"));
 }
Example #17
0
        public ActionResult <DialogsResponse> Post(DialogsRequest req)
        {
            _subscribeOperation.Subscribe(req.Session.UserId, "41946361");  // Дмитрий Емец
            _subscribeOperation.Subscribe(req.Session.UserId, "2222944");   // Андрей Ромашко
            _subscribeOperation.Subscribe(req.Session.UserId, "19458733");  // Степан Берёзкин
            _subscribeOperation.Subscribe(req.Session.UserId, "1152487");   // Владимир Киняйкин
            _subscribeOperation.Subscribe(req.Session.UserId, "562314067"); // Я

            var message = _dequeueOperation.Dequeue(req.Session.UserId) switch
            {
                Just <string> article => article.Value,
                _ => "Сохраняйте спокойствие и ждите новостей"
            };

            return(BuildDialogsResponse(req, message));
        }
Example #18
0
        void XPrint(int size, ConsoleColor col, string text, Just align = Just.Left)
        {
            try {
                if (text == null)
                {
                    text = "";
                }
                var x = Console.CursorLeft;
                Console.ForegroundColor = col;
                if (text.Length > size)
                {
                    Console.WriteLine(text);
                    while (Console.CursorLeft < x + size)
                    {
                        Console.Write(" ");
                    }
                }
                else
                {
                    switch (align)
                    {
                    case Just.Left:
                        Console.Write(text);
                        for (int i = text.Length; i < size; i++)
                        {
                            Console.Write(" ");
                        }
                        break;

                    case Just.Right:
                        for (int i = 0; i < size - text.Length; i++)
                        {
                            Console.Write(" ");
                        }
                        Console.Write(text);
                        break;
                    }
                }
            } catch (Exception crap) {
                QCol.QuickError(crap.Message);
                QCol.Magenta(crap.StackTrace);
                Console.Write("\n\n");
            }
        }
        public static void TestExtension()
        {
            IFunctor <int> result = new Just <int>()
            {
                Value = 1
            }
            .fmap(x => x.ToString())
            .fmap(x => int.Parse(x));

            var value = ((Just <int>)result).Value;

            Console.WriteLine(value);

            result = ((Func <int, int>)(x => x + 1)).fmap(new Just <int> {
                Value = 1
            });

            result = new Just <int> {
                Value = 1
            }.fmap(x => x + 1);

            Func <int, Func <int, int> > plus = x => y => x - y;

            result = new Just <int>()
            {
                Value = 2
            }
            .Apply(new Just <int>()
            {
                Value = 1
            }
                   .Apply(new Just <Func <int, Func <int, int> > >()
            {
                Value = plus
            }));

            value = ((Just <int>)result).Value;
            Console.WriteLine(value);

            result = new Just <int>()
            {
                Value = 1
            }
            .Apply(new Just <Func <int, Func <int, int> > >()
            {
                Value = plus
            })
            .Apply(new Just <int>()
            {
                Value = 2
            });

            value = ((Just <int>)result).Value;
            Console.WriteLine(value);

            result = new Just <int>()
            {
                Value = 1
            }
            .fmap(plus)
            .Apply(new Just <int>()
            {
                Value = 2
            });

            value = ((Just <int>)result).Value;
            Console.WriteLine(value);

            var result2 = new Just <int>()
            {
                Value = 1
            }
            .Bind(x => new Just <int> {
                Value = x + 1
            })
            .Bind(x => new Just <string>()
            {
                Value = "Value: " + x
            });

            var value2 = ((Just <string>)result2).Value;

            Console.WriteLine(value2);

            var list = new List <int> {
                1, 2, 3
            }
            .Bind(x => new List <char> {
                'a', 'b'
            }
                  .Bind(ch => new List <(int, char)> {
                (x, ch)
            }));
Example #20
0
 static void logger_LogEntryAdded(object sender, Just.EventArguments.StringEventArgs e)
 {
     Console.WriteLine(e.Value);
 }
Example #21
0
        public void return_true_when_has_value()
        {
            var mayBe = Just <string> .Of("some value");

            mayBe.HasValue.ShouldBeTrue();
        }
Example #22
0
        public Maybe <ILineNode> Execute(EAParser p, Token self, IList <IParamNode> parameters, MergeableGenerator <Token> tokens)
        {
            if (parameters[0].Type == ParamType.MACRO)
            {
                MacroInvocationNode signature = (MacroInvocationNode)(parameters[0]);
                string        name            = signature.Name;
                IList <Token> myParams        = new List <Token>();
                foreach (IList <Token> l1 in signature.Parameters)
                {
                    if (l1.Count != 1 || l1[0].Type != TokenType.IDENTIFIER)
                    {
                        p.Error(l1[0].Location, "Macro parameters must be identifiers (got " + l1[0].Content + ").");
                    }
                    else
                    {
                        myParams.Add(l1[0]);
                    }
                }

                /* if (!p.IsValidMacroName(name, myParams.Count))
                 * {
                 *  if (p.IsReservedName(name))
                 *  {
                 *      p.Error(signature.MyLocation, "Invalid redefinition: " + name);
                 *  }
                 *  else
                 *      p.Warning(signature.MyLocation, "Redefining " + name + '.');
                 * }*/
                if (p.Macros.HasMacro(name, myParams.Count))
                {
                    p.Warning(signature.MyLocation, "Redefining " + name + '.');
                }
                Maybe <IList <Token> > toRepl;
                if (parameters.Count != 2)
                {
                    toRepl = new Just <IList <Token> >(new List <Token>());
                }
                else
                {
                    toRepl = ExpandParam(p, parameters[1], myParams.Select((Token t) => t.Content));
                }
                if (!toRepl.IsNothing)
                {
                    p.Macros.AddMacro(new Macro(myParams, toRepl.FromJust), name, myParams.Count);
                }
            }
            else
            {
                //Note [mutually] recursive definitions are handled by Parser expansion.
                Maybe <string> maybeIdentifier;
                if (parameters[0].Type == ParamType.ATOM && !(maybeIdentifier = ((IAtomNode)parameters[0]).GetIdentifier()).IsNothing)
                {
                    string name = maybeIdentifier.FromJust;
                    if (p.Definitions.ContainsKey(name))
                    {
                        p.Warning(parameters[0].MyLocation, "Redefining " + name + '.');
                    }
                    if (parameters.Count == 2)
                    {
                        Maybe <IList <Token> > toRepl = ExpandParam(p, parameters[1], Enumerable.Empty <string>());
                        if (!toRepl.IsNothing)
                        {
                            p.Definitions[name] = new Definition(toRepl.FromJust);
                        }
                    }
                    else
                    {
                        p.Definitions[name] = new Definition();
                    }
                }
                else
                {
                    p.Error(parameters[0].MyLocation, "Definition names must be identifiers (got " + parameters[0].ToString() + ").");
                }
            }
            return(new Nothing <ILineNode>());
        }
Example #23
0
 /// <summary>
 /// 发送Just数据包
 /// </summary>
 /// <param name="just">数据包</param>
 public void SendJust(Just.Just just)
 {
     JustArgs args = new JustArgs();
     args.buffer = just.get();
     this.SendJust(args);
 }
Example #24
0
 static Maybe<Distance> Distance(Just<Location> a, Nothing<Location> b)
 {
     return new Nothing<Distance>();
 }
Example #25
0
 void XPrint(int size, ConsoleColor col, int whatever, Just align    = Just.Right) => XPrint(size, col, $"{whatever}", align);
Example #26
0
    void UpdateTiming()
    {
        // find section index
        int newIndex  = sectionIndex_;
        int oldSample = currentSample_;

        currentSample_ = musicSource_.timeSamples;
        if (sectionIndex_ + 1 >= Sections.Count)
        {
            if (currentSample_ < oldSample)
            {
                newIndex = 0;
            }
        }
        else
        {
            if (Sections[sectionIndex_ + 1].StartTimeSamples <= currentSample_)
            {
                newIndex = sectionIndex_ + 1;
            }
        }

        if (newIndex != sectionIndex_)
        {
            sectionIndex_ = newIndex;
            OnSectionChanged();
        }

        // calc current timing
        isNearChanged_ = false;
        isJustChanged_ = false;
        int sectionSample = currentSample_ - CurrentSection_.StartTimeSamples;

        if (sectionSample >= 0)
        {
            just_.Bar  = (int)(sectionSample / samplesPerBar_) + CurrentSection_.StartTiming.Bar;
            just_.Beat = (int)((sectionSample % samplesPerBar_) / samplesPerBeat_) + CurrentSection_.StartTiming.Beat;
            just_.Unit = (int)(((sectionSample % samplesPerBar_) % samplesPerBeat_) / samplesPerUnit_) + CurrentSection_.StartTiming.Unit;
            just_.Fix(CurrentSection_);
            if (sectionIndex_ + 1 >= Sections.Count)
            {
                if (numLoopBar_ > 0)
                {
                    while (just_.Bar >= numLoopBar_)
                    {
                        just_.Decrement(CurrentSection_);
                    }
                }
            }
            else
            {
                while (just_ >= Sections[sectionIndex_ + 1].StartTiming)
                {
                    just_.Decrement(CurrentSection_);
                }
            }

            just_.Subtract(CurrentSection_.StartTiming, CurrentSection_);
            timeSecFromJust_ = (double)(sectionSample - just_.Bar * samplesPerBar_ - just_.Beat * samplesPerBeat_ - just_.Unit * samplesPerUnit_) / (double)samplingRate_;
            isFormerHalf_    = (timeSecFromJust_ * samplingRate_) < samplesPerUnit_ / 2;
            just_.Add(CurrentSection_.StartTiming, CurrentSection_);

            near_.Copy(just_);
            if (!isFormerHalf_)
            {
                near_.Increment(CurrentSection_);
            }
            if (samplesInLoop_ != 0 && currentSample_ + samplesPerUnit_ / 2 >= samplesInLoop_)
            {
                near_.Init();
            }

            isNearChanged_ = (near_.Equals(oldNear_) == false);
            isJustChanged_ = (just_.Equals(oldJust_) == false);

            CallEvents();

            oldNear_.Copy(near_);
            oldJust_.Copy(just_);
        }

        if (DebugText != null)
        {
            DebugText.text = "Just = " + Just.ToString() + ", MusicalTime = " + MusicalTime_;
            if (Sections.Count > 0)
            {
                DebugText.text += System.Environment.NewLine + "section[" + sectionIndex_ + "] = " + CurrentSection_.ToString();
            }
        }
    }
Example #27
0
 void XPrint(int size, ConsoleColor col, object whatever, Just align = Just.Left) => XPrint(size, col, $"{whatever}", align);
Example #28
0
 public void Just_FMap_ReturnsNothing()
 {
     var just = new Just<string>("Hello");
     var result = just.FMap<int>(a => Int32.Parse(a));
     Assert.IsInstanceOf<Nothing<int>>(result);
 }
Example #29
0
 public void JustShouldReturnItsValue()
 {
     var something = new Just<string>("test");
     Assert.Equal("test", something.Return());
 }
Example #30
0
File: Main.cs Project: dohjo/NTPR
 void _Logger_StatusChanged(object sender, Just.EventArguments.StringEventArgs e)
 {
     toolStripStatusLabel.Text = e.Value;
 }
 public void Just5から値を取り出したら5が返ってくること()
 {
     var just5 = new Just<int>(5);
     Assert.That(just5.Value, Is.EqualTo(5));
     Assert.That(Just<int>.FromJust(just5), Is.EqualTo(5));
 }
Example #32
0
    void UpdateTiming()
    {
        // find section index
        int newIndex = sectionIndex_;

        if (CreateSectionClips)
        {
            newIndex       = sectionSources_.IndexOf(musicSource_);
            currentSample_ = musicSource_.timeSamples;
        }
        else
        {
            int oldSample = currentSample_;
            currentSample_ = musicSource_.timeSamples;
            if (sectionIndex_ + 1 >= Sections.Count)
            {
                if (currentSample_ < oldSample)
                {
                    newIndex = 0;
                }
            }
            else
            {
                if (Sections[sectionIndex_ + 1].StartTimeSamples <= currentSample_)
                {
                    newIndex = sectionIndex_ + 1;
                }
            }
        }

        if (newIndex != sectionIndex_)
        {
            sectionIndex_ = newIndex;
            OnSectionChanged();
        }

        // calc current timing
        isNearChanged_ = false;
        isJustChanged_ = false;
        int sectionSample = currentSample_ - (CreateSectionClips ? 0 : CurrentSection_.StartTimeSamples);

        if (sectionSample >= 0)
        {
            just_.Bar  = (int)(sectionSample / samplesPerBar_) + CurrentSection_.StartBar;
            just_.Beat = (int)((sectionSample % samplesPerBar_) / samplesPerBeat_);
            just_.Unit = (int)(((sectionSample % samplesPerBar_) % samplesPerBeat_) / samplesPerUnit_);
            just_.Fix(CurrentSection_);
            if (CreateSectionClips)
            {
                if (CurrentSection_.LoopType == Section.ClipType.Loop && numLoopBar_ > 0)
                {
                    just_.Bar -= CurrentSection_.StartBar;
                    while (just_.Bar >= numLoopBar_)
                    {
                        just_.Decrement(CurrentSection_);
                    }
                    just_.Bar += CurrentSection_.StartBar;
                }

                if (isTransitioning_ && just_.Equals(transitionTiming_))
                {
                    if (CurrentSection_.LoopType == Section.ClipType.Loop && just_.Bar == CurrentSection_.StartBar)
                    {
                        just_.Bar = CurrentSection_.StartBar + numLoopBar_;
                    }
                    just_.Decrement(CurrentSection_);
                }
            }
            else
            {
                if (sectionIndex_ + 1 >= Sections.Count)
                {
                    if (numLoopBar_ > 0)
                    {
                        while (just_.Bar >= numLoopBar_)
                        {
                            just_.Decrement(CurrentSection_);
                        }
                    }
                }
                else
                {
                    while (just_.Bar >= Sections[sectionIndex_ + 1].StartBar)
                    {
                        just_.Decrement(CurrentSection_);
                    }
                }
            }

            just_.Bar       -= CurrentSection_.StartBar;
            timeSecFromJust_ = (double)(sectionSample - just_.Bar * samplesPerBar_ - just_.Beat * samplesPerBeat_ - just_.Unit * samplesPerUnit_) / (double)samplingRate_;
            isFormerHalf_    = (timeSecFromJust_ * samplingRate_) < samplesPerUnit_ / 2;
            just_.Bar       += CurrentSection_.StartBar;

            near_.Copy(just_);
            if (!isFormerHalf_)
            {
                near_.Increment(CurrentSection_);
            }
            if (samplesInLoop_ != 0 && currentSample_ + samplesPerUnit_ / 2 >= samplesInLoop_)
            {
                near_.Init();
            }

            isNearChanged_ = (near_.Equals(oldNear_) == false);
            isJustChanged_ = (just_.Equals(oldJust_) == false);

            CallEvents();

            oldNear_.Copy(near_);
            oldJust_.Copy(just_);
        }

        if (DebugText != null)
        {
            DebugText.text = "Just = " + Just.ToString() + ", MusicalTime = " + MusicalTime_;
            if (Sections.Count > 0)
            {
                DebugText.text += System.Environment.NewLine + "section[" + sectionIndex_ + "] = " + CurrentSection_.ToString();
            }
        }
        else if (DebugPrint)
        {
            string text = "Just = " + Just.ToString() + ", MusicalTime = " + MusicalTime_;
            if (Sections.Count > 0)
            {
                text += System.Environment.NewLine + "section[" + sectionIndex_ + "] = " + CurrentSection_.ToString();
            }
            Debug.Log(text);
        }
    }
Example #33
0
 /// <summary>
 /// 发送Just数据包
 /// </summary>
 /// <param name="just">数据包</param>
 public void SendJust(Just.Just just, object obj)
 {
     JustArgs args = new JustArgs();
     args.buffer = just.get();
     args.TAG = obj;
     this.SendJust(args);
 }