简陋VDOM
一个简陋的 VDOM
希望以后会慢慢完善吧…
1 | let data = { |
结构为:
1 | <div id="root"> |
希望以后会慢慢完善吧…
1 | let data = { |
结构为:
1 | <div id="root"> |
Let’s create a normal container first.
1 | class Container { |
But we should not directly manipulate the data in the container. We need a function to do this.
1 | // Container.prototype.map :: (a -> b) -> Container a -> Container b |
After passing the values in the Container to the map function, we can let them manipulate it; after the operation is completed, in order to prevent accidents, put them back into the Container they belong to. The result is that we can call the map continuously, running any function we want to run.
And the Functor is a container type that implements the map function and adheres to certain rules.
In Haskell, the Maybe type is defined as follow:
1 | data Maybe a = Just a | Nothing |
Maybe will check if its value is empty before calling the passed function. So let’s create a simple one.
1 | class Maybe { |
In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole. -- Wikipedia
Example:
1 | var compose = (f, g) => (x) => f(g(x)) |
In the definition of compose, g will be executed before f, thus creating a right-to-left data stream. This is much more readable than nesting a bunch of function calls.
Like many other functional programming concepts, associativity is derived from math.It is an expression in which the order of evaluation does not affect the end result provided the sequence of the operands does not get changed. -- JOSEPH REX
Example:
1 | 2 + (3 + 4) === 2 + 3 + 4 // true |
Because of the grouping of calls to compose is not important, so the result is the same. This also gives us the ability to write a variadic compose.
Like this:
1 | var last = compose(head, reverse) |
There is no standard answer on how to composition, just make it more reusable.
Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. -- Wikipedia
It mean this:
1 | let add = (x) => (y) => x + y |
In order to define functions more easily, we need the loadsh
, it will help us to currying the function.
1 | var _ = require("loadsh").curry |
function has special rules:
Although each input will only have one output, but for different inputs may have the same output.
Given all these, pure functions have a big set of advantages. They are easier to read and understand, as they do one thing. There is no need to look outside the function code and check for variables. There is no need to think how changing the value of the variable will affect other functions. No mutations in other functions will affect the result of the pure function for a specific input.
Pure functions are easier to test, as all dependencies are in the function definition and they do one thing.
For Example:
1 | var arr = [1, 2, 3] |
Another Example:
1 | // Impure |
sudo npm i yddict -g
, 用法: yd hello
存储方式:表头存放节点,相邻节点存放于之后的链表中。
** 使用 Map 模拟 **
1 | class Graph { |
1 | var graph = new Graph() |