How Map and Filter function works in RXJS

In order to understand how Map and Filter works , we can implement our own map function.
Map
Map in Rxjs used for projection, means you can transform the array in to entirely new array.

Array.prototype.map = function(projectionFunction){
var results=[];
this.forEach(function(item) {
results.push(projectionFunction(item));
});
return results;
};

You can see I have written a map function which accepts an anonymous function as a parameter. This will be your function to apply the projection to transform the array. Inside the map function you can see iterating each item in a array , call the project function by passing each item and finally the result of the projection function will push to the results array.

JSON.stringify([1,2,3].map(function(x){return x+1;}))

Output
[2,3,4]

https://www.learnrxjs.io/operators/transformation/map.html

Filter
Filter is used to filter an array based on some condition. You can specify the condition through an anonymous function. The implementation of Filter and Map are almost same only the difference is Map will transform each item in an array and will push to the main array. But the filter will only push to the main array if the condition pass.
var hotelCollection = [
{
"id": 9999,
"name": "Hotel XXX",
"place": "Wayne",
"rating": 5.0
},
{
"id": 8888,
"name": "Hotel YYY",
"place": "WoodCliff Lake",
"rating": 4.5
},
{
"id": 7777,
"name": "Hotel ZZZ",
"place": "Franklin Lake",
"rating": 5.0
},
{
"id": 6666,
"title": "Hotel PPP",
"place": "Lancester",
"rating": 4.3
},
];

Source

Array.prototype.filter=function(predicateFunction){
var results=[];
this.forEach(element => {
if(predicateFunction(element)){
results.push(element)
}
});
return results;
}

var collection = hotelCollection.filter(x=>x.rating===5);
https://www.learnrxjs.io/operators/filtering/filter.html
Thanks for reading

How Yield keyword works in C#

Yield in C# , can use with the return statement to build a collection of returns. For example , I have a method that we need to return a collection of integers in a lazy manner and expose it through an IEnumerable interface. Based on the value produce with the yield return , consumer can write some logic based on their need.

In the below example you can see I have a method ReturnSomeIntegers() , which return 1 to 10 by iterate using for loop with yield keyword and also two more yield return with 20 and 30 respectively.

class Program
{
static void Main(string[] args)
{

YieldDemo();
Console.ReadKey();

}
static void YieldDemo()
{
foreach ( int n in ReturnSomeIntegers())
{
if (n == 20)
{
Console.WriteLine("From Yield Return Two - " + n);
}
else if (n == 30)
{
Console.WriteLine("From Yield Return Three - " + n);
}
else
{
Console.WriteLine("From Yield Return One - " + n);
}
}
}
private static IEnumerable ReturnSomeIntegers()
{
for(int i = 1; i <= 10; i++)
{
yield return i;
}
yield return 20;
yield return 30;
}
}

Whenever ReturnSomeIntegers() method return a single value , the execution will switchback to YieldDemo() for each method and print the value based on the condition.

In Computer Science this is known as Continuation
https://en.wikipedia.org/wiki/Continuation

Publish and Consume Azure Service bus queues pro-grammatically in C#

You can easily publish messages in to the queue and subscribe messages from queue pro grammatically using C#
Publish messages in to queues

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
var lastKey = ' ';
var count = 1;
while (lastKey != 'q')
{
var body = $"Message {count++}";
var message = new BrokeredMessage(body);
client.Send(message);
Console.WriteLine($"Sent {message.MessageId}");
lastKey = Console.ReadKey().KeyChar;
}

Consume

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
client.OnMessage(message => {
Console.WriteLine($"Message : {message.MessageId} Message Body : {message.GetBody()}");
});
Console.ReadLine();

Connection string you can copy from here

Elastic Search – 403 – cluster_block_exception

I got an error while adding a new document to an existing index.

cluster_block_exception –¬† u’blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]

Below are the put request I have tried to send

I resolved this error by sending a new put request to the products index( which are already created before) by updating the blocks.read_only_allow_delete to false.

Url you have to send the put request

http://localhost:9200/{{yourindex}}/_settings

In my case the index is products

http://localhost:9200/products/_settings

Body parameter that you are passing along with the request

{
“index”:{
“blocks.read_only_allow_delete”:false
}
}

After the successful request I got the exact result I am expected

 

 

Windows 10 Start menu stopped working

My windows 10 start menu suddenly stopped working.Below are the steps I have done to make it working.

1. Open Windows Powershell Command as Administrator
2.Run the command Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”}


Wait until the process completes(Ignore the red errors) and finally restart Windows.

Webpack throwing TypeError: Data must be a string or a buffer in Angular CLI

Recently I got some error unexpectedly while running my Angular-CLI application.

By default you will not get any information about the error which WebPack is throwing. But I will tell you , how you can log the exact error related to your issue. The reason could be different for your scenario.

Navigate to yoursource\node_modules\webpack\lib\dependencies and open HarmonyExportImportedSpecifierDependency.js file and log the import dependency
updateHash(hash) {
super.updateHash(hash);
const hashValue = this.getHashValue(this.importDependency.module);
if (this.importDependency.module != null){
// console.log('Module resource: ', this.importDependency.module.resource);
}else{
console.log('\nFile not found: ', this.importDependency);
}
hash.update(hashValue);
}

In my case I got the module in my application where the issue is.

Hope this helps

Template Variable to Interact with Child Components in Angular 2+

I have already discussed in my last couple of blogs to interact Components using @Input and @Output. Here I am going to explain about Template Variable to interact with Child Components. By using this Template reference variable you can access public properties and methods of a child component and also to bind data to a child component.
Template Reference Variable allow you to specify a variable name on a component and you can access any public property or methods on that component using that variable.

In order to demonstrate I have a product card component which is my child component placed inside the  ProductListComponent(parent component) to list some products. You can see I have added a template reference variable named #productReference on this child component

So over here in our ProductCardComponent will just a create a method and property to access from the ProductListComponent

Now you can access this property and method from the ProductListComponent