Almost-Weekend Reading List

What Is a Pure Function?

That’s exactly what the title says, this article explains what a pure function is. There are 2 things that define a pure function. One of them is the function always returns the same value for the same inputs. I’ll let you read the other from the blog.


The Cloud-native Azure Application Architect Map

If you are confused with Azure services offering because there are too many of them, you’re not alone. The way-too-many-services always lead to analysis paralysis of what to use for your project. This architecture map should help you decide.


A Step-by-Step Guide to Continuous Deployment on Kubernetes

`Dockerfile`, `docker-compose.yaml` file, Kubernete manifest file… ever confused on how to do CI/CD on Kubernetes exactly? Tomas will walk you through his experience.


Research Paper Week: Automatic Indexing in Azure SQL DB

This one is a heavy read, could probably be your help-me-sleep reading list. The research is about how Azure automatically perform indexing in Azure SQL database. Today, Azure already has a lot of automatic recommendation on how to increase your Azure SQL performance. This one takes a step further but automatically applying the recommendation and if it’s not working as intended, roll it back. Pretty cool idea!


Avoid multithreading traps with Roslyn: Lock object selection

What NOT to lock when multithreading? Well, here’s the list.


Advertisements

Fair Distribution Algorithm

Given 5 boxes with different weight [24, 27, 17, 15, 17], distribute the weight as even as possible among 3 trucks of the same size. The trucks can fit unlimited number of boxes. The weight of the boxes can’t be transferred, for example move 4 from box 1 (originally weight 24) and transfer to box 2.

What if we have more boxes with different weight, let’s say 6 boxes with weight of [1, 2, 17, 21, 7, 6].

What if the number of truck change to 5 trucks?

What’s the most effective algorithm to distribute the boxes evenly (in weight) within all the trucks?

C# Check if PropertyType is Null

You want to check if a property’s PropertyType is nullable. In the example below, only property `IsStudent` is nullable.

User.cs

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool? IsStudent { get; set; }
}

Checking if PropertyType is nullable is useable when adding column to `DataTable` object. If column to be added is nullable type, we need to explicitly set it.

To check if PropertyType is nullable, we can use `System.Nullable.GetUnderlyingType(Type nullableType)`, see example below and reference here.

public void GetUsers()
{
    System.Reflection.PropertyInfo[] properties = typeof(User).GetProperties();
    System.Reflection.PropertyInfo[] readableProperties = properties.Where(w => w.CanRead).ToArray();
    foreach (var property in readableProperties)
    {
        var propertyType = property.PropertyType;
        if (System.Nullable.GetUnderlyingType(propertyType) != null)
        {
            // It's a nullable type
        }
        else
        {
            // It's not a nullable type
        }
    }
}

Reference
Stackoverflow

Integrate Ionic in Existing Angular Project

Angular Project

I just finished my project, written in Angular and although it runs on mobile browsers, it needs to be deployed as a native application. The choice is obvious, Ionic.

I’m integrating Ionic for Android development, but it’s optional. You don’t have to prepare your Ionic project for Android development, if you don’t want to. The same concept can also be used to prepare your project for iOS development.

This applies to:

  • Angular 7.
  • Ionic 5.
  • Java SDK 8 update 202.
  • Android SDK 9.0 (API level 28)
  • Android Studio 3.3.2

Here’s my approach integrating Ionic in my Angular project.

Bring-in Ionic

The step includes creating a new project using Ionic CLI, I’m going to refer it as Ionic project while my original Angular project as, well, Angular project.

  • In your Angular project, update all npm packages, to the latest major if you can. This is to avoid version conflict with Ionic project’s npm packages.
  • Start a new Ionic blank project.
    ionic start project-name blank
    
  • Update all npm packages in the newly created Ionic project.
    npm i -g npm-check-updates && ncu -u
    
  • Copy ionic.config.json from Ionic project to Angular project.
  • Copy angular.json from Ionic project to Angular project.
    • If you have specifically changed anything in your angular.json, make that necessary changes in the Angular project after the copy.
    • Ionic uses SCSS for style sheet by default, so if you are not using SCSS, make sure to copy settings under projects/app/architect/**/options/styles from Angular project’s angular.json prior to copy.
    • integrate-ionic-in-angular-project-1
  • Copy package.json from Ionic project to Angular project.
      • If you have specifically changed anything in your package.json (npm scripts, etc), make that necessary changes in the Angular project after the copy.
      • Combine the npm packages from both projects setting under dependencies and devDependencies.
      • integrate-ionic-in-angular-project-2

     

  • Combine .gitignore files from both projects, if you are using Git source control.
  • In Angular project, delete package-lock.json file and node_modules folder. These should be located in root of the project.
  • In Angular project, run npm install command.
    npm install
    
  • Test and make sure everything runs.
    // Test run with Ionic
    ionic serve
    // Test run with Angular
    ng serve --open
    
  • If you want to prepare your project for Cordova, runs the following command. Note that environment setup is required. Please refer to reference section for more details.
    // For Android
    ionic cordova prepare android
    // For iOS
    //ionic cordova prepare ios
    

References

ECMAScript 2015 Destructuring and Spread

Destructuring

Destructuring is a way to unpack values from arrays, or properties from objects, into its own variables.

var o = {p: 42, q: true};
var {p: foo, q: bar} = o;

console.log(foo); // 42
console.log(bar); // true

Spread

Spread allows collection to be expanded in places where zero or more arguments are expected (in function), or elements are expected (in array literals), or key-value pairs are expected (in object literals).

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));
// expected output: 6

Reference

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

BuzzWord: Functional Programming Short Guides

This is summary of: Anjana Vakil’s video

1. Do everything with functions. Function take input and return output.
Non functional

var name = “Anjana”;
var greeting = “Hi, I’m ”;
console.log(greeting + name);
// Output: “Hi, I’m Anjana”

Functional

function greet(name) {
    return “Hi, I’m ” + name;
}
greet(“Anjana”);
// Output: “Hi, I’m Anjana”

2. Avoid side effects. Function should only process input it is given and return output, everything else is distractions (ie: process global variables, run command not intended to process input, etc). This is pure functions.
Not pure

var name = “Anjana”;
function greet() {
    console.log(“Hi, I’m ” + name);
}

Pure

function greet(name) {
    return “Hi, I’m ” + name;
}

3. Could have higher-order functions. Inputs and outputs can be functions.

function makeAdjectifier(adjective) {
    return function (string) {
        return adjective + “ ” + string;
    };
}

var coolifier = makeAdjectifier(“cool”);
coolifier(“conference”);
// Output: "cool conference"

4. Don’t iterate. Instead of iterate, use functions (map, reduce, filter).
Iterate

var fruits = ["apple", "orange", "banana"];
var goodFruits = [];
for (var i = 0; i  fruit + " great");
// Output: ["apple great", "orange great", "banana great"]

5. Avoid mutability objects. Unintended changes to objects could cause confusion and introduce bugs. Instead, use immutable objects.
Mutable

var rooms = [“H1”, “H2”, “H3”];
rooms[2] = “H4”;
// Output: ["H1", "H2", "H4"]

Immutable objects with function

var rooms = [“H1”, “H2”, “H3”];
var newRooms = rooms.map(function (rm) {
    if (rm == “H3”) {
        return “H4”;
    } else {
        return rm;
    }
});
// Output: rooms => ["H1", "H2", "H3"]
// Output: newRooms => ["H1", "H2", "H4"]

6. Persistent data structure for efficient immutability.
Instead of copying immutable objects which take time and memory (space) for every single copy, use persistent data structure that treat object as nodes and connect them in tree structure, changes of objects will only affect certain nodes, not entire object. This can be achieved with third party framework such as Mori or Immutable.js