jsx vs templates

JSX pros

Expressing in form of javascript (Not confined to arbitrary syntax).

Treat view as a data. With these data one can take snapshot, render to alternative targets.

Cons

Diffing cost is relative to the total size of view rather than the number of nodes changed.

Template pros

Enhances team work by separation of work based on skill-set

Does not need runtime scheduling for optimization.

Cons

Constrained by the templates syntax.

Runtime compilation cost

Adding entire language in technology stack.

VIM basic commands for beginners

I started learning vim from https://www.openvim.com/. https://danielmiessler.com/study/vim/

below are some basic commands I learned.

Vim has two modes insert mode and normal mode.

Insert mode helps to write text in vim as normal text editor like atom.

Normal mode make work efficient by manipulating text with shortcut commands.

To toggle between insert mode and escape mode you can type esc for normal mode and i for insert mode.

vim -v (prints vim version)
vim ~/work/my-proj
Shift + g (points cursor to bottom of a file)
gg (points cursor to top of a file)
j (next below line of the file)
k (next up of current line)
l (shifts cursor to +1 index from current index)
h (shifts cursor to -1 index from current index)
:q (exits file with warning for unsaved files)
:q! (forcefully exits file without warning)
:wq (write and quits file from current session)
:dd deletes line
ZZ(capital) alternative to q
:syntax on syntax highlights
:syntax of syntax does not highlights
u undo
ctrl + r redo

Highlighting syntax default in vim

open vi ~/.vimrc
add vim command :syntax on and save

VIM cheatsheets
https://vim.rtorr.com/

Async/Await in jS

async/await is built on top of the promise and supports all promise based apis.

async keyword in function

  1. It declares an asynchronous function eg:  async getUsers() {…}
  2. It enables the use of await.
  3. It returns promise. Promise is created when starting the execution of function.

await in async function

  1. await waits for promise to be settled. if promise is fulfilled the result of await is the promise resolved value. if the promise is rejected, await throws the rejection value.
  2. await used in front of promise call, await forces the rest of the code to wait until the promise finished the return result.

 

For reading more:

https://tutorialzine.com/2017/07/javascript-async-await-explained

http://exploringjs.com/es2016-es2017/ch_async-functions.html

Hash#update

Hash#update
Its an alias for !merge which updates the given hash object and returns the updated hash. Basically used to update the value of hash with same key or !merge.

NOTE:  update routine overrides the value of the caller object which keys matches with the parameter pass hash in the update routine.
Usage 1:

  hash1 = { a: 'I am from hash 1'}
  hash2 = { a: 'I am from hash 2'}
  hash1.update(hash2)
   => {:a=>"I am from hash 2"}
  hash1
   => {:a=>"I am from hash 2"}

usage 2:

  hash1 = { a: 'I am from hash 1'}
  hash2 = { b: 'I am from hash 1'}

  hash1.update(hash2)
  hash1
    => {:a=>"I am from hash 1", :b=>"I am from hash 1"}

for more details:
http://ruby-doc.org/core-1.9.3/Hash.html#method-i-update

Selection sort in ruby

a = [9, 8, 6, 5, 4, 3, 2, 1]

// sorting in ascending order

total_index = a.length - 1
(total_index).times do |i|
  current_top_index = i
  (i + 1).upto(total_index) do |j|
    if a[j] < a[current_top_index]
      temp = a[j]
      a[j] = a[current_top_index]
      a[current_top_index] = temp
    end
  end
end
a = [1, 2, 3, 4, 5, 6, 8, 9]

Time complexity
O(n*n)

Space complexity
O(1)