写点什么

ARTS - Week One

用户头像
shepherd
关注
发布于: 2020 年 05 月 26 日
ARTS - Week One

Algorithm

Problem

Reverse Integer - From LeetCode

Given a 32-bit signed integer, reverse digits of an integer.

Solution

var reverse = function (x) {
let strX = String(Math.abs(x)).split('').reverse().join('')
let revX = x > 0 ? Number(strX) : -Number(strX)
if (revX < -Math.pow(2, 31) || (revX > Math.pow(2, 31) - 1)) return 0
return revX
};

Thinking

It's just a beginning, and making a small attempt.



Review

Artical

The Key To Accelerating Your Coding Skills

Link

http://blog.thefirehoseproject.com/posts/learn-to-code-and-be-self-reliant/

Review

The final stage of the inflection point process is acceptance. Acceptance that software development is a process of continuous learning. Acceptance that the feeling that you’ve successfully learned everything just means you should start thinking about solving more complicated problems.

As a programmer,I just accept the happy fact, work and learn, live and learn. Keep learning!

Tips

Programs often waste time calling functions which recalculate the same results over and over again.We can use memorization to solve this problem by caching its previously computed results.

For example:

// calculate fibonacci without Memoization
let fibonacciWithoutMemoization = (function () {
return function f(n) {
if (n === 0 || n === 1)
return n;
else
return f(n - 1) + f(n - 2);
}
})()
let benginTime = Date.now()
console.log(fibonacciWithoutMemoization(50)) // 12586269025
let execTime = Date.now() - benginTime
console.log(`execTime is ${execTime}ms`) // execTime is 159179ms
// calculate fibonacci with Memoization
let fibonacciWithMemoization = (function () {
let memo = {}
return function f(n) {
var value;
if (n in memo) {
value = memo[n]
} else {
if (n === 0 || n === 1) {
value = n
} else {
value = f(n - 1) + f(n - 2)
}
memo[n] = value
}
return value
}
})()
let benginTime = Date.now()
console.log(fibonacciWithoutMemoization(50)) // 12586269025
let execTime = Date.now() - benginTime
console.log(`execTime is ${execTime}ms`) // 6ms



Share

Artical

Glossary of Modern JavaScript Concepts: Part 1

Link

https://auth0.com/blog/glossary-of-modern-javascript-concepts/

Summary

1.Purity: Pure Functions, Impure Functions, Side Effects
  • Pure functions must take arguments.

  • The same input (arguments) will always produce the same output (return).

  • Pure functions rely only on local state and do not mutate external state (note: console.logchanges global state).

  • Pure functions do not produce side effects.

  • Pure functions cannot call impure functions.

  • Impure function mutates state outside its scope. Any function that has side effects (see below) is impure.

  • When a function or expression modifies state outside its own context, the result is a side effect. Include making a call to an API, manipulating the DOM, raising an alert dialog, writing to a database, etc. 

// impure function producing a side effect
function showAlert() {
alert('This is a side effect!');
}
// impure function mutating external state
var globalVal = 1;
function incrementGlobalVal(x) {
globalVal += x;
}
// impure function that resembles a pure function,
// but returns different results given the same inputs
function getRandomRange(min, max) {
return Math.random() * (max - min) + min;
}
2.State
  • State refers to the information a program has access to and can operate on at a point in time. 

  • Stateful programs, apps, or components store data in memory about the current state. 

  • Stateless functions or components perform tasks as though running them for the first time, every time. Pure functions are stateless. 

// stateful
var number = 1;
function increment() {
return number++;
}
increment(); // global variable modified: number = 2
// stateless
var number = 1;
function increment(n) {
return n + 1;
}
increment(number); // global variable NOT modified: returns 2
3.Immutability and Mutability
  • If an object is immutable, its value cannot be modified after creation.

  • If an object is mutable, its value can be modified after creation.

  • Immutability's advantage:  It results in code that is simpler to reason about; It also enables persistency.

  • Immutability's disadvantage: many algorithms and operations cannot be implemented efficiently.

4.Imperative and Declarative Programming
  • Imperative programming describes how a program's logic works in explicit commands with statements that modify the program state.

  • Declarative programming describes what a program's logic accomplishes without describing how.

5.Higher-order Functions
  • accepts another function as an argument, or returns a function as a result.

6.Functional Programming
  • Core functionality is implemented using pure functions without side effects.

  • Data is immutable.

  • Functional programs are stateless.

  • Imperative container code manages side effects and executes declarative, pure core code.

7.Observables
  • User interface events like button clicks, mouse movement, etc. are hotHot observables will always push even if we're not specifically reacting to them with a subscription.

  • cold observable begins pushing only when we subscribe to it. If we subscribe again, it will start over.

8.Reactive Programming
  • Reactive programming is concerned with propagating and responding to incoming events over time, declaratively (describing what to do rather than how).

  • Reactive programming is often associated with Reactive Extensions.

9.Functional Reactive Programming
  • denotative: the meaning of each function or type is precise, simple, and implementation-independent ("functional" references this)

  • continuous timevariables have a particular value for a very short time: between any two points are an infinite number of other points; provides transformation flexibility, efficiency, modularity, and accuracy ("reactive" references this)

  • Functional reactive programming should be:

  • dynamic: can react over time or to input changes

  • time-varying: reactive behaviors can change continually while reactive values change discretely

  • efficient: minimize amount of processing necessary when inputs change

  • historically aware: pure functions map state from a previous point in time to the next point in time; state changes concern the local element and not the global program state



用户头像

shepherd

关注

还未添加个人签名 2020.05.13 加入

还未添加个人简介

评论 (1 条评论)

发布
用户头像
你真棒,加油
2020 年 05 月 26 日 13:17
回复
没有更多了
ARTS - Week One