C# code in a notebook

Links: notebook, html, PDF, python, slides, slides(2), GitHub

A few example on how to add C# code in a notebook.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

We add magic command ``%%CS```.

%load_ext csharpy

Magic Command CS

The magic command only exports one function in one piece of code.

%%CS SquareX

public static double SquareX(double x)
{
    return x*x ;
}
<function csharpy.runtime.compile.create_cs_function.<locals>.<lambda>(*params)>
SquareX(4)
16.0

Other parameters:

%%CS --help

.
usage: CS [-h] [-i [IDEP [IDEP ...]]] [-d [DEP [DEP ...]]] [-c CATCH] name
Compiles and wrap a C# function into a Python function.
positional arguments:
  name                  function name
optional arguments:
  -h, --help            show this help message and exit
  -i [IDEP [IDEP ...]], --idep [IDEP [IDEP ...]]
                        internal dependencies (like System, System.Linq)
  -d [DEP [DEP ...]], --dep [DEP [DEP ...]]
                        dependencies (assembly name without extension)
  -c CATCH, --catch CATCH
                        catch exception
usage: CS [-h] [-i [IDEP [IDEP ...]]] [-d [DEP [DEP ...]]] [-c CATCH] name

With dependencies

%%CS cs_qsort -i System -i System.Linq
-d System.Core

public static int[] cs_qsortl(int[] li)
{
    if (li.Length == 0)
        return null;
    else
    {
        var pivot = li[0];
        var lesser = cs_qsortl(li.Skip(1).Where(x => x < pivot).ToArray());
        var greater = cs_qsortl(li.Skip(1).Where(x => x >= pivot).ToArray());
        var res = new int[li.Length];

        if (lesser != null && lesser.Length > 0)
            Array.Copy(lesser, 0, res, 0, lesser.Length);
        int nb = lesser == null ? 0 : lesser.Length;
        res[nb] = pivot;
        if (greater != null && greater.Length > 0)
            Array.Copy(greater, 0, res, nb + 1, greater.Length);

        return res;
    }
}

public static int[] cs_qsort(string lis)
{
    return cs_qsortl(lis.Split(';').Select(c=>int.Parse(c)).ToArray()) ;
}
<function csharpy.runtime.compile.create_cs_function.<locals>.<lambda>(*params)>

The result is a C# object.

cs_qsort("4;5;3;-1;0;1000")
<System.Int32[] at 0x1cfb63dbe10>

Which can be converted:

list(cs_qsort("4;5;3;-1;0;1000"))
[-1, 0, 3, 4, 5, 1000]

Without a magic commmand

The module provides function to do the same thing outside a notebook.

code = """
public static int[] cs_qsortl2(int[] li)
{
    if (li.Length == 0)
        return null;
    else
    {
        var pivot = li[0];
        var lesser = cs_qsortl2(li.Skip(1).Where(x => x < pivot).ToArray());
        var greater = cs_qsortl2(li.Skip(1).Where(x => x >= pivot).ToArray());
        var res = new int[li.Length];

        if (lesser != null && lesser.Length > 0)
            Array.Copy(lesser, 0, res, 0, lesser.Length);
        int nb = lesser == null ? 0 : lesser.Length;
        res[nb] = pivot;
        if (greater != null && greater.Length > 0)
            Array.Copy(greater, 0, res, nb + 1, greater.Length);

        return res;
    }
}

public static int[] cs_qsort2(string lis)
{
    return cs_qsortl2(lis.Split(';').Select(c=>int.Parse(c)).ToArray()) ;
}
"""
from csharpy.runtime import create_cs_function
cs_qsort2 = create_cs_function("cs_qsort2", code, usings=["System", "System.Linq"],
                               dependencies=["System.Core"])
list(cs_qsort2("4;5;3;-1;0;1000"))
[-1, 0, 3, 4, 5, 1000]