Map vs Reduce vs Filter in JavaScript

So these methods are part of the “functional” aspect of JavaScript. JavaScript is a strange language in a good way. Before we get into this, know that all these methods above try to replace the “for” loop, or any other type of loop you can think of.

Some of you might be saying, “I like my loops thank you very much, it is in every language out there !”. And yes that is very true, however these make writing a whole loop for something trivial a thing of the past. So lets get on with it, its not as hard as you probably things it is.

All these methods only working on arrays. And they never modify the array you apply them to. They just return a new array.

MAP

As in Mapping “X” to “Y”, or transforming X into Y. In the case of JavaScript you are making a map of your values. You basically just give it a function (could be anonymous) and it applies it to each array element, and forms a new array.

REDUCE

As in reducing something to its essence, or compressing something down. In the case of JavaScript you are taking all your values in an array, and compressing them into something useful.

FILTER

As in finding something based on a certain set of parameters. I think you guys kinda already know what this particular method does.

I hope this helped you better understand Reduce, Map, and Filter 🙂

Questions People have Asked Me – Part 1

Below are some questions I was recently asked, with my answers. 

Please let me know if any of them are wrong, its a learning opportunity for me 🙂

What is the binary sort algorithm and how does it work? 

The binary sort algorithm (BSA) is used to effectively sort data. It works on the principle of continuously cutting the data set in half, until it finds what it is searching for. However, this algorithm only works on data sets that are already sorted. 

First the BSA checks the middle of the data set and compares the value it is searching for. If this value is the value it is searching for then it stops. However, if it is not equal, it checks if the value it found is bigger or smaller then the value it is trying to find. If the value is smaller, the BSA repeats the process on the left, whereas if the value is bigger it repeats the process on the right. This process is repeated multiple times until the value that the BSA is looking for is found. 

What is recursion and how is it used? 

Recursion in programming is when the program starts calling its self, from inside its self. This programming technique is usually used when a single large program can be solved in smaller parts and has a valid base case. Such as computing the Fibonacci Sequence or traversing a binary search tree. 

What is polymorphism and what is its purpose? 

Polymorphism is an aspect of Object-Oriented Programming where a “object” can take on many different forms, if all the forms are its children.  

Explain when you should use interfaces and when you should use abstract base classes. 

Both interfaces and abstract classes are a type of contract, in the class structures of a software application. Interfaces are a form of contract between two different entities, where you want to separate the functions from the implementations. This is done such that you existing application does not need to change much if a certain part of it is changed. This can be seen in the commonly used repository pattern, which is used to separate data access logic from the business logic. Where as abstract classes are a less extreme version of interfaces, where certain methods defined in it can have real implementations. This allows any child class that inherits from the abstract class to get those method implementations. The difference between the two can be further seen in how their child methods are derived. Since interfaces are “implemented”, where as abstract classes are “extended”. 

When should you use static methods and static variables? And when shouldn’t you use them. 

Static methods and variables can be used from a class without having to instantiate it. This is usually used when, you want to group a set of functionality or utility function together. An example of this is the “Math” class in JAVA, which gives the user all the math related function they need. You wouldn’t want to use them when you would be creating your inheritance-based class structures, most of the time. 

 

Write a SQL statement to create a table called “author” with the columns “id”, “name”, “age” (for MySQL or SQL Server). 

CREATE TABLE author ( 

id int NOT NULL AUTO_INCREMENT, 

name varchar(255) NOT NULL, 

age int, 

PRIMARY KEY (id) 

); 

Write a SQL statement to create a table called “book” with the columns “id”, “title”, “genre”, “author_id” (for MySQL or SQL Server). 

CREATE TABLE book ( 

id int NOT NULL AUTO_INCREMENT, 

title varchar(255) NOT NULL, 

genre varchar(255), 

author_id int, 

PRIMARY KEY (id), 

FOREIGN KEY (`author_id`) REFERENCES `author` (`id`) ON DELETE CASCADE); 

 

For the “author” and “book” tables created above, write a SQL statement to tell you the number of books each author has written, but only for authors who have written 2 or more books. The output should not show authors that have written only 1 book. The output should have the author’s name and the number of books they have written. 

SELECT author.name, COUNT(*) AS ‘# books’ FROM author, book WHERE author.id = book.author_id GROUP BY author.name HAVING COUNT(*) > 1; 

 

In databases, what are indexes used for and how to you decided how to use them effectively. 

Indexes in databases are used to speed up data retrieval. However, they come at the additional cost of space, and added complexity to database maintenance. They should only ever be used when the same type, or group of data is constantly being accessed. If the number of reads get even larger, there should also be some sort of caching layer the application queries, such that it doesn’t need to query the SQL database directly. 

 

What is the value of unit testing and what are some of your strategies for writing good unit tests? 

Unit testing is used to test the functionality of the different parts of an application. Its value lies in the fact that they make the programmer, test their code in a systematic way. And feeds into a workflow where tests are run before anything gets committed to the master branch. I think the best way to write a test case, is to write the test before writing the application logic, since it gets you thinking about what edge/special cases to consider. This is also known as the test-driven development approach. 

Getting a Job as a Developer in the Modern Age

I am gonna just  start making fun of stuff below:

I remember a time when simply listing “Java” as language on your resume, was a sure fire way to land a job … the good old days (1990s). Too bad I wasn’t around during the tech bubble. The days when simply adding “.com” to your company name would shoot up its stock price by 50%.

Now … now its early 2019. A time when no one cares if you can actually make stuff with code. No, people and large corporations realized that there is something much more important. And if you are a developer reading this, you already know what that “something” is. Its writing little programs in front of people you don’t know, to do absolutely nothing useful. Yes that’s right, they don’t actually care if can do the job. They just care if you can find a loop in a linked list, or print out a binary tree in descending order. Cause everyone knows that it is exactly what you need when making your web page, or UI, or implementing  business logic (cause everyone knows that every simple piece of logic needs a tree data structure in there somewhere right ?).

Now I am gonna stop making fun of stuff.

I write to let “people” (who these people are, I will never really know) that I am gonna be  starting a series of set of posts talking about all the stuff, I am currently doing to work for someone else.

 

 

 

 

WTF is are Stacks, Queues, and Deques ?

Stack

A stack is a stack of books, or a stack of sandbags or a stack of elephants or even a stack of unicorns. Basically a stack is anything that follows LIFO (Last In First Out), which means that if the last thing you put onto the stack is the first thing you have to take out, then its a stack.

Lets say you build a tower of blocks, that tower is a stack. Why ? Cause to get to the block at the base you need to take off all the other blocks. Below you will see a visual example of what I am talking about.

stackexample

 

Queue

A Queue in programming is the same thing as the Queue while in line to buy food, or go to a movie, or get into a night club, its a first come first serve basis. Meaning the first thing to get out of a Queue was the first thing to go into the Queue.

You can also think of Queues as pipes that transport things. In the case of plumbing, the steel pipe and the water moving through it the Queue. Since the water that first enters the pipe is the water that first leaves the pipe.

Below is a diagram to show what I am talking about:

queuesexample

 

Deques

Deques are like the cooler older brother of the Queue, since it lets things flow in more then one direction. Now the user of the data structure can choose how they take elements out of it. If you understood how Queues worked, then you should be able to understand the diagram below.

dequesexample

Project – Cognative Oncology Systems (COS)

Overview

COS is a Saas product designed to help Oncologists better diagnose and track tumours in their patients. This MVP is for my end of degree capstone project.

COS uses a trained neural network to do Image Segmentation on CT and MRI Scans.

Here is a link to the presentation that was made in 2018: https://docs.google.com/presentation/d/1jemo6qzxRQu7MUc8TgouUCiTZMz8LapDyJOfqdsiyLM/edit?usp=sharing

Technolgies Used

COS uses a number of open source and closed source technolgies:

  • ASPNET MVC
  • Flask
  • Razor Pages
  • JQuery
  • Tensor Flow
  • Docker
  • MS SQL
  • Python
  • C#

Hosted On:

  • Azure Web App as a Service
  • Azure VM (Ubuntu)

 

WTF is Token Authentication ?

So your working on a web app and you realize you want to allow them have accounts and login and out. To accomplish this, you can use a number of different things, or just use what ever your web framework (ASP.NET MVC, Express, Rails, and etc) of choose does.

Or you can use this thing called “Token Authentication”. Now what it is, exactly what it sounds like, its just a token you pass between your client (browser) and server to validate your request.

You might be thinking well that sounds okay but what stops someone from capturing your token and pretending to be you. Or what would stop someone from making a fake token ?

JWT (JSON Web Token) stops all those things. It is self contained and can hold a variety of different information. Its structure can be broken down into three parts:

  1. Header (can be decoded by anyone)
    • Contains what algorithm was used to encrypt it
  2. Payload (can be decoded by anyone)
    • The info (user name, id, expiry date and etc) stored inside the token
    • Got to be careful with what type of info you put in here
  3. Signature
    • What the server uses to actually validate the token.
    • Generated by doing the following :
    • HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
    • That “secret” part is what is used to generate the signature, and lives on your server.

     

So that is why:

  1. A person cannot capture your token and infinity use it , since we can specify a expiry date.
  2. A person cannot make a fake one, since we hash the token with a secret that only lives on the server

A few cool side features of using a JWT is that the server does not need to validate the user by interacting with the data store. Which may be a big deal if you want to have a high performance application. The other cool feature is that since you have reduced your authentication method to a JWT, it gives you a lot more flexibility in what ( maybe a mobile app ) can interact with your API.

WTF is an Extension ?

Have you every want to modify your car ? Well if you have then you already know the basics of what a extension is. Since you modify something that someone else has made (maybe you also made it too, but thats okay).

OR have you ever been in a situation where, you are working on something. And you thought “maybe I should write a new class to extend this other class, that way I add to the functionality”. You could do that, “could” being the keyword there. OR you could just use Extentions 🙂

Extensions let you add new functionality to existing classes, structs, enumerations, or protocols. All you have to do is extended them.