June 27, 2023
190

# Functional Ruby

June 27, 2023

## Transcript

1. 為你⾃⼰學 系列單元
ޒഒላӃ
Functional Ruby

2. ޒഒላӃ
വᏐࣜఔࣜઃܭ
'VODUJPOBM1SPHSBNNJOH '1

3. ޒഒላӃ
೺ਞྻཫॴ༗తحᏐ
ငҎ೭ޙฒܭࢉશ෦ݩૉ័࿨

4. ޒഒላӃ
+4
// JavaScript

const list = [1, 2, 3, 4, 5]

const odd_numbers = []

// 挑出奇數

for (let i = 0; i < list.length; i++) {

const el = list[i]

if (el % 2 === 1) {

odd_numbers.push(el)

}

}

const triple_numbers = []

// 乘 3 倍

for (let i = 0; i < odd_numbers.length; i++) {

triple_numbers.push(odd_numbers[i] * 3)

}

let total = 0

// 計算總和

for (let i = 0; i < triple_numbers.length; i++) {

total += triple_numbers[i]

}

console.log(total) // 27

5. ޒഒላӃ
+4
// JavaScript

const list = [1, 2, 3, 4, 5]

const result = list.filter(i => i % 2 === 1)

.map(i => i * 3)

.reduce((acc, cv) => acc + cv, 0)

console.log(result) // 27

6. ޒഒላӃ
+4
// JavaScript

const list = [1, 2, 3, 4, 5]

const oddOnly = (i) => i % 2 === 1

const triple = (i) => i * 3

const sum = (acc, cv) => acc + cv

const result = list.filter(oddOnly)

.map(triple)

.reduce(sum, 0)

console.log(result) // 27

7. ޒഒላӃ
ॄኄੋ'1ʁ

8. ޒഒላӃ
ఔࣜޠݴయൣ
1SPHSBNNJOH1BSBEJHNT

9. ޒഒላӃ
෺݅ಋ޲ఔࣜઃܭ
0CKFDU0SJFOUFE1SPHSBNNJOH

10. ޒഒላӃ
෧᧋៺ঝ
&ODBQTVMBUJPO*OIFSJUBODF

11. ޒഒላӃ
വᏐࣜఔࣜઃܭ
'VODUJPOBM1SPHSBNNJOH

12. ޒഒላӃ
വᏐ
'VODUJPO

13. ޒഒላӃ
ॄኄੋവᏐʁ

14. ޒഒላӃ
f(x) = 3x + 2

15. ޒഒላӃ
വᏐ༌ೖ值ᢛ༌ग़值೭ؒత᮫܎

16. ޒഒላӃ
७വᏐ
1VSF'VODUJPO

17. ޒഒላӃ
1VSF'VODUJPO
༌ग़݁Ռ୞᪑༌ೖ值༗᮫

18. ޒഒላӃ
+4
# 不純

def one_weeks_later

Time.now + 7.days

end

19. ޒഒላӃ
+4
# 比較純

def one_weeks_later(current_time)

current_time + 7.days

end

20. ޒഒላӃ
1VSF'VODUJPO
ෆ။଄੒㐫ଶվᏓ

21. ޒഒላӃ
ᔒ༗෭࡞༻
/PTJEFFGGFDU

22. ޒഒላӃ
+4
# 不純

Book.create(title: '為你⾃⼰學 Ruby')

end

23. ޒഒላӃ
+4
# 不純

def replace_element(arr, idx, value)

arr[idx] = value

arr

end

list = [1, 2, 3]

result = replace_element(list, 0, "a")

p result # ["a", 2, 3]

p list # ???

24. ޒഒላӃ
+4
# 比較純

def replace_element(arr, idx, value)

dup_arr = arr.dup

dup_arr[idx] = value

dup_arr

end

list = [1, 2, 3]

result = replace_element(list, 0, "a")

p result # ["a", 2, 3]

p list # ???

25. ޒഒላӃ
ෆՄᏓੑ
*NNVUBCJMJUZ

26. ޒഒላӃ
+4
// JavaScript

const list = [1, 2, 3]

const otherList = list

console.log(list) // [1, 2, 3]

otherList[0] = "a"

console.log(list) // ???

27. ޒഒላӃ
+4
// Rust

fn main() {

let list = [1, 2, 3];

list[0] = 100; // 預設是不能修改的

println!("{:?}", list);

}

28. ޒഒላӃ
+4
# Elixir

list = [1, 2, 3]

list[0] = 100 # 預設是不可修改的

29. ޒഒላӃ
'1JO3VCZ

30. ޒഒላӃ
+4
list = [1, 2, 3, 4, 5]

double_list = list.map { |n| 2 * n }

odd_numbers = list.filter { |n| n.odd? }

p double_list # [2, 4, 6, 8, 10]

p odd_numbers # [1, 3, 5]

31. ޒഒላӃ
3VCZฒෆࢉੋਅਖ਼త'1-BOHVBHF

32. ޒഒላӃ
വᏐੋҰ౳ެຽ
'JSTU\$MBTT\$JUJ[FO

33. ޒഒላӃ
+4
// JavaScript

const list = [1, 2, 3, 4, 5]

const oddOnly = (i) => i % 2 === 1

const triple = (i) => i * 3

const sum = (acc, cv) => acc + cv

const result = list.filter(oddOnly)

.map(triple)

.reduce(sum, 0)

console.log(result) // 27

34. ޒഒላӃ
3VCZॴ༗త౦੢౎ੋ෺݅
&WFSZUIJOHJO3VCZJTBOPCKFDU

35. ޒഒላӃ
ୠଖመ༗ࠣ౦੢ฒෆੋ෺݅

36. ޒഒላӃ
ᩋํ๏ʮ෺݅Խʯ

37. ޒഒላӃ
+4
# 把⽅法物件化

a + b

end

m2 = Proc.new { |x, y| add(x, y) }

m3 = -> (x, y) { add(x, y) }

puts m1.call(1, 2) # 3

puts m2.call(3, 4) # 7

puts m3.call(5, 6) # 11

38. ޒഒላӃ
ෆՄᏓੑ
*NNVUBCJMJUZ

39. ޒഒላӃ
+4
# Data.define 是 Ruby 3.2 之後才加進來的新功能

Location = Data.define(:x, :y)

point = Location.new(0, 0)

puts point.x, point.y

point.x = 100 # 錯誤

40. ޒഒላӃ
ိᴍෳᯑత

41. ޒഒላӃ
ᐬཬԽ
\$VSSZJOH

42. ޒഒላӃ
೺ݪຊधཁଟݸჩᏐతവᏐ
᫚׵੒Ұܥྻ୞ཁݸჩᏐతവᏐతաఔ

43. ޒഒላӃ
+4

a + b + c

end

# 柯⾥化

-> (y) {

-> (z) {

}

}

}

44. ޒഒላӃ
+4
# 分開寫

puts result

# 或串在⼀起寫

45. ޒഒላӃ
+4

a + b + c

end

# 柯⾥化

# curried_add = -> (x) {

# -> (y) {

# -> (z) {

# }

# }

# }

# Ruby 內建的⽅法

46. ޒഒላӃ
ภവᏐ
1BSUJBM'VODUJPOT

47. ޒഒላӃ
෦份BQQMZݪ࢝വᏐɼઌݻఆവᏐత෦෼
ჩᏐɼճၚ઀डႫᰨჩᏐత৽വᏐ

48. ޒഒላӃ
+4

a + b + c

end

# Partial Function

# 先給 1 個參數

# 剩下的 2 個參數之後再⼀次給⾜

49. ޒഒላӃ
+4

a + b + c

end

# Partial Function

# 或是先給 2 個參數

# 最後 1 個參數之後再給

50. ޒഒላӃ
+4

a + b + c

end

# 利⽤內建的 curry ⽅法

# 先給 1 個參數

# 剩下的參數之後再⼀次給⾜

51. ޒഒላӃ
വᏐ߹੒
'VODUJPO\$PNQPTJUJPO

52. ޒഒላӃ
ઌՃɼ࠶Ճഒɼ࠷ޙ࠶ฏํʂ

53. ޒഒላӃ
+4
// JavaScript

const add_one = (n) => n + 1

const double = (n) => n * 2

const square = (n) => n * n

54. ޒഒላӃ
؅ઢ
1JQF

55. ޒഒላӃ
+4
# Elixir

add_one = fn x -> x + 1 end

double = fn x -> x * 2 end

square = fn x -> x * x end

|> double.()

|> square.()

IO.inspect result # 144

56. ޒഒላӃ
3VCZᔒ༗Ṝኄํศత౦੢

57. ޒഒላӃ
+4
add_one = -> (n) { n + 1 }

double = -> (n) { n * 2 }

square = -> (n) { n * n }

# 先加 1，再加倍，最後再平⽅

.compose(square)

puts composed_fn.call(5) # 144

58. ޒഒላӃ
+4
add_one = -> (n) { n + 1 }

double = -> (n) { n * 2 }

square = -> (n) { n * n }

# 先加 1，再加倍，最後再平⽅

composed_fn = add_one >> double >> square

puts composed_fn.call(5) # 144

59. ޒഒላӃ
޻঎෰຿