SlideShare a Scribd company logo
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
toString().padStart(width) : cell.padEnd(width); }).join(''))).join
}; const proportion = (max, val) => Math.round(val * 100 / max); co
calcProportion = table => { table.sort((row1, row2) => row2[DENSITY
row1[DENSITY_COL]); const maxDensity = table[0][DENSITY_COL]; table
forEach(row => { row.push(proportion(maxDensity, row[DENSITY_COL]))
return table; }; const getDataset = file => { const lines = fs.read
FileSync(file, 'utf8').toString().split('n'); lines.shift(); lines
return lines.map(line => line.split(',')); }; const main = compose
(getDataset, calcProportion, renderTable); const fs = require('fs'
compose = (...funcs) => x => funcs.reduce((x, fn) => fn(x), x); con
DENSITY_COL = 3; const renderTable = table => { const cellWidth = [
8, 8, 18, 6]; return table.map(row => (row.map((cell, i) => { const
= cellWidth[i]; return i ? cell.toString().padStart(width) : cell.p
(width); }).join(''))).join('n'); }; const proportion = (max, val)
Прототипное
программирование
Тимур Шемсединов
github.com/HowProgrammingWorks
Chief Software Architect at Metarhia
Lecturer at Kiev Polytechnic Institute
github.com/tshemsedinov
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
1. point.__proto__ -> pointMethods
x: Number
y: Number
__proto__
point
move(x, y)
toString()
__proto__
pointMethods
point.__proto__
toString()
valueOf()
...
Object.prototype
pointMethods.__proto__
Object.prototype.__proto__
null
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
2. function Point(x, y) { ... }
x: Number
y: Number
__proto__
point
move(x, y)
toString()
constructor
__proto__
Point.prototype
__proto__
toString()
valueOf()
...
Object.prototype
null
__proto__ __proto__
from(obj)
prototype
__proto__
Point
constructor
prototype
apply, bind, call,
constructor
...
Function.prototype
__proto__
__proto__
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
3. class Point { ... }
x: Number
y: Number
__proto__
point
move(x, y)
toString()
constructor
__proto__
Point.prototype
__proto__
Object.prototype
null
__proto__ __proto__
from(obj)
prototype
__proto__
Point
constructor
prototype
Function.prototype
__proto__
__proto__
Object
Function
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
4. (#1, #2) square -> Square -> Rect
x: Number
y: Number
width: Number
height: Number
__proto__
square
move(x, y)
constructor
__proto__
Square.prototype
__proto__
Objectnull
__proto__
prototype
__proto__
Square
constructor
prototype
toString()
constructor
__proto__
Rect.prototype
prototype
__proto__
Rect
constructor
prototype
__proto__
Square.prototype
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
4. (#3, #4, #5) square -> Square -> Rect
x: Number
y: Number
width: Number
height: Number
__proto__
square
move(x, y)
constructor
__proto__
Square.prototype
__proto__
Objectnull
__proto__
prototype
__proto__
Square
constructor
prototype
toString()
constructor
__proto__
Rect.prototype
prototype
__proto__
Rect
constructor
prototype
__proto__
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
5. class Square extends Rect
x: Number
y: Number
width: Number
height: Number
__proto__
square
move(x, y)
constructor
__proto__
Square.prototype
__proto__
Object
__proto__
prototype
__proto__
class Square
constructor
prototype
toString()
constructor
__proto__
Rect.prototype
prototype
__proto__
class Rect
constructor
prototype
__proto__
__proto__
Function.prototype
__proto__

More Related Content

Prototype programming in JavaScript

  • 1. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c toString().padStart(width) : cell.padEnd(width); }).join(''))).join }; const proportion = (max, val) => Math.round(val * 100 / max); co calcProportion = table => { table.sort((row1, row2) => row2[DENSITY row1[DENSITY_COL]); const maxDensity = table[0][DENSITY_COL]; table forEach(row => { row.push(proportion(maxDensity, row[DENSITY_COL])) return table; }; const getDataset = file => { const lines = fs.read FileSync(file, 'utf8').toString().split('n'); lines.shift(); lines return lines.map(line => line.split(',')); }; const main = compose (getDataset, calcProportion, renderTable); const fs = require('fs' compose = (...funcs) => x => funcs.reduce((x, fn) => fn(x), x); con DENSITY_COL = 3; const renderTable = table => { const cellWidth = [ 8, 8, 18, 6]; return table.map(row => (row.map((cell, i) => { const = cellWidth[i]; return i ? cell.toString().padStart(width) : cell.p (width); }).join(''))).join('n'); }; const proportion = (max, val) Прототипное программирование Тимур Шемсединов github.com/HowProgrammingWorks Chief Software Architect at Metarhia Lecturer at Kiev Polytechnic Institute github.com/tshemsedinov
  • 2. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 1. point.__proto__ -> pointMethods x: Number y: Number __proto__ point move(x, y) toString() __proto__ pointMethods point.__proto__ toString() valueOf() ... Object.prototype pointMethods.__proto__ Object.prototype.__proto__ null
  • 3. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 2. function Point(x, y) { ... } x: Number y: Number __proto__ point move(x, y) toString() constructor __proto__ Point.prototype __proto__ toString() valueOf() ... Object.prototype null __proto__ __proto__ from(obj) prototype __proto__ Point constructor prototype apply, bind, call, constructor ... Function.prototype __proto__ __proto__
  • 4. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 3. class Point { ... } x: Number y: Number __proto__ point move(x, y) toString() constructor __proto__ Point.prototype __proto__ Object.prototype null __proto__ __proto__ from(obj) prototype __proto__ Point constructor prototype Function.prototype __proto__ __proto__ Object Function
  • 5. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 4. (#1, #2) square -> Square -> Rect x: Number y: Number width: Number height: Number __proto__ square move(x, y) constructor __proto__ Square.prototype __proto__ Objectnull __proto__ prototype __proto__ Square constructor prototype toString() constructor __proto__ Rect.prototype prototype __proto__ Rect constructor prototype __proto__ Square.prototype
  • 6. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 4. (#3, #4, #5) square -> Square -> Rect x: Number y: Number width: Number height: Number __proto__ square move(x, y) constructor __proto__ Square.prototype __proto__ Objectnull __proto__ prototype __proto__ Square constructor prototype toString() constructor __proto__ Rect.prototype prototype __proto__ Rect constructor prototype __proto__
  • 7. const fs = require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c 5. class Square extends Rect x: Number y: Number width: Number height: Number __proto__ square move(x, y) constructor __proto__ Square.prototype __proto__ Object __proto__ prototype __proto__ class Square constructor prototype toString() constructor __proto__ Rect.prototype prototype __proto__ class Rect constructor prototype __proto__ __proto__ Function.prototype __proto__