понедельник, 14 августа 2017 г.

.NET Core interop with C#: native and Go

Because I can.

.NET interop is a wonderful feature and I was happy to know that in .NET Core it is still powerful and convenient. While consuming native libraries is pretty starightforward at least on Windows but today we'll try some weird things on Mac OS (I'm pretty sure on Linux it will be the same).
Calling native functions is pretty simple. Let's start with creating a basic .NET Core application:

  mkdir interop   
  cd interop   
  dotnet new console  

Then open Program.cs in your favorite text editor and make the next changes:

 using System;  
 using System.Runtime.InteropServices;  
 namespace interop  
 {  
   class Program  
   {  
     [DllImport("libm")]  
     public static extern double sin(double angle);  
     static void Main(string[] args)  
     {  
       Console.WriteLine(sin(3.1415 / 2));  
     }  
   }  
 }  

Run this with

 dotnet run  

and you'll see the result close to 1.
Ok, let's try something more custom. What about some additions in the magical unmanage world? Create a file main.go in the "interop" folder with the following content:

 package main  
 import "C"  
 //export Add  
 func Add(a, b int) int {  
      return a+b  
 }  
 func main() {}  

Please pay attention to the "import C" and "//export Add" lines, they are crucial for the c-style shared libraries. Now let's build our lib into libgo.so file:

 go build -buildmode=c-shared -o libgo.so main.go  

Copy the output into /bin/Debug/{dotnet version} folder. Change Program.cs content to the following:

 using System;  
 using System.Runtime.InteropServices;  
 namespace interop  
 {  
   class Program  
   {  
     [DllImport("libm")]  
     public static extern double sin(double angle);  
     [DllImport("libgo.so")]  
     public static extern int Add(int a, int b);  
     static void Main(string[] args)  
     {        
       Console.WriteLine(sin(3.1415 / 2));      
       Console.WriteLine(Add(2,3));     
     }  
   }  
 }  

Wow, native addition! As you can see, this is pretty simple when working with value types. Next time we'll try to figure out how to process reference types.
The source code is available on my GitHub.

среда, 26 апреля 2017 г.

Resharper Build does not build

If your Resharper Build&Run matrix is grey and some nodes say "This project it set to "Never Build" in options", go to ReSharper->Options->Build->Heuristics and examine the "Projects that should NEVER be built" window. One unlikely has a project she does not want to build.

понедельник, 18 июля 2016 г.

Scaffolding a Suave app with Yeoman for Visual Studio code

Suave is a simple web development F# library providing a lightweight web server and a set of combinators to manipulate route flow and task composition.

F# in Visual Studio Code

I'm a little bit an IDE-kid and aware of such things like Emacs and Vim however I learned a number of fancy tricks in wonderful Sublime Text and I can edit a line in Vim and then exit it without having an insult. At the same time, I see no point in choosing a text editor over an IDE in my daily works because, you know, performance matters. My performance, not my laptop's (sorry, Greg)
But sometimes you want something different.

пятница, 1 июля 2016 г.

ReSharper does not build tests on run

It took some time for me to figure out the reason, and the internet is full of advices to play with platform and framework and “run every assembly is separate app domain” settings. In my case the issue was this setting value:
i41^cimgpsh_orig
Stupid thing, hard to find, impossible to trace.

понедельник, 7 марта 2016 г.

new project on GitHub

Ok, here is a new project on GitHub: ep_activepads. It is a plugin for superb collaborative text editor etherpad-lite that lists all pads people are working on right now. It is highly inspired by ep_adminpads plugin.

What I would like to add alter is data auto update, probably with the simple timer on client side.

To install the plugin, clone it into node_modules/ep_activepads folder of your etherpad installation, terminal there and do npm install.

воскресенье, 7 февраля 2016 г.

Snooker “get remaining points” small Erlang thing

Just a little reminder for myself on Erlang module structure and common mistakes I’ve made composing this. It is a little Erlang module calculating how many points are on the snooker table. Number of the red balls in represented as integer, color balls are atoms.

-module(snooker).

-export([get_points/1]).

get_points(NumberOfReds) when is_integer(NumberOfReds) -> get_points(NumberOfReds, 0);
get_points(0) -> get_points(yellow, 0);
get_points(yellow) -> get_points(yellow, 0);
get_points(green) -> get_points(green, 0);
get_points(brown) -> get_points(brown, 0);
get_points(blue) -> get_points(blue, 0);
get_points(pink) -> get_points(pink, 0);
get_points(black) -> get_points(black, 0).

get_points(NumberOfReds, Total) when is_integer(NumberOfReds) andalso NumberOfReds =/= 0 -> get_points(NumberOfReds - 1, Total) + 1 + get_points(black);
get_points(0, Total) -> get_points(yellow, Total);
get_points(yellow, Total) -> get_points(green, Total) + 2;
get_points(green, Total) -> get_points(brown, Total) + 3;
get_points(brown, Total) -> get_points(blue, Total) + 4;
get_points(blue, Total) -> get_points(pink, Total) + 5;
get_points(pink, Total) -> get_points(black, Total) + 6;
get_points(black, Total) -> Total + 7.