Effect-TS 提供了處理 Option 和 Either 類型的強大工具。在本文中,我們將探索使用函式庫的實用函數轉換和操作這些類型的各種方法。
O.getRight 函數將 Either 轉換為 Option,並丟棄錯誤。如果 Either 正確,則傳回 O.some(value),否則傳回 O.none。
import { Option as O, Either as E, pipe } from 'effect'; function conversions_ex01() { const eitherRight = E.right('ok'); // Create an Either containing the value 'ok' const eitherLeft = E.left('error'); // Create an Either representing an error console.log(O.getRight(eitherRight)); // Output: Some('ok') console.log(O.getRight(eitherLeft)); // Output: None }
O.getLeft 函數將 Either 轉換為 Option,並丟棄該值。如果Either為Left,則回傳O.some(error),否則回傳O.none。
import { Option as O, Either as E, pipe } from 'effect'; function conversions_ex02() { const eitherRight = E.right('ok'); // Create an Either containing the value 'ok' const eitherLeft = E.left('error'); // Create an Either representing an error console.log(O.getLeft(eitherRight)); // Output: None console.log(O.getLeft(eitherLeft)); // Output: Some('error') }
O.getOrElse 函數如果是 Some,則傳回 Option 內的值,否則傳回提供的預設值。
import { Option as O, pipe } from 'effect'; function conversions_ex03() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log( pipe( some, O.getOrElse(() => 'default') ) ); // Output: 1 (since some contains 1) console.log( pipe( none, O.getOrElse(() => 'default') ) ); // Output: 'default' (since none is None) }
O.orElse 函數傳回提供的 Option,如果 self 為 None,否則傳回 self。此函數允許選項鏈接,其中後備是另一個選項。
import { Option as O, pipe } from 'effect'; function conversions_ex04() { const some1 = O.some(1); // Create an Option containing the value 1 const some2 = O.some(2); // Create an Option containing the value 2 const none = O.none(); // Create an Option representing no value console.log( pipe( some1, O.orElse(() => some2) ) ); // Output: Some(1) (since some1 contains 1) console.log( pipe( none, O.orElse(() => some2) ) ); // Output: Some(2) (since none is None and fallback is some2) }
如果 self 為 None,O.orElseSome 函數將傳回包含在 Some 中的提供的預設值,否則傳回 self。此函數允許連結選項,其中回退是包含在 Some 中的預設值。
import { Option as O, pipe } from 'effect'; function conversions_ex05() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log( pipe( some, O.orElseSome(() => 2) ) ); // Output: Some(1) (since some contains 1) console.log( pipe( none, O.orElseSome(() => 2) ) ); // Output: Some(2) (since none is None and fallback is 2) }
O.orElseEither 函數傳回一個包含 Either 的選項,其中 Left 來自後備選項,Right 來自原始選項。此函數允許連結選項,其中後備提供“任一”以獲取更多上下文。
import { Option as O, Either as E, pipe } from 'effect'; function conversions_ex06() { const some1 = O.some(1); // Create an Option containing the value 1 const some2 = O.some(2); // Create an Option containing the value 2 const none = O.none(); // Create an Option representing no value console.log( pipe( some1, O.orElseEither(() => some2) ) ); // Output: Some(Right(1)) (since some1 contains 1) console.log( pipe( none, O.orElseEither(() => some2) ) ); // Output: Some(Left(2)) (since none is None and fallback is some2) }
O.firstSomeOf 函數傳回在可迭代的 Options 中找到的第一個 Some。如果全部為None,則傳回None。
import { Option as O } from 'effect'; function conversions_ex07() { const options = [O.none(), O.some(1), O.some(2)]; // Create an iterable of Options const optionsAllNone = [O.none(), O.none()]; // Create an iterable of None Options console.log(O.firstSomeOf(options)); // Output: Some(1) (since the first non-None Option is Some(1)) console.log(O.firstSomeOf(optionsAllNone)); // Output: None (since all Options are None) }
O.toRefinement 函數將傳回 Option 的函數轉換為類型保護,從而允許更具體的類型檢查。
import { Option as O } from 'effect'; function conversions_ex08() { const isPositive = (n: number): O.Option<number> => n > 0 ? O.some(n) : O.none(); const isPositiveRefinement = O.toRefinement(isPositive); console.log(isPositiveRefinement(1)); // Output: true (since 1 is positive) console.log(isPositiveRefinement(-1)); // Output: false (since -1 is not positive) }
O.toArray 函數將 Option 轉換為陣列。如果Option為Some,則傳回包含該值的陣列;如果為 None,則傳回空數組。
import { Option as O } from 'effect'; function conversions_ex09() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(O.toArray(some)); // Output: [1] (since some contains 1) console.log(O.toArray(none)); // Output: [] (since none is None) }
在本文中,我們探索了 Effect-TS 提供的用於轉換和操作 Option 和 Either 類型的各種函數。這些函數增強了程式碼的靈活性和表現力,使您能夠更優雅地處理可選值和容易出錯的值。無論您需要將 Either 轉換為 Option、連結多個 Option 值或執行型別安全操作,Effect-TS 都提供了一組強大的工具來簡化這些任務。透過利用這些實用程序,您可以編寫更清晰、更易於維護的程式碼,以有效處理值的存在或不存在。
以上是探索 Effect-TS 中的選項轉換的詳細內容。更多資訊請關注PHP中文網其他相關文章!