예제 #1
0
        public void using_bind_function_to_take_string_to_int_to_age_to_which_is_option_of_option()
        {
            // Arrange
            var i = Int.Parse("1");
            //...var age = i_.Map(i => Age.Of(i));

            var i_ = Int.Parse("1");

            // Bind : age is Option<Age>
            // i_ -> age_ ->
            //   so   1 -> Age(1)
            //   so -1  -> None
            //   so 160 -> None
            var age = i_.Bind(Age.Of);

            // map  wraps the option in an option?
            //Option<Age> ageOpt = iOpt.Bind(i => Age.Of(i));

            // Act

            // Assert
            var ac = Int.Parse("1").Bind(Age.Of).Value(); // should not really use the value function

            Assert.Equal(1, ac.Value);
        }
예제 #2
0
        public static double?GetImpactFromArgs(GraphQLField node)  //TODO: variables support
        {
            double?newImpact = null;

            if (node.Arguments != null)
            {
                if (node.Arguments.ValueFor("id") != null)
                {
                    newImpact = 1;
                }
                else
                {
                    if (node.Arguments.ValueFor("first") is GraphQLIntValue firstValue)
                    {
                        newImpact = Int.Parse(firstValue.Value);
                    }
                    else
                    {
                        if (node.Arguments.ValueFor("last") is GraphQLIntValue lastValue)
                        {
                            newImpact = Int.Parse(lastValue.Value);
                        }
                    }
                }
            }

            return(newImpact);
        }
        public void BindDemo()
        {
            // Int.Parse is a function from LaYumba. It takes a string and returns an Option<int>.
            // Int.Parse: s -> Option<int>
            var optI = Int.Parse("12");

            // Age.Of: int -> Option<Age>
            // Age.Of(1)

            // Combination with Map:
            var ageOpt = optI.Map(i => Age.Of(i));

            // Problem: returns Option<Option<Age>> ARRGH!
            var ageOpt1 = optI.Map(i => Age.Of(i));

            // Solution: Bind instead of Map when combining functions which return M<T>
            Func <string, Option <Age> > parseAge = s
                                                    => Int.Parse(s).Bind(Age.Of);

            var ageO = parseAge("12");

            ageO.Match(
                () => true.Should().Be(false), // <- ensure that this path is never called!
                x => x.Value.Should().Be(12)
                );
        }
예제 #4
0
    public AndroidVersion(string sourceVersion)
    {
        string[] tokens = sourceVersion.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
        int      major  = 1;

        if (tokens.Length > 0)
        {
            Int.Parse(tokens[0]).Match(() => major = 1, num => major = num);
        }
        int minor = 0;

        if (tokens.Length > 1)
        {
            Int.Parse(tokens[1]).Match(() => minor = 0, num => minor = num);
        }

        int versionCode = 0;

        if (tokens.Length > 2)
        {
            Int.Parse(tokens[2]).Match(() => versionCode = 0, num => versionCode = num);
        }
        Major       = major;
        Minor       = minor;
        VersionCode = versionCode;
    }
예제 #5
0
 //Button in the form named btnSolveForX
 private void btnSolveForX_Click(object sender, EventArgs e)
 {
     y         = Int.Parse(txtY.Text);
     z         = Int.Parse(txtZ.Text);
     x         = z - y; // As x + y = z -> x = z -y
     txtX.Text = z.ToString();
     PrintResultInConsole();
 }
예제 #6
0
        public void BindForOptionTest()
        {
            Func <string, Option <Age> > parseAge = s => Int.Parse(s).Bind(Age.Of);

            parseAge("26").IsSome().Should().BeTrue();
            parseAge("invalid").IsSome().Should().BeFalse();
            parseAge("180").IsSome().Should().BeFalse();
        }
예제 #7
0
        internal static void _main()
        {
            Func <string, Option <Age> > parseAge = s
                                                    => Int.Parse(s).Bind(Age.Of);

            parseAge("26");       // => Some(26)
            parseAge("notAnAge"); // => None
            parseAge("11111");    // => None
        }
예제 #8
0
파일: hom.cs 프로젝트: stefanBOK/Homework
 static void Main(string[] args)
 {
     int size = Int.Parse(Console.ReadLine());
     int[,] array =
     {
           for (int i = 0; i < size; i++)
     {
         for (int j = 0; j < size; j++)
     }
 }
        static Option <Age> ParseAge(string s)
        {
            // Apply the Age.Of function to each element of the optI (single, could be None)
            // 2 Option types combined give us this problem to work with
            //Option<int> optI = Int.Parse(s);
            //Option<Option<Age>> ageOpt = optI.Map(x => Age.Of(x));

            // Using Bind to chain two functions that return Option so we get a flattened Option<age>
            return(Int.Parse(s)
                   .Bind(x => Age.Of(x)));
        }
예제 #10
0
 public LegalPerson(string cnpj, string corporateName, string fantasyName, string stateRegistration, DateTime openingDate)
 {
     this.Partner           = new List <PhysicalPerson>();
     this.Cnpj              = Int.Parse(cnpj);
     this.CorporateName     = corporateName;
     this.FantasyName       = fantasyName;
     this.StateRegistration = Int.Parse(stateRegistration);
     this.OpeningDate       = openingDate;
     this.Age      = new Random().Next(1, 70);
     this.Revenues = new Random().NextBytes(3000, 10000);
 }
예제 #11
0
        public static void Main(string[] args)
        {
            Alumnos al        = new Alumnos();
            int     registros = 0;



            Console.WriteLine("Elige ");
            Console.WriteLine("1.Agrega tupla");
            Console.WriteLine("2.Consultar tuplas.");


            int con = Int32.Parse(Console.ReadLine());

            switch (con)

            {
            case 1:

                int codigo = 0;
                Console.WriteLine("dame el codigo del Alumno:");
                codigo = int.Parse(Console.ReadLine());


                Console.WriteLine("dame el nombre del Alumno:");
                String nombre   = Console.ReadLine();
                int    telefono = 0;

                Console.WriteLine("Ingrese el TELÉFONO del Alumno:");
                telefono = Int.Parse(Console.ReadLine());


                Console.WriteLine("Ingrese el E-MAIL del Alumno:");
                String email = Console.ReadLine();


                al.insertarRegistroNuevo(codigo, nombre, telefono, email);
                break;


            case 2:

                al.mostrarTodos();

                break;
            }

            Console.ReadKey(true);
        }
예제 #12
0
        public void S44_Filter_values_with_where()
        {
            // Arrange
            bool IsNatural(int i) => i > 0;

            Option <int> ToNatural(string s) =>
            Int.Parse(s).Where(IsNatural);

            // Act

            // Assert
            Assert.Equal(None, ToNatural("nOT_A numb3r"));
            Assert.Equal(None, ToNatural("-1"));
            Assert.Equal(Some(1), ToNatural("1"));
        }
예제 #13
0
        public void using_map_function_to_take_string_to_int_to_age_to_which_is_option_of_option()
        {
            // Arrange
            var i_   = Int.Parse("1");
            var age_ = i_.Map(i => Age.Of(i));

            Option <int> iOpt = Int.Parse("1");

            // map  wraps the option in an option?
            Option <Option <Age> > ageOptOpt = iOpt.Map(i => Age.Of(i));

            // Act

            // Assert
            var acOpt = ageOptOpt = ageOptOpt.Match(() => null, t => t);
            // ...
        }
        public static void Run()
        {
            Console.WriteLine("hello!");
            //var result = CalculateRiskProfile(new Age(20));
            // this isn't going to work as we need to handle the None case in the Option<Age>
            //var result = CalculateRiskProfile(Age.Of(20));

            // regular variable pointing to a function
            // taking a string and returning an Option<Age>
            // using Bind!!!
            // Method Group
            Func <string, Option <Age> > parseAge = s => Int.Parse(s).Bind(Age.Of);

            Option <Age> a = parseAge("26"); // => Some(26)

            // how to work with Option<Age>?
            // Match is easiest
        }
예제 #15
0
        public void using_bind_to_parse_age()
        {
            // Arrange

            //Func<string, Option<Age>> parseAge = s => Int.Parse(s).Bind(Age.Of);
            Option <Age> parseAge(string s) => Int.Parse(s).Bind(Age.Of);

            // Act

            // Assert
            Assert.Equal(None, parseAge("notAnAge"));
            Assert.Equal(None, parseAge("180"));
            Assert.Equal(None, parseAge("-1"));

            //var ac1 = parseAge("1").Value().Value;
            var age1 = parseAge("1").Value();

            Assert.Equal(1, parseAge("1").Value().Value);
        }
예제 #16
0
        static void Main(string[] args)
        {
            Console.WriteLine(Funk1.GreetingMessage());
            Funk1.Triples();
            Funk1.Original(new int[] { 1, 5, 3, 8, 2, 4, 6, 9, 8, 3 });
            Funk1.Unpredictible();

            var circle = new Circle(10);

            WriteLine($"New circle with Circumference: {circle.Stats.Circumference} and Area: {circle.Stats.Area}");
            WriteLine($"Days of the week: {string.Join(", ", Utils.Days)}");
            WriteLine($"Days of the week starting with S : {string.Join(", ", Utils.DaysStartingWith("S"))}");

            Func <DayOfWeek, bool> daysStartingWith = d => d.ToString().StartsWith("T");

            WriteLine($"Days of the week starting with T : {string.Join(", ", Utils.Days.Where(daysStartingWith))}");

            Func <double, double, double> divide = (x, y) => x / y;

            WriteLine($"Divide 10 / 5: {divide(10, 5)}");
            var divideBy = divide.SwapArgs();

            WriteLine($"Divide 10 / 5: {divideBy(10, 5)}");

            WriteLine($"Mode of range: {Utils.GetModOfNumbers(1, 20, Utils.isModOf2)}");
            WriteLine($"Mode of range: {Utils.GetModOfNumbers(1, 20, Utils.isMod(3))}");

            var shoppingList = new List <string> {
                "coffee beans", "BANANAS", "Dates"
            };

            WriteLine($"Shopping list in sequence: {string.Join(", ", shoppingList.Select(Extensions.ToSentenceCase))}");
            WriteLine($"Shopping list in sequence: {string.Join(", ", shoppingList.AsParallel().Select(Extensions.ToSentenceCase))}");
            WriteLine($"Shopping list (Formated): {string.Join(", ", Utils.FormatToList(shoppingList))}");

            WriteLine(LaYumba.Greet(None));
            WriteLine(LaYumba.Greet("Yannis"));

            WriteLine(Int.Parse("10") == 10);
            WriteLine(Int.Parse("blblb"));
        }
예제 #17
0
        public void EitherReturningOperationsChainedByBindTest(
            string nroStr, int divisor, int numBase, Either <string, int> expected
            )
        {
            //Arrange
            Func <int, int, Either <string, int> > div = (x, y) =>
                                                         y == 0 ?
                                                         (Either <string, int>)Left("Division by zero error")
                    : (Either <string, int>)Right(x / y);

            Func <int, int, Either <string, int> > log = (x, y) =>
                                                         y <= 0 ?
                                                         (Either <string, int>)Left("Domain error in log function")
                : (Either <string, int>)Right((int)Log(x, y));
            //Act
            var result = Int.Parse(nroStr)
                         .ToEither("Invalid input")
                         .Bind(i => div(i, divisor))
                         .Bind(i => log(i, numBase));

            //Assert
            Assert.Equal(expected: expected, actual: result);
        }
예제 #18
0
파일: Program.cs 프로젝트: nf313743/books
        static void Main(string[] args)
        {
            string       input   = "66";
            Option <int> optI    = Int.Parse(input);
            var          ageOpt  = optI.Map(i => Age.Of(i));
            var          ageOpt1 = optI.Bind(i => Age.Of(i));

            // Taking an option, applying a function that returns an option
            // and return that result
            var result = optI.Bind(x => Some(x));

            // Taking an option, applying a function that can return anything
            // return the result wrapping in an Option.
            var result2 = optI.Map(x => x);


            Calc(3, 0)
            .Bind(Calc(3, 3))
            .Match(
                x => Console.WriteLine(x),
                y => Console.WriteLine(y)
                );
        }
예제 #19
0
        static void Main(string[] args)
        {
            var text = File.ReadAllText("input.txt");

            string[] split   = text.Split(",");
            int[]    opcodes = new int[50000];
            int      counter = 0;

            foreach (string x in split)
            {
                opcodes[counter] = Int.Parse(x);
                counter++;
            }

            Task task0 = new Task(() => Compute(opcodes));
            Task task1 = new Task(() => DrawScreen());
            Task task2 = new Task(() => UpdateGrid());

            task2.Start();
            task1.Start();
            task0.Start();
            Task.WaitAll(task0, task1, task2);
        }
예제 #20
0
        internal static void _main_1()
        {
            WriteLine("Enter first addend:");
            var s1 = ReadLine();

            WriteLine("Enter second addend:");
            var s2 = ReadLine();

            var result = from a in Int.Parse(s1)
                         from b in Int.Parse(s2)
                         select a + b;

            // alternatively, any of the following
            result = Int.Parse(s1).Bind(a => Int.Parse(s2).Map(b => a + b));
            result = Int.Parse(s1).SelectMany(a => Int.Parse(s2), (a, b) => a + b);
            result = Some(new Func <int, int, int>((a, b) => a + b))
                     .Apply(Int.Parse(s1)).Apply(Int.Parse(s2));

            WriteLine(result.Match(
                          Some: r => $"{s1} + {s2} = {r}",
                          None: () => "Please enter 2 valid integers"));

            ReadKey();
        }
예제 #21
0
        static void Main(string[] args)
        {
            int n = Int.Parse(Console.ReadLine()); //created size_of_array

            string s = Console.ReadLine();         //saved entering string_of_numbers

            string[] ss = s.Split(' ');            //divided the entered string by spaces and saved to an array of string type

            int[] numbers = new int[n];            //created new_integer_type_array

            for (int i = 0; i < n; i++)            //created a loop , which will continue a times
            {
                int number = Int.Parse(ss[i]);     //convert elements of string_array to integer
                numbers[i] = number;               //wrote the converted element in our integer_array
            }

            int cnt = 0;                 //created a integer_variable

            for (int i = 0; i < n; i++)  //used a loop to find number_of_primes in array
            {
                if (IsPrime(numbers[i])) //checked if function true/false
                {
                    cnt++;               //increment cnt if comdition is true
                }
            }
            Console.WriteLine(cnt);                  //output of_number_of_primes
            for (int i = 0; i < n; i++)              //useed a loop to find primes
            {
                if (IsPrime(numbers[i]))             //checked for true/false value to_be_prime
                {
                    Console.Write(numbers[i] + " "); //output primes if true_value
                }
            }

            Console.ReadKey();// to console won't close
        }
예제 #22
0
 static Option <int> MultiplicationWithBind(string strX, string strY)
 => Int.Parse(strX).Bind(x => Int.Parse(strY).Bind <int, int>(y => multiply(x, y)));
예제 #23
0
 private static int ExtractInt(StringReader reader)
 {
     var line = reader.ReadLine();
     var split = line.Split(':')
     return Int.Parse(split[1]);
 }
예제 #24
0
        // Then change the first one of the functions to return an `Either`.

        static Either <string, int> ParseIntVerbose(this string s)
        => Int.Parse(s).ToEither(() => $"'{s}' is not a valid representation of an int");
예제 #25
0
 public Option <int> ToNatural() => Int.Parse(_value).Where(IsNatural);
예제 #26
0
 // Parse is a function that does the int parse and returns Some or None
 static Option <int> ToNatural(string s) => Int.Parse(s).Where(IsNatural);
예제 #27
0
 private void button1_Click(object sender, EventArgs e)
 {
     frm.SetIndex(Int.Parse(textBox1.Text));
 }
예제 #28
0
 public static Option <int> DoubleOf(string s)
 => from i in Int.Parse(s)
 select i * 2;
예제 #29
0
 // Parses an age from an int and tries to make an age from the given int.
 static Option <Age> ParseAge(string s) =>
 Int.Parse(s).Bind(Age.Of);
 private static int ParseInt(string line)
 {
     var split = line.Split(':')
     return Int.Parse(split[1]);
 }