點學寫靚PROGRAM

315 回覆
17 Like 1 Dislike
2017-04-25 19:54:21


有乜嘢伏? 真心請教!

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...

咁我都認同佢this嘅resolution係好好
我都成日用
2017-04-25 19:59:29


有乜嘢伏? 真心請教!

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 20:01: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; // 其他人有講, 我諗純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 係唔存在架
}

所以我先舉Java做例子
2017-04-25 20:05: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

算係架已經(最少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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦
2017-04-25 20:09: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

算係架已經(最少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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大
2017-04-25 20:12:19
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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己可以做FP, 你話lambda係咪好偉大

咁咋...
不過用lambda真係好爽, 除左c++
2017-04-25 20:17:45
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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.
2017-04-25 20:20:47
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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

人家Lambda Calculus係數學家紙上玩意, 無視電腦架構(其實發明出Lambda Calculus既時候電腦都仲未發明)
2017-04-25 20:24: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; // 其他人有講, 我諗純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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

人家Lambda Calculus係數學家紙上玩意, 無視電腦架構(其實發明出Lambda Calculus既時候電腦都仲未發明)

咁好多programming concept都係電腦發明之前就已經有
2017-04-25 20:24: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

算係架已經(最少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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

人家Lambda Calculus係數學家紙上玩意, 無視電腦架構(其實發明出Lambda Calculus既時候電腦都仲未發明)


我唔理!
2017-04-25 20:29:03
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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

人家Lambda Calculus係數學家紙上玩意, 無視電腦架構(其實發明出Lambda Calculus既時候電腦都仲未發明)

咁好多programming concept都係電腦發明之前就已經有

我有次同Professor傾計, 佢同我講好多依家CS既Algorithm其實係人地EE發明, 係電路入面solve過既solution
2017-04-25 22:43:39
fp寫左一排會覺得佢似係宗教團體
同埋街外嘅library質素好參差,
有d走火入魔嘅寫埋d純fp library, 看似好elegant,
咁用符號做func名寫到d code成篇數學proof都算...
d func唔肯加番正常名做alias都算...
見過有個仲因為個data type叫Alpha, 佢又要簡寫叫A, 如果係咁都好
佢用希臘字母個A, 即係一樣樣果個unicode character
仲要懶醒咁係份doc寫, 呢個唔係A黎架
雖然佢個api好fancy, d implementation都好fit use case, 但點用
fp嘅會係咁, 當然都有d靚lib係fp得黎, 既貼地又貼keyword
fp lang既non-fp library又會去番另一個極端

而家會用下d fp concept同constructs,
但用得最多嘅其實只係immutable, call下map/reduce幫手砌loop
local immutability係最易融入existing non-fp code base
返工都用得而且唔會比人鬧
2017-04-26 00:03:53
fp寫左一排會覺得佢似係宗教團體
同埋街外嘅library質素好參差,
有d走火入魔嘅寫埋d純fp library, 看似好elegant,
咁用符號做func名寫到d code成篇數學proof都算...
d func唔肯加番正常名做alias都算...
見過有個仲因為個data type叫Alpha, 佢又要簡寫叫A, 如果係咁都好
佢用希臘字母個A, 即係一樣樣果個unicode character
仲要懶醒咁係份doc寫, 呢個唔係A黎架
雖然佢個api好fancy, d implementation都好fit use case, 但點用
fp嘅會係咁, 當然都有d靚lib係fp得黎, 既貼地又貼keyword
fp lang既non-fp library又會去番另一個極端

而家會用下d fp concept同constructs,
但用得最多嘅其實只係immutable, call下map/reduce幫手砌loop
local immutability係最易融入existing non-fp code base
返工都用得而且唔會比人鬧

Agree, 所以我主張OOP/FP混用
互相取長補短
2017-04-26 03:39:20
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

I am surprised you ask this kind of question. I guess this is a javascript thing. Arrow functions is a shorthand for function definitions. But it is only great when we want to define function that does arbitrary thing.

Except for the "this" binding mechanism that is different. One thing crucial is how the Javascript engines would read them in different order.

f();
function f(){
console.log('hi');
}


the above code would work and print hi on console. on the other hand


f();
var f = () => console.log('hi');


Would give you error saying that f is not defined.
2017-04-26 05:11: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

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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

Most of the time you don't want to deal with lambda calculus. It is very inefficient. Usually it tells us if something is possible or not but not how long it takes.
2017-04-26 06:14:14

其實好多時候都會extend個新嘅exception class出嚟

具體係點
extends 邊個, 改D咩? 定係加自訂field入去?
巴打實際Project都係咁做?

public class ExceptionExample {
class AException extends Exception {
AException(){
super();
}
}

class BException extends Exception {
BException(){
super();
}
}

public void simulate(){
try{
switch(1){
case 0: throw new AException();
case 1: throw new BException();
}
}catch(AException e){
System.out.println("Exception A");
}catch(BException e){
System.out.println("Exception B");
}catch(Exception e){
System.out.println("not expected");
}
}

public static void main(String[] args) {
ExceptionExample a = new ExceptionExample();
a.simulate();
}
}
2017-04-26 06:21:09

其實好多時候都會extend個新嘅exception class出嚟

具體係點
extends 邊個, 改D咩? 定係加自訂field入去?
巴打實際Project都係咁做?

public class ExceptionExample {
class AException extends Exception {
AException(){
super();
}
}

class BException extends Exception {
BException(){
super();
}
}

public void simulate(){
try{
switch(1){
case 0: throw new AException();
case 1: throw new BException();
}
}catch(AException e){
System.out.println("Exception A");
}catch(BException e){
System.out.println("Exception B");
}catch(Exception e){
System.out.println("not expected");
}
}

public static void main(String[] args) {
ExceptionExample a = new ExceptionExample();
a.simulate();
}
}

自己執返啲indentation,食曬,你quote個reply好似會出返曬嚟。

用依個方法,你可以有一個好嘅Exception名(Exception同Error係唔一樣嘅嘢,Error係一啲fatal嘅嘢,無得resolve,exception相對上係預咗會出現,你預先plan定去handle),之後catch返相應Exception去handle,唔會話throw完都唔知邊到throw,點解throw。

如果所有嘢都係generic Exception,其實好難處理。
2017-04-26 06:43:47
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

I am surprised you ask this kind of question. I guess this is a javascript thing. Arrow functions is a shorthand for function definitions. But it is only great when we want to define function that does arbitrary thing.

Except for the "this" binding mechanism that is different. One thing crucial is how the Javascript engines would read them in different order.

f();
function f(){
console.log('hi');
}


the above code would work and print hi on console. on the other hand


f();
var f = () => console.log('hi');


Would give you error saying that f is not defined.


Ha ha, I actually know the difference between traditional function definition and the short-hand one; the former is subject to HOISTING but the latter is not. Such a situation is very similar to the fact that var(function-scoped) is subject to hoisting but let(block-scoped) is not.

a = 10;
if (true) {
console.log(a)
var a = 20;
console.log(a);
}


Output:
10
20


a = 10;
if (true) {
console.log(a)
let a = 20;
console.log(a);
}


Output:
ReferenceError




Anyway, hoisting is actually something you should avoid because it might probably pollute the program with global variables and undefined functions.
2017-04-26 09:09:07
FP好多時係stateless, this 呢個字係OOP 野,同FP炒埋緊係有問題
2017-04-26 09:48:06
FP好多時係stateless, this 呢個字係OOP 野,同FP炒埋緊係有問題

有呢種可能,不過上面舉既例子純粹因為JavaScript 既this與眾不同
2017-04-26 09:52:08
FP好多時係stateless, this 呢個字係OOP 野,同FP炒埋緊係有問題

有呢種可能,不過上面舉既例子純粹因為JavaScript 既this與眾不同

而且仲有d bind/apply tricks去玩個this
不過js轉寫fp, 或者起碼轉哂stateless就冇呢個問題
2017-04-26 11:36:42
雖然我自己做Programmer都係幾年時間,唔算長,我追完呢個POST都有好多位一頭煙/睇完好累
但我想問下寫靚Program唔係好應該見到咩CASE就應用番咩最好既工具咩 ,唔係應該寫完堆CODE之後,你睇落個logic係make sense,順眼架咩
我見到上面有D CASE明明直接可以用While,又要特登用For(;true;)咁,雖然呢D方法你要寫係可以While代替For,For可以代替While,但自問感覺係好怪好唔舒服
可能我真係無知,所以想請教下
2017-04-26 12:10:23
雖然我自己做Programmer都係幾年時間,唔算長,我追完呢個POST都有好多位一頭煙/睇完好累
但我想問下寫靚Program唔係好應該見到咩CASE就應用番咩最好既工具咩 ,唔係應該寫完堆CODE之後,你睇落個logic係make sense,順眼架咩
我見到上面有D CASE明明直接可以用While,又要特登用For(;true;)咁,雖然呢D方法你要寫係可以While代替For,For可以代替While,但自問感覺係好怪好唔舒服
可能我真係無知,所以想請教下

唔會無知
因為靚同順眼都幾主觀
未學過OOP去睇OOP既code -> fail
未學過FP去睇FP既code -> fail
未學過Design Pattern去睇Design Pattern既code -> fail
2017-04-26 21:17: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; // 其他人有講, 我諗純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 係唔存在架
}

所以我先舉Java做例子

java8唔係有lambda啦咩
c++都有lambda啦

就係因為有左lambda, 依家Java都話自己做到FP, 你話lambda係咪好偉大


連Haskell呢隻Pure FPL都implement唔到100% Lambda Calculus, Java呀, Scala呀嗰啲所謂FP libraries就唔好存有太大期望了.

Most of the time you don't want to deal with lambda calculus. It is very inefficient. Usually it tells us if something is possible or not but not how long it takes.


其實一早都知, performance/efficiency係FL嘅死症(以前電腦太慢, 所以嗰陣時唔興), 但電腦嘅運算能力一路都以好驚人嘅速度增長(1960年嘅超級電腦得250kFLOPS[i.e.25x10^4 FLOPS], 2016年已經去到93.01 PFLOPS[i.e.93.01x10^18 FLOPS] ), 所以宜家一般PC用imperative/OO/functional programs, 個performance分別已經唔係太大, that's why FL近年可以興得起! Anyway, 要performance嘅, 始終都係用番C, C++呢啲好啲嘅!
2017-04-26 22:54:46
雖然我自己做Programmer都係幾年時間,唔算長,我追完呢個POST都有好多位一頭煙/睇完好累
但我想問下寫靚Program唔係好應該見到咩CASE就應用番咩最好既工具咩 ,唔係應該寫完堆CODE之後,你睇落個logic係make sense,順眼架咩
我見到上面有D CASE明明直接可以用While,又要特登用For(;true;)咁,雖然呢D方法你要寫係可以While代替For,For可以代替While,但自問感覺係好怪好唔舒服
可能我真係無知,所以想請教下


做左就兩年, 基本上都係返工邊做邊學, 見到個Post啲 terms 真係R曬頭
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞