wakamsha
May 19, 2018
3k

JavaScript ( 時々 TypeScript ) で ゆるやかにはじめる関数型プログラミング

https://javascript-fes.doorkeeper.jp/events/73314
#jsfes

May 19, 2018

Transcript

1. +BWB4DSJQU ࣌ʑ5ZQF4DSJQU Ͱ ΏΔ΍͔ʹ͸͡ΊΔؔ਺ܕϓϩάϥϛϯά Introducing functional programming w/ JavaScript (

& sometimes TypeScript ) ॳՆͷ+BWB4DSJQUࡇ  !XBLBNTIB /BPLJ:"."%"

4. ['foo', 'bar', 'baz'].map((val: string) => { return `\${val}!!!`; }); //=>

['foo!!!', 'bar!!!', 'baz!!!'] function double(x: number): number { return x * 2; } double(3); //=> 6
5. ['foo', 'bar', 'baz'].map((val: string) => { return `\${val}!!!`; }); //=>

['foo!!!', 'bar!!!', 'baz!!!'] function double(x: number): number { return x * 2; } double(3); //=> 6 ͲͪΒ΋ؔ਺ܕϓϩάϥϛϯάͷఆٛʹଈͨ͠΋ͷͰ͢
6. ['foo', 'bar', 'baz'].map((val: string) => { return `\${val}!!!`; }); //=>

['foo!!!', 'bar!!!', 'baz!!!'] function double(x: number): number { return x * 2; } double(3); //=> 6 ͦ΋ͦ΋ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ ෳ਺ͷࣜΛؔ਺ͷద༻ʹΑͬͯ૊Έ߹ΘͤΔϓϩάϥϛϯάελΠϧ ؔ਺߹੒ ෭࡞༻ͷແ͍ؔ਺ΛϝΠϯʹѻ͏ϓϩάϥϛϯάελΠϧ ७ਮ
7. ['foo', 'bar', 'baz'].map((val: string) => { return `\${val}!!!`; }); //=>

['foo!!!', 'bar!!!', 'baz!!!'] function double(x: number): number { return x * 2; } double(3); //=> 6 ରٛʹҐஔ͢Δͷ͕ʰखଓ͖ܕϓϩάϥϛϯάʱ ม਺Λഁյతʹॻ͖׵͑ΔͳͲɺ෭࡞༻Λۦ࢖͢Δ ໨తͷͨΊʹ࣮ߦ͢΂͖Ұ࿈ͷεςοϓΛҰͭͷؔ਺ʹ࣋ͨͤΔ ఆٛͨؔ͠਺͸ϓϩάϥϜ࣮ߦதͷ೚ҙͷλΠϛϯάͰݺͼग़ͤΔ
8.  ؔ਺߹੒  খ͞ͳؔ਺Λ૊Έ߹ΘͤΔͱ͍͏͜ͱ  ߴ֊ؔ਺  ७ਮ  ෭࡞༻Λແ͘͢ͱ͍͏͜ͱ

 ෆมੑ *NNVUBCMF   ࢀরಁաੑ  ͜Ε͔Βؔ਺ܕϓϩάϥϛϯάΛֶͿਓ΁ ຊ೔͓࣋ͪؼΓ͍͖͍ͨͩͨ͜ͱ 1PJOUTUP MFBSO

11. ͱɺͦͷલʹߴ֊ؔ਺ʹ͍ͭͯ [1, 4, 9, 16].map(x => x * 2); ['spray',

'limit', 'exuberant', 'present'].filter(word => word.length > 6 ); "SSBZͷ*UFSBUJWFͳϝιου͸ؔ਺ΛҾ਺ʹऔΔ ؔ਺ΛҾ਺PS໭Γ஋ͱͯ͠ѻ͏΋ͷΛʰߴ֊ؔ਺ʱͱ͍͏ ߴ֊ؔ਺͸ؔ਺Λ߹੒͢Δ΋ͷͰ͋Δ

13. /** * ཁ݅: ͋Δ਺஋ʹ 1 Λ଍ͯ͠ 2ഒͨ݁͠ՌΛฦ͢ػೳ */ // Ұͭͷؔ਺Ͱ࣮ݱ͢Δͱ͜͏ͳΔ

// ಛԽ͗ͯͯ͢͠൚༻ੑʹ͚ܽΔͨΊɺอकੑ΋௿͍ function addOneTimesTwo(x: number): number { return (x + 1) * 2; } // Ҿ਺ʹ 1 Λ଍͚ͩ͢ function addOne(x: number): number { return x + 1; } // Ҿ਺Λ 2ഒ͢Δ͚ͩ function timesTwo(x: number): number { return x * 2; }

16. function compose(fn1, fn2) { return function(x0) { return fn1(fn2(x0)); }

} // TypeScript Ͱॻ͘ͱ͜Μͳײ͡ function compose<V0, T1, T2>( fn1: (x: T1) => T2, fn2: (x0: V0) => T1, ): (x0: V0) => T2 { return function(x0: V0): T2 { return fn1(fn2(x0)); }; }

compose<number, number, number>(timesTwo, addOne)(2); //=> 6

compose<number, number, number>(timesTwo, addOne)(2); //=> 6 ؔ਺߹੒ ؔ਺͸ͻͱ·ͱ·Γʹͤͣɺࡉ͔͘෼ղ͢Δ খ͞ͳؔ਺Λ૊Έ߹Θͤͯେ͖ͳػೳΛ࣮ݱ͢Δ খ͘͞୯७ͳؔ਺͸ɺͦΕ͚ͩ൚༻ੑ͕ߴ͍ 6OJU5FTUΛॻ͘ͷ͕༰қʹͳΔ

21. // 1 function sum(x: number, y: number): number { return

x + y; } // 2 let i: number = 0; function sum2(x: number, y: number): number { i += x + y; return x + y; } // 3 function sum3(x: number, y: number): number { return x + y + i; } // 4 function sum4(x: number, y: number): number { const result = x + y; console.log(result); return result; }
22. // 1 function sum(x: number, y: number): number { return

x + y; } // 2 let i: number = 0; function sum2(x: number, y: number): number { i += x + y; return x + y; } // 3 function sum3(x: number, y: number): number { return x + y + i; } // 4 function sum4(x: number, y: number): number { const result = x + y; console.log(result); return result; } Ұͭ໨͚͕ͩ७ਮͳؔ਺Ͱ͢ ଞ͸શͯʰෆ७ʱͳؔ਺Ͱ͢
23. // 1 function sum(x: number, y: number): number { return

x + y; } // 2 let i: number = 0; function sum2(x: number, y: number): number { i += x + y; return x + y; } // 3 function sum3(x: number, y: number): number { return x + y + i; } // 4 function sum4(x: number, y: number): number { const result = x + y; console.log(result); return result; } ७ਮͳؔ਺ͱ͸ ໭Γ஋͸ඞͣҾ਺ͱͯ͠༩͑ΒΕͨ஋͔ΒͷΈܭࢉ͞ΕΔ ؔ਺ͷ֎෦Ͱมߋ͞ΕΔՄೳੑͷ͋ΔσʔλʹҰ੾ґଘ͠ͳ͍ ؔ਺࣮ߦ෦ͷ֎ଆʹଘࡏ͢ΔԿ͔͠Βͷঢ়ଶΛҰ੾มߋ͠ͳ͍
24. // Ҿ਺ͷ஋Λ଍͠ࢉ͚ͨͩ͠ ( ७ਮ ) function sum(x: number, y: number):

number { return x + y; } // άϩʔόϧม਺Λมߋ͍ͯ͠Δ let i: number = 0; function sum2(x: number, y: number): number { i += x + y; return x + y; } // ໭Γ஋͕Ҿ਺͚ͩ͡Όͳ͘άϩʔόϧม਺ʹґଘͯ͠Δ function sum3(x: number, y: number): number { return x + y + i; } // console.log ͱ͍͏׬શʹ֎ͷੈքʹׯবͯ͠Δ function sum4(x: number, y: number): number { const result = x + y; console.log(result); return result; }

27. ෭࡞༻Λແͨ͘͢ΊͷୈҰา let i: number = 0; function sum3(x: number, y:

number, i: number): number { return x + y + i; } ॲཧʹඞཁͳσʔλ͸શͯҾ਺ͱͯ͠ड͚औΔ άϩʔόϧม਺΋Ҿ਺ͱͯ͠ड͚औΕ͹0,

29. class User { constructor(private firstName: string, private lastName: string) {}

public updateFirstName(name: string): User { return new User(name, this.lastName); } public updateLastName(name: string): User { return new User(this.firstName, name); } public greet(): string { return `hello, \${this.firstName} \${this.lastName}!!`; } }
30. class User { constructor(private firstName: string, private lastName: string) {}

public updateFirstName(name: string): User { return new User(name, this.lastName); } public updateLastName(name: string): User { return new User(this.firstName, name); } public greet(): string { return `hello, \${this.firstName} \${this.lastName}!!`; } } ΦϒδΣΫτ͸\$MBTTͱͯ͠ఆٛ͢Δ ֤ϝιουͷ࠷ޙʹࣗ਎ͷ৽͍͠ΠϯελϯεΛ໭Γ஋ʹฦ͢ ຖճ৽͍͠ΠϯελϯεΛͦͷ࣌ͷ಺෦஋͔Βੜ੒͢Δ ݺͼग़͠ͷίϯϑϦΫτ͕ൃੜ͠ͳ͍