-
String functions in JavaScript by example
You can test these functions online at jsbin.com.
Get the character at a specific index var name = "amir"; alert("charAt(0): " + name.charAt(0)); It will return “a” as it is the first character of “amir”.
Find the index of character in string var name = "amir"; alert("indexOf('m'):" + name.indexOf('m')); it will return 1 as “m” is the second character of the “amir” and it start counting from 0.
-
Underscorejs JavaScript library by example
Underscore is a JavaScript open source library that brings lots of functions. You can download the latest version of the library from underscorejs.org .
For each You can go through an array and reach to each element that exist in it .
A functional style var numbers = [1, 3, 4]; _.each(numbers, function (num) { alert('functional :' + num); }); Output :
functional : 1 functional: 3 functional: 4 Object oriental style _(numbers).
-
JavaScript fundamentals by example
Iteration Loop function in JavaScript is similar with other OOP languages.
Foreach loop var amir = { name: "amir", age: 26 }; You can go through the object with foreach loop without knowing the number of elements that exist in that object.
(function (obj) { for (var arg in obj) { alert(arg + ' : ' + obj[arg]); } })(amir); The input arguments are arrays You can treat the input arguments as arrays.
-
JavaScript fundamentals by example
What is JavaScript? It is a scripting language that runs on the client side. JavaScript is the language of the browser which is dynamic and have OO and functional features.
IDE There are a couple of good IDE that help you to develop JavaScript functions such as VS2012, WebStorm , DreamWeaver and Sublime. There are also some online editors that you can run your code and get instant feedback such as JsBin.
-
Higher order functions in cSharp
What are higher order functions? Treating functions as regular objects enables us to use them as arguments and results of other functions. Functions that handle other functions are called higher order functions (codeProject).
Example If you need to ask your method to call various methods in different situation you can use the Func delegate.
extension func
private static IEnumerable<int> Find(this IEnumerable<int> list, Func<int, bool> NumberMatchFormat) { return list.
-
The CAP Theorem
What is Distributed System ? The client can read and write data into distributed system from any node.
What is CAP Theorem ? It define the behaviour of distributed systems. It will guarantee two of “Consistency”, “Availability” and “Partition Tolerance” attribute at the same time. In other words you can get any two at any time but not possible to get all of them at the same time.
Consistency system guarantees to read data as fresh as you just wrote.
-
CheckOut codekata with cSharp
What is CheckOut codeKata ? The goal in Check out codekata is to find the way to calculate total price of items. there is some discount for some of the items that cause this codeKata become a little more challenging.
Item Unit Price Special Price A 50 3 for 130 B 30 2 for 45 C 20 1 2 3 4 5 Item Unit Price Special Price A 50 3 for 130 B 30 2 for 45 C 20 You can find more info about the scenario here.
-
Make Generic Extension Method step by step in CSharp
What is extension method? “Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type” (source). In other words by using extension method we embed our functionality in the class rather than make an instance of it and using it. LINQ is a set of extension methods that add various functionality to collection members such as IEnumerable.
-
ErrorHandling in cSharp example
What is error handing ? “Things can go wrong, and when they do, we as programmers are responsible for making sure that our code does what it needs to do” (clean code, Robert Martin).
Write your try-catch-finally first and provide enough context for your exception errors. It is always better to catch an exception rather than check the type or return null.
Raise an exception Sometimes in after we catch the exception and logging it , we might need to raise an exception to terminate the process.
-
Use Delegate and Event in CSharp to have Tell Don’t Ask
What is Tell don’t ask? In this pattern we make our decision and then tell object what you want them to do rather than asking them about their state. It is okay to get state of an object , as long as do not use them to make decisions on the caller side .Once, you start making decisions based on object properties then you violating object encapsulation.
It also helps us to decouple the responsibilities and preventing to violating the Law of Demeter (source 1, source 2).