點學寫靚PROGRAM

315 回覆
17 Like 1 Dislike
2017-04-25 14:29:46
何必驕枉過正, 我一直都係話三思先用if, if對底層係好重要
但好多時咩為之靚code唔係我扔條絕對rule出黎就可以寫到
寫code前三思好重要

唔明有咩問題,我未見過有program冇if
sorting點樣唔用if寫到

追post pls

睇唔到邊到有解釋同理據先問
design pattern 有冇if 對真正 寫logic需唔需要if冇衝突
readability depends on點develop,if唔係主因
以前讀hardware if都唔係cost最expensive

何必驕枉過正, 我一直都係話三思先用if, if對底層係好重要
但好多時咩為之靚code唔係我扔條絕對rule出黎就可以寫到
寫code前三思好重要
2017-04-25 14:32:54
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂

of course, I am just writing them out separately to enhance readability.


你分開兩個variable對初學者要一步步係易睇d,
但要比人見到fp可以幾簡潔要show埋後面,
睇下個loop可以點樣慢慢變成一句
而你可以一眼望出佢做緊乜

我會話4段加埋先係一個complete example

Well said. I agree.
2017-04-25 14:46:20
其實OOP同FP兩樣野無衝突,而且work得好好添

OOP畫class diagram, define完interface之後
點樣implement可以換FP思維去做

同時可以好好組織project structure 同控制data flow

OOP本身個object係stateful, 自己顧自己

我諗你想講嘅係implement果時,
用埋immutable個思維, 唔改internal state,
每次要改都變成return一個新既object出黎

design係互相有幫助, 如果考慮埋fp implement,
你一開始諗data flow就會預埋點先真正uni-direction
唔驚d data中間比人改左就輕輕鬆鬆等收工

可以倒轉想,FP可以改善OOP stateful引起既問題。例如js可以用immutableJS將所有variable變成immutableJS
其實關於state可以再進一步講多d
不論class或是web application的API design可以看看CQRS這套design principle 進一步避免stateful引起的問題
2017-04-25 14:49:40
strong post lm
2017-04-25 15:31:27
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你
2017-04-25 16:11:57
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"
2017-04-25 16:14:36
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax
2017-04-25 16:18:29
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎
2017-04-25 16:21:58
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

但你個名
2017-04-25 16:29:00
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

已經用緊async/await
返唔到轉頭
callback hell 拜鳩拜
2017-04-25 16:33:16
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

已經用緊async/await
返唔到轉頭
callback hell 拜鳩拜

d promise寫得好d code都拍得好靚架
假設我d code都乖乖地咁拍左邊係咁then,
呢d情況下async await會差幾遠
2017-04-25 17:19:53
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0;
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

第一個function仲用埋global variable

屌 sor

javascript 既話用strict mode

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

但你個名

Branding Guidelines for Angular and AngularJS
https://angularjs.blogspot.hk/2017/01/branding-guidelines-for-angular-and.html
2017-04-25 19:08:07

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

已經用緊async/await
返唔到轉頭
callback hell 拜鳩拜

d promise寫得好d code都拍得好靚架
假設我d code都乖乖地咁拍左邊係咁then,
呢d情況下async await會差幾遠

但唔記得await就爽yy
試過
2017-04-25 19:19:58

how would it affect my code?

係function打global varibale會鬧你

直接用ES6寫可以唔洗打"use strict"

巴打有冇追es7 / 其他 experimental feature
有annotation, async await 果d
追唔哂, 我淨係寫babel-presets-es2015包嘅野
最多寫react嘅時候會加埋jsx syntax

寫緊Angular 4, 當然日日寫啦😎

已經用緊async/await
返唔到轉頭
callback hell 拜鳩拜

d promise寫得好d code都拍得好靚架
假設我d code都乖乖地咁拍左邊係咁then,
呢d情況下async await會差幾遠

但唔記得await就爽yy
試過



我寫過其他fp lang, js嘅promise都算monad (定monoid?)
Promise.prototype.then做哂map同flatmap
有Promise.resolve同Promise.reject幫d簡單func直出結果
仲有Promise.all將array of promise轉做promise of array
啱啱望過下async await, 我唔覺得佢嘅syntax比promise優勝
2017-04-25 19:24:46
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15
2017-04-25 19:31:25
value = condition ? 1 : 2;

呢 d算唔算用左 if ?
2017-04-25 19:33:13
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand
2017-04-25 19:36:15
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand


有乜嘢伏? 真心請教!
2017-04-25 19:40:57
[quote][quote][quote][quote][quote][quote]d promise寫得好d code都拍得好靚架
假設我d code都乖乖地咁拍左邊係咁then,
呢d情況下async await會差幾遠

native Promise 根本係未完成品
又無defer,又無cancel, ajax做到既野佢仲未implement 晒
只係執好左ajax callback同error handling既code syntax order, 等你寫code唔洗打晒交咁。
好多nodejs既developer 都會用third party 既Promise library, 無他,因為native Promise除左1個all同race, 咩control flow都無畀你...純native Promise去寫有dependency既async request task又係容易寫到好扭計
唔係寫唔到,不過如果你想寫得靚又想做到個program error-prone中途插error catch落下retry就難d, 你d code又會好易打交
最起碼用async await既code structure直無贏晒
2017-04-25 19:43:26
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand


有乜嘢伏? 真心請教!


BTW, ES6個class其實都係Prototype-based, 好多人反對用, 如果要寫class, 我都會用番ES5嗰隻算!
2017-04-25 19:47:04
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand


有乜嘢伏? 真心請教!

function Whatever() {
this.x = -10;
this.abs = () => Math.abs(this.x);
}

let w = new Whatever();
w.abs(); // 10

lambda個this scope會跟左function個this scope

傳統function你要做呢樣野

function Whatever() {
var self = this;
this.x = -10;
this.abs = function () {
return Math.abs(self.x); // 用 this 你就食左屎
};
}

babel transpile果陣會自動幫lambda搵定個this scope

我記得有frd用lambda中過伏, 唔記得detail
但係至少由此可見lambda同function嘅sematics係唔同
如果個program冇object果類野唔洗reference this就ok

即刻寫可能1999, 有問題我再講
2017-04-25 19:47:35
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand

算係架已經(最少Java班developer恨左好耐)
只係睇係從咩角度講,lambda提供左first class function 既既environment得你可以寫出FP way
2017-04-25 19:52:39
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand

算係架已經(最少Java班developer恨左好耐)
只係睇係從咩角度講,lambda提供左first class function 既既environment得你可以寫出FP way

function本身就係js既first class citizen,
同lambda出現根本冇關係
只係好多人成日都當左係shorthand,
又或者當function係過氣嘅寫法
冇留意到佢地semantics係有分別

仲有個case就係variable length argument

function sum() {
return arguments.reduce((arr, x) => arr + x); // 貪方便寫番lambda算
}

const sum = () => {
// arguments 係唔存在架
}
2017-04-25 19:52:54


有乜嘢伏? 真心請教!

function Whatever() {
this.x = -10;
this.abs = () => Math.abs(this.x);
}

let w = new Whatever();
w.abs(); // 10

lambda個this scope會跟左function個this scope

傳統function你要做呢樣野

function Whatever() {
var self = this;
this.x = -10;
this.abs = function () {
return Math.abs(self.x); // 用 this 你就食左屎
};
}

babel transpile果陣會自動幫lambda搵定個this scope

我記得有frd用lambda中過伏, 唔記得detail
但係至少由此可見lambda同function嘅sematics係唔同
如果個program冇object果類野唔洗reference this就ok

即刻寫可能1999, 有問題我再講


我可以肯肯地同你講ES6故意引入lambda去改this絕對係萬眾期待, 所有人讚不絕口既feature...
2017-04-25 19:53:12
FP 真係會幫到clean code
因為會令我地學識點將大件的野拆細來做

仲有higher order functions is a must for clean code

For example: 如果我地有個list of numbers,之後我地想搵所有number 的 absolute value 之和。

可能你會好想咁寫:
function f(xs){
var sum = 0; // 其他人有講, 我諗純typo姐
for(var i = 0; i < xs.length; i++){
sum += abs(xs[i]);
}
return sum;
}

但靚d 的方法係:
function f(xs){
var abs_xs = xs.map(abs);
var sum = abs_xs.reduce((x, y) => x+y, 0);
return sum;
}

啱啱入黎
專業寫fp係第二個function根本無var
function abs_sum(xs) {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
}

又或者可以直接reduce
xs.reduce((acc, x) => acc + abs(x), 0)

不過呢d都係用緊higher order function,
想寫hof既話好快會發現d hof已經比人寫哂


Why not define the abs function first? (Maybe I'm wrong and abs is assumed to have been defined or imported from a maths library)

Also why not change to ES2015 (or ES6) style of defining a function? (It's now in 2017 and ES6 has already been in effect for quite a while!!!)

If I were you, I would add/change the code as follows:
const abs = (x) => {return x < 0 ? -x : x};

const abs_sum = (xs) => {
return xs.map(abs).reduce((acc, x) => acc + x, 0);
};


e.g. under node:
> abs_sum([1,2,3,-4,-5]);
15

你唔寫class咁用哂lambda梗係冇問題
但係lambda絕對唔係function既short hand,
this is so 伏

用冇問題, 但要記住lambda唔係short hand

算係架已經(最少Java班developer恨左好耐)
只係睇係從咩角度講,lambda提供左first class function 既既environment得你可以寫出FP way

function本身就係js既first class citizen,
同lambda出現根本冇關係
只係好多人成日都當左係shorthand,
又或者當function係過氣嘅寫法
冇留意到佢地semantics係有分別

仲有個case就係variable length argument

function sum() {
return arguments.reduce((arr, x) => arr + x, 0); // 貪方便寫番lambda算
}

const sum = () => {
// arguments 係唔存在架
}
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞