Git Product home page Git Product logo

nitre's Introduction

This is a reimagination of Pythons popular Itertools module for C#. Albeit LINQ provides syntactically simple expressions there is still a few things that Itertools does really well whilst making sense in C#. Most of those things seem to evolve around the use of tuples or infinite collections. Features for enumerables are already decently covered in the System.Linq-namespace.

This app requires .NET Core. PowerShell is optional.

List of features ported.

Type Nr Method Implemented Comment
Infinite  1 count
Infinite  2 cycle
Infinite  3 repeat -
Short termination 4 chain -
Short termination 5 compress
Short termination 6 dropwhile
Short termination 7 groupby -
Short termination 8 filter Also overloads index
Short termination 9 filterfalse Also overloads index
Short termination 10 islice
Short termination 11 map -
Short termination 12 starmap
Short termination 13 tee - Due to language design return tuple is defined by generics rather than argument.
Short termination 14 takewhile
Combinatoric 15 product
Combinatoric 16 permutations -
Combinatoric 17 combinations 🔨
Combinatoric 18 combinations_with_replacements 🔨

Planned changes

This library will probably include a few basic operations in addition to those listed above because of their implied use. However currying, partials and bind will not be included since the have great support through lambdas already.

Bind is already possible so no changes in relation to this:

(i, j) => valueFactory(i, "value0", j);

Currying is achieved with:

i => j => k => valueFactory(i, j, k);

Building, testing and running

PS> ./make.ps1
PS> ./make.ps1 test
PS> ./make.ps1 run

Example draft

For example a cartesian product can be described as:

    var libraries =
        from l in Libraries
        join m in Municipalities on l.MunicipalityId equals m.MunicipalityId
        from c in Countries
        where c.IsoCode equals m.IsoCode || "GP" == c.IsoCode
        select l;

Instead it's intended to make use of tuples for applying functions similar to parameter unpacking:

    var libraries =
        Product(Libraries, Municipalities, Countries)
        .Filter((item => item.Item1.MunicipalityId == item.Item2.MunicipalityId)
        .Filter(item => item.Item2.IsoCode == item.Item3.IsoCode || "GP" == item.Item3.IsoCode)
        .Select(t => t.Item1);

nitre's People

Contributors

jmrnilsson avatar

Stargazers

Aaron K avatar  avatar Florian Bruggisser avatar  avatar  avatar

Watchers

 avatar

Forkers

sembug

nitre's Issues

Oh, of course PythonSharp

Add PythonSharp to generate test suites.

using System.Collections.Generic;
using Xunit;
using PythonSharp;

namespace itertoolsTests
{
    public class itertoolsTest
    {
        [Fact]
        public void Test_count()
        {
            IEnumerator<int> count = Python.iter(Python.__builtins__.get("range")(10));
            for (int i = 0; i < 10; i++)
            {
                Assert.Equal(i, count.MoveNext());
            }
        }

        [Fact]
        public void Test_cycle()
        {
            List<int> myList = new List<int>() { 1, 2, 3 };
            IEnumerator<int> cycle = Python.iter(Python.import("itertools").get("cycle")(myList));
            for (int i = 0; i < 9; i++)
            {
                Assert.Equal(myList[i % 3], cycle.MoveNext());
            }
        }

        [Fact]
        public void Test_repeat()
        {
            IEnumerator<int> repeat = Python.iter(Python.import("itertools").get("repeat")(3, 4));
            for (int i = 0; i < 4; i++)
            {
                Assert.Equal(3, repeat.MoveNext());
            }
        }

        // Add more tests for other functions in the itertools module as needed...
    }
}

// or,
using Xunit;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace MyNamespace
{
    public class MyTestClass
    {
        private readonly ScriptEngine _pythonEngine;
        private readonly ScriptScope _pythonScope;

        public MyTestClass()
        {
            _pythonEngine = Python.CreateEngine();
            _pythonScope = _pythonEngine.CreateScope();
            _pythonEngine.ExecuteFile("itertools.py", _pythonScope);
        }

        [Fact]
        public void Test_count()
        {
            var countFunction = _pythonScope.GetVariable("itertools.count");
            var countIterator = countFunction(10);

            for (int i = 0; i < 10; i++)
            {
                Assert.Equal(i, countIterator.next());
            }
        }

        [Fact]
        public void Test_cycle()
        {
            var cycleFunction = _pythonScope.GetVariable("itertools.cycle");
            var myList = new[] { 1, 2, 3 };
            var cycleIterator = cycleFunction(myList);

            for (int i = 0; i < 9; i++)
            {
                Assert.Equal(myList[i % 3], cycleIterator.next());
            }
        }

        [Fact]
        public void Test_islice()
        {
            var isliceFunction = _pythonScope.GetVariable("itertools.islice");
            var myList = new[] { 1, 2, 3, 4, 5 };
            var isliceIterator = isliceFunction(myList, 1, 4);

            for (int i = 1; i < 4; i++)
            {
                Assert.Equal(i + 1, isliceIterator.next());
            }
        }

        // Add more tests for other functions in the itertools module as needed...
    }
}

Allow for unpack of tuples as well

Also add nice feature to unpack tuple or list.

Let say:

		private void Unpack<T, T0>(Tuple<T, T0> tuple, out T item1, out T0 item2)
		{
			item1 = tuple.Item1;
			item2 = tuple.Item2;
		}

Implement NAryList

Example: 2-ary tupled list

        private class NAryList<T0, T1> : IList<Tuple<T0, T1>>
	{
		public Tuple<T0, T1> this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

		public int Count => throw new NotImplementedException();

		public bool IsReadOnly => throw new NotImplementedException();

		public void Add(Tuple<T0, T1> item)
		{
			throw new NotImplementedException();
		}

		public void Clear()
		{
			throw new NotImplementedException();
		}

		public bool Contains(Tuple<T0, T1> item)
		{
			throw new NotImplementedException();
		}

		public void CopyTo(Tuple<T0, T1>[] array, int arrayIndex)
		{
			throw new NotImplementedException();
		}

		public IEnumerator<Tuple<T0, T1>> GetEnumerator()
		{
			throw new NotImplementedException();
		}

		public int IndexOf(Tuple<T0, T1> item)
		{
			throw new NotImplementedException();
		}

		public void Insert(int index, Tuple<T0, T1> item)
		{
			throw new NotImplementedException();
		}

		public bool Remove(Tuple<T0, T1> item)
		{
			throw new NotImplementedException();
		}

		public void RemoveAt(int index)
		{
			throw new NotImplementedException();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			throw new NotImplementedException();
		}
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.