First OSS Pull-Request Accepted


I am very happy to announce that my first OSS pull-request has been accepted by Microsoft 🙂

Advertisements

My views on F#


The below given points are what I felt when I started with F#. Since I am still learning about F#, these may sound absurd or weird to the Masters or geeks in F#.

1. I am a C# developer, sought to learn functional programming, started with Scala and got advice from great people and moved to F#, i’ m still fiddling around
2. I am not finding easy when handling the interfaces. It’s not as easy in C#, I.e. when passing around the interfaces or trying to create a reference to an object using its interface.
3. Returning a Task is a bit difficult in F# ( FOR ME). Methods like return Task can’t have a Task.FromResult<object>(null) like in C#.
4. Interoperability with C#, I mainly use lambda’s (Action & Func), as F# function is different from C# Func.
5. I stumbled upon an issue recently, I.e. there should not be even an empty file in F# project. This is because of the multiple modules convention
6. Ordering of the files in the project is like sequential, which is a bit difficult to follow coming from C#
7. IDE support for dll method signature lookups are required, most of the time, I’m hitting on F12 to see the signature which does not show up in the IDE
8. Big concern is since latest versions of C# are borrowing the concept from F#, will F# fade away with time and C# become a functional cum imperative language.

Please note that, these are my ideas and thoughts, I don’t find fault with anyone, as I am a learner still.

Tips for SQL Server


Showing the SQL Statistics in XML

To show the query plan

SET STATISTICS XML { ON | OFF }

To Show the execution times for a query, wrap the query using the below

SET STATISTICS TIME ON;

SELECT * FROM USERS;

SET STATISTICS TIME OFF;

Sample Query with CTE

WITH URT_CTE (AssignedTo, RoleId, AssignedBy,UserId)
AS
(
SELECT distinct urt.userroletenant_stratifiedtenantid AS AssignedTo,ur.userrole_roleid AS RoleId, urt.userroletenant_roleassigningtenantid AS AssignedBy, Ud.User_UserId as UserId
FROM userroletenant AS urt
INNER JOIN userroles AS ur ON ur.userrole_id = urt.userroletenant_userroleid
INNER JOIN userdetails AS ud ON ud.user_userid = ur.userrole_userid AND ud.user_userid=@userId
WHERE urt.userroletenant_roleassigningtenantid=@tenantid
)

Select AssignedTo From URT_CTE

Getting started with aspnet Core 1.0


Getting Started with ASP.Net Core 1.0

Today, I got started with the asp.net core 1.0. I have been using the .net version 4.6.1. Since, the official release is out, I thought that it may help me in learning and getting accustomed to this model so that I can migrate the code that I work day in and day out to reflect the latest

The following are the steps that I did

1. Downloaded the asp.net core 1.0 for windows [Not visual studio one, though I have VS 2015]
2. Installed the Visual Studio Code
3. created a new folder as per mentioned in the microsoft document
mkdir aspnetcore
cd aspnetcore
dotnet new

4. The step3 was completed with a couple of files like project.json and program.cs etc
5. I added the Kestrel Web Server and then registered the same in json file
“Microsoft.AspNetCore.Server.Kestrel”: “1.0.0”,
6. I created a new dummy middleware to test out the OWIN pipeline
app.Run(context =>
{
return context.Response.WriteAsync(“Hello from ASP.NET Core!”);
});

7. now i called dotnet build from the same command prompt
8. Then, I need to call dotnet run so that the source code is built and run in a port, typically port # 5000.
9. Things went well. I thought, it was enough for a hello world
10. I had to really create a WebApi and test. I created a new Folder for storing the models, controllers
11. Once the code for the above are done, I am still using VSCode, as expected, there will be build errors when running “dotnet build”
12. I had to add MVC somehow to the project, I guessed it should be within the project.json file, so googled and added
“Microsoft.AspNetCore.Mvc”:”1.0.0″
13. Then it seemed that I had to add MVC to the request pipeline so that I can access the above built in Api
14. I tried app.UseMvc() within startup > Configuration as I usually do in the previous versions [No googling done so far]
15. I got the message that AddMvc should be within ConfigureServices and as part of the IServiceCollection
16. Added that and the routes as well
17. Built and ran the app, viola, things were working fine!!!

Got a good start with ASP.Net Core 1.0

Source Code URL : https://github.com/d-saravanan/aspnetcore1.samplemvc.git

High-Order functions in Scala


I am posting the code snippet that progressively shows how the higher order functions can be written in Scala programming language

I will detail the steps a bit later, code for now.

class intSum {

def sum(a: Int, b : Int) :Int = {
if(a > b) 0;
else a + sum(a+1,b)
}

def multiSum(addend: Int, a: Int, b: Int) : Int = {
if(a > b) 0;
else add(a,addend) + multiSum(addend, a+1,b)
}

def prodSum(prodend: Int, a: Int, b: Int) : Int = {
if(a > b) 0;
else multiply(a,prodend) + prodSum(prodend,a+1,b)
}

def add(base: Int, no: Int) : Int = {
base+no
}

def multiply(base: Int, multiplicand: Int) : Int = {
base * multiplicand
}

def multiSumWithFunc(f :Int=>Int, a: Int, b: Int) : Int = {
if(a > b) 0
else f(a) + multiSumWithFunc(f, a+1, b)
}

def prodsumWithFunc(g: Int=> Int, a: Int, b: Int) : Int = {
if(a > b) 0
else g(a) + prodsumWithFunc(g,a+1,b)
}

def addWithFunc(f: Int=>Int)(a: Int, b: Int) : Int = {
if(a > b) 0
else f(a) + addWithFunc(f)(a+1, b)
}
}

object intSumApp{
def main(args: Array[String]){
var is = new intSum()
println(” The value of multisum is ” +is.multiSum(1,1,5))
println(“The value of prodsum is “+is.prodSum(2,1,6))
println(” The value of multisum with func is ” +is.multiSumWithFunc(x=> x+1,1,5))
println(“The value of prodsum with func is “+is.prodsumWithFunc(x=>x*2,1,6))
println(” The value of multisum is ” +is.addWithFunc(x=>x+1) (1,5))
var addf = is.addWithFunc(x=>x+1) _;
println(” The value of multisum is ” +addf(1,5))
}
}

Curried Functions in Scala


The following sample code snippet shows the power of Scala Curried functions. Being a .Net developer, I am really amazed by having Scala support this with less code, in C#, we could achievesimilar to this with some Action arguments to the methods

Scala

Currying functions in Scala

Step1 : Define the lowest level of functionality

scala> def XTable(m: Int) (n: Int) = m*n
XTable: (m: Int)(n: Int)Int

Step2: Define a Semi-customized or partly specialized version of the above function

scala> def xTable2 = XTable(2) _
xTable2: Int => Int

Ste3: Consume the above function with values, to get the job done 🙂

scala> xTable2(5)
res11: Int = 10

Yet another Curried function
scala> def adder(m: Int, n: Int) = m + n
adder: (m: Int,n: Int)Int

scala> val curriedAdd = (adder _).curried
curriedAdd: Int => (Int => Int) = <function1>

scala> val addTwo = curriedAdd(2)
addTwo: Int => Int = <function1>

scala> addTwo(4)
res22: Int = 6

Efficient way to sum subset arrays in an array


Today, I was stumbled upon by a problem in HackerRank algorithms section which wanted to find the subsets sum that are equal between a fulcrum point [x]

i.e. for A1,A2,… Ai, Ai+1, Ai+2…An

we are required to find the i where left of Ai = right of Ai, excluding i

I did start with a bruteforce approach and then finally found out that the sum operation was very time consuming and had to fix that and the below given code is the efficient code

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
   int t = int.Parse(Console.ReadLine());

            for (int i = 0; i < t; i++) { int n = int.Parse(Console.ReadLine()); int[] inputs = Array.ConvertAll(Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), r => int.Parse(r));

                //var sw = new Stopwatch();
                //sw.Start();
                Console.WriteLine(SherlockWatsonCheck(inputs));
                //sw.Stop();
                //Console.WriteLine(sw.ElapsedMilliseconds);
            } 
    }
    
    public static string SherlockWatsonCheck(int[] inputs)
        {
            if (inputs.Length < 2)
            {
                return "YES";
            }
            //Stopwatch sw1 = new Stopwatch();
            //sw1.Start();
            try
            {

                int totalSum = inputs.Sum();
                int loopSum = inputs[0];

                for (int i = 1; i < inputs.Length - 1; i++)
                {
                    loopSum += inputs[i];

                    if (loopSum-inputs[i] == totalSum - loopSum)
                    {
                        return ("YES");

                    }
                }

                return ("NO");
            }
            finally
            {
                //sw1.Stop();
                //Console.WriteLine(sw1.ElapsedMilliseconds);
            }
        }

}