!! 圖多 !!
!! 以下的圖片都擷取自講者的投影片 !!
https://www.slideshare.net/ScottWlaschin/the-functional-programming-toolkit-ndc-oslo-2019-150648710
前言 會覺得 functional programming 很可怕,也許我們只是不熟悉這些在 FP 的字而已
如果把
functor 換成 mappable catamorphism 換成 collapsable monad 換成 chainable monoid 換成 aggregatable 就變得平易近人一些了 (?)
而且事實上,object oriented programming 也有一些看起來很可怕的字 w
The Functional Kit 這個影片主要在說 working with effects 的這幾個部分,也就是 functor, monad 和 applicative
Part 1. Core principles of statically-typed FP Functions are things function 是把輸入轉換成輸出的東西
function 是獨立的,不屬於任何 class,因為如此,function 是 reusable 的
function 可以是輸入、輸出甚至參數
https://www.youtube.com/watch?v=2JB1_e5wZmU
https://www.slideshare.net/ScottWlaschin/domain-modeling-made-functional-kandddinsky-2019
講者 Scott Wlaschin 是 Domain Modeling Made Functional 的作者
前言 之後主要從下面這個例子展開,展示 type 的重要和在設計中的美妙之處
type Contact = { FirstName: string MiddleInitial: string LastName: string EmailAddress: string IsEmailVerified: bool } Functional programming is really good for… Boring Line Of Business Applications (BLOBAs)
Part.1 The importance of design 下面是一個 F# 的程式碼,即使沒有學過 F# 甚至沒有學過程式語言,還是可以很清楚地猜到這應該是個撲克牌的遊戲
module CardGame = type Suit = Club | Diamond | Spade | Heart type Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King type Card = Suit * Rank type Hand = Card list type Deck = Card list type Player = { Name:string; Hand:Hand } type Game = { Deck:Deck; Players: Player list } type Deal = Deck –› (Deck * Card) type PickupCard = (Hand * Card) –› Hand 在這裡,動作被 model 成 function