ก่อนจะเจาะจงไปที่สถานการณ์ของเรา เรามาถอยกลับมามองภาพรวมกันก่อน โดยทั่วไปแล้ว ถ้าเรามี function ที่สร้าง Result หลายๆ ตัว เราจะ compose มันเข้าด้วยกันอย่างสะอาดได้อย่างไร
นี่คือภาพแสดงปัญหานี้ function ปกติสามารถมองเป็นรางรถไฟเส้นหนึ่งได้:
แต่ function ที่มี output เป็น Result สามารถมองเป็นรางรถไฟที่แยกออกเป็นสองทาง แบบนี้:
ผมจะเรียกฟังก์ชันแบบนี้ว่า switch functions ตามอุปมาเรื่องรถไฟ บางทีก็เรียกว่าฟังก์ชันแบบ “monadic” เหมือนกัน
แล้วเราจะเชื่อม function “switch” สองตัวนี้เข้าด้วยกันอย่างไร ถ้า output สำเร็จ เราก็อยากไปต่อที่ function ถัดไปใน ลำดับ แต่ถ้า output เป็น error เราก็อยากข้ามมันไปเลย ตามที่แสดงใน figure .
เราจะรวม switch สองตัวนี้เข้าด้วยกันยังไง เพื่อให้เส้นทาง failure ทั้งสองเชื่อมกัน คำตอบชัดเจนอยู่แล้ว—แบบนี้:
และถ้าคุณเชื่อม step ทั้งหมดใน pipeline ด้วยวิธีนี้ คุณจะได้สิ่งที่ผมเรียกว่าโมเดล “two-track” ของ error handling หรือ “railroad-oriented programming” ซึ่งมีหน้าตาแบบนี้:
ในแนวทางนี้ รางบนคือ happy path และรางล่างคือเส้นทาง failure คุณเริ่มต้นที่รางฝั่งสำเร็จ ถ้าโชคดีคุณก็จะอยู่บนราง นั้นไปจนจบ แต่ถ้าเกิด error คุณจะถูกสับรางไปยังรางฝั่ง failure และข้าม step ที่เหลือใน pipeline ไปเลย
ดูดีนะ แต่มีปัญหาใหญ่อยู่อย่างหนึ่ง คือเราไม่สามารถ compose function ที่สร้าง result แบบนี้เข้าด้วยกันได้ เพราะ type ของ output แบบ two-track ไม่เหมือนกับ type ของ input แบบ one-track:
แล้วเราจะแก้ปัญหานี้ได้อย่างไร เราจะเชื่อม output แบบ two-track เข้ากับ input แบบ one-track ได้อย่างไร ลองสังเกตดูว่า ถ้า function ตัวที่สองมี input แบบ two -track ล่ะก็ การเชื่อมมันจะไม่มีปัญหาเลย:
ดังนั้นเราต้องแปลงฟังก์ชัน “switch” ที่มี input หนึ่งตัวและ output สองตัว ให้กลายเป็นฟังก์ชันแบบ two-track เพื่อทำแบบนี้ เรามาสร้าง “adapter block” พิเศษที่มีช่องสำหรับใส่ switch function แล้วแปลงมันให้เป็นฟังก์ชันแบบ two-track กัน:
ถ้าเราแปลงทุก step ของเราให้เป็น function แบบ two-track เราก็จะสามารถ compose มันเข้าด้วยกันได้อย่างลงตัว หลังจากที่มันถูกแปลงแล้ว:
ผลลัพธ์สุดท้ายคือ pipeline แบบ two-track ที่มีราง “success” และราง “failure” ตามที่เราต้องการเป๊ะ
Implementing the Adapter Blocks (การ Implement Adapter Blocks)
เราได้พูดถึงแนวคิดของ “function adapters” earlier ไปแล้ว ตัว adapter ที่แปลง switch functions ให้เป็นฟังก์ชันแบบ two-track นั้นเป็นตัวที่สำคัญมากใน toolkit ของ functional programming—มันมักถูกเรียกว่า bind หรือ flatMap ในศัพท์ของ FP มัน implement ได้ง่ายอย่างน่าประหลาดใจ นี่คือ logic ของมัน:
-
Input คือ function “switch” Output คือ function แบบ two-track-only ตัวใหม่ ซึ่งแทนด้วย lambda ที่มี input แบบ two-track และ output แบบ two-track
-
ถ้า input แบบ two-track เป็นความสำเร็จ ก็ส่ง input นั้นไปให้ switch function ทำงาน output ของ switch function ก็เป็นค่าแบบ two-track อยู่แล้ว เราจึงไม่ต้องทำอะไรเพิ่มเติมกับมัน
-
ถ้า input แบบ two-track เป็นความล้มเหลว ก็ข้าม switch function ไปและคืนค่า failure ออกไปเลย
การ implement เป็น code จะมีหน้าตาแบบนี้:
| | let bind switchFn = |
| | fun twoTrackInput -> |
| | match twoTrackInput with |
| | | Ok success -> switchFn success |
| | | Error failure -> Error failure |
อีกวิธีที่เทียบเท่ากันแต่พบได้บ่อยกว่าคือ ให้ bind มี input parameter สอง ตัว—function “switch” กับค่าแบบ two-track (a Result )—และตัด lambda ออกไป แบบนี้:
| | let bind switchFn twoTrackInput = |
| | match twoTrackInput with |
| | | Ok success -> switchFn success |
| | | Error failure -> Error failure |
การ implement ทั้งสองแบบของ bind เทียบเท่ากัน คือแบบที่สอง เมื่อถูก curried แล้ว ก็จะเหมือนกับแบบแรกเป๊ะ (ดู Currying )
Adapter block ที่มีประโยชน์อีกตัวคือตัวที่แปลง function แบบ single-track ให้เป็น function แบบ two-track
มันมักถูกเรียกว่า map ในศัพท์ของ FP นี่คือ logic ของมัน:
-
Input คือ function แบบ one-track และค่าแบบ two-track (a Result )
-
ถ้า input Result เป็นความสำเร็จ ก็ส่ง input นั้นไปให้ function แบบ one-track ทำงาน แล้วห่อ output ด้วย Ok เพื่อทำให้มันกลายเป็น Result อีกครั้ง (เพราะ output ต้องเป็นแบบ two-track)
-
ถ้า input Result เป็นความล้มเหลว ก็ข้าม function นั้นไปเหมือนเดิม
การ implement เป็น code จะมีหน้าตาแบบนี้:
| | let map f aResult = |
| | match aResult with |
| | | Ok success -> Ok (f success) |
| | | Error failure -> Error failure |
เมื่อมี bind , map และ function อื่นๆ ที่คล้ายกันอีกไม่กี่ตัว เราก็จะมี toolkit ที่ทรงพลัง ซึ่งใช้ compose function ที่ shape ไม่ตรงกันหลากหลายแบบได้
Organizing the Result Functions (การจัดระเบียบฟังก์ชันของ Result)
เราควรวาง function ใหม่พวกนี้ไว้ตรงไหนในโครงสร้าง code ของเรา วิธีมาตรฐานคือวางมันไว้ใน module ที่มีชื่อเดียวกับ type ในกรณีนี้คือ Result module ก็จะมีหน้าตาแบบนี้:
| | /// Define the Result type |
| | type Result< ' Success, ' Failure> = |
| | | Ok of ' Success |
| | | Error of ' Failure |
| | |
| | /// Functions that work with Result |
| | module Result = |
| | |
| | let bind f aResult = ... |
| | |
| | let map f aResult = ... |
เนื่องจาก Result และ function ที่เกี่ยวข้องถูกใช้ทั่วไปหมดใน domain เราจึงมักจะสร้าง utility module ใหม่ขึ้นมา (เช่น Result.fs ) แล้ววางไว้ก่อน domain types ในโครงสร้างโปรเจกต์ของเรา
Composition and Type Checking (การ Compose และการตรวจสอบ Type)
เราเน้นไปที่การทำให้ “shape” ของ function ตรงกัน ด้วยการแปลง function “switch” ให้เป็น function แบบ “two-track” แต่แน่นอนว่า การตรวจสอบ type ก็ยังเกิดขึ้นอยู่ ดังนั้นเราต้องแน่ใจด้วยว่า type ตรงกันเพื่อให้ composition ทำงานได้
บนฝั่ง success track type สามารถเปลี่ยนไปได้ตลอดราง ตราบใดที่ type ของ output ของ step หนึ่งตรงกับ type ของ input ของ step ถัดไป ตัวอย่างเช่น function สามตัวด้านล่างนี้สามารถ compose เป็น pipeline ได้ด้วย bind เพราะ output ( Bananas ) ของ FunctionA ตรงกับ input ของ FunctionB และ output ( Cherries ) ของ FunctionB ก็ตรงกับ input ของ FunctionC .
| | type FunctionA = Apple -> Result<Bananas,...> |
| | type FunctionB = Bananas -> Result<Cherries,...> |
| | type FunctionC = Cherries -> Result<Lemon,...> |
function bind จะถูกใช้แบบนี้:
| | let functionA : FunctionA = ... |
| | let functionB : FunctionB = ... |
| | let functionC : FunctionC = ... |
| | |
| | let functionABC input = |
| | input |
| | |> functionA |
| | |> Result.bind functionB |
| | |> Result.bind functionC |
ในทางกลับกัน FunctionA และ FunctionC ไม่สามารถ compose กันได้โดยตรง แม้จะใช้ bind ก็ตาม เพราะ type ไม่ตรงกัน:
Converting to a Common Error Type (การแปลงเป็น Error Type ร่วมกัน)
ต่างจาก success track ที่ type เปลี่ยนได้ในแต่ละ step error track จะมี type แบบเดียวกันสม่ำเสมอตลอดทั้งราง นั่นคือ ทุก function ใน pipeline ต้องมี error type เดียวกัน
ในหลายกรณี นั่นหมายความว่าเราต้องปรับ error type ให้เข้ากันได้ ในการทำแบบนั้น เรามาสร้าง function ที่คล้ายกับ map แต่ทำงานกับค่าใน failure track กันเถอะ function นั้นเรียกว่า mapError และจะ implement แบบนี้:
| | let mapError f aResult = |
| | match aResult with |
| | | Ok success -> Ok success |
| | | Error failure -> Error (f failure) |
ตัวอย่างเช่น สมมติว่าเรามี AppleError และ BananaError และเรามี function สองตัวที่ใช้ error type เหล่านี้
| | type FunctionA = Apple -> Result<Bananas,AppleError> |
| | type FunctionB = Bananas -> Result<Cherries,BananaError> |
การที่ error type ไม่ตรงกันทำให้ FunctionA และ FunctionB compose กันไม่ได้ สิ่งที่เราต้องทำคือสร้าง type ใหม่ที่ทั้ง AppleError และ BananaError แปลงมาเป็นได้—ทางเลือกระหว่างสองอย่างนี้ เรามาเรียกมันว่า FruitError กัน:
| | type FruitError = |
| | | AppleErrorCase of AppleError |
| | | BananaErrorCase of BananaError |
จากนั้นเราสามารถแปลง functionA ให้มี result type เป็น FruitError ได้แบบนี้:
| | let functionA : FunctionA = ... |
| | |
| | let functionAWithFruitError input = |
| | input |
| | |> functionA |
| | |> Result.mapError ( fun appleError -> AppleErrorCase appleError) |
ลดรูปให้สั้นลงได้แบบนี้:
| | let functionAWithFruitError input = |
| | input |
| | |> functionA |
| | |> Result.mapError AppleErrorCase |
นี่คือแผนภาพของการแปลงนี้:
ถ้าเราดู signature ของ functionA และ functionAWithFruitError เราจะเห็นว่าตอนนี้มัน type ในกรณี error ต่างกันแล้ว ตามที่เราต้องการ:
| | // type of functionA |
| | Apple -> Result<Bananas,AppleError> |
| | |
| | // type of functionAWithFruitError |
| | Apple -> Result<Bananas,FruitError> |
ในทำนองเดียวกัน เราก็สามารถแปลงกรณี error ของ functionB จาก BananaError ให้เป็น FruitError ได้เช่นกัน เมื่อนำทั้งหมดมารวมกัน code ก็จะมีหน้าตาประมาณนี้:
| | let functionA : FunctionA = ... |
| | let functionB : FunctionB = ... |
| | |
| | // convert functionA to use "FruitError" |
| | let functionAWithFruitError input = |
| | input |> functionA |> Result.mapError AppleErrorCase |
| | |
| | // convert functionB to use "FruitError" |
| | let functionBWithFruitError input = |
| | input |> functionB |> Result.mapError BananaErrorCase |
| | |
| | // and now we can compose the new versions with "bind" |
| | let functionAB input = |
| | input |
| | |> functionAWithFruitError |
| | |> Result.bind functionBWithFruitError |
Signature ของ functionAB ที่รวมกันแล้วคือแบบนี้:
| | val functionAB : Apple -> Result<Cherries,FruitError> |