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 函数返回在可迭代选项中找到的第一个 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=> 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 都提供了一组强大的工具来简化这些任务。通过利用这些实用程序,您可以编写更清晰、更易于维护的代码,以有效处理值的存在或不存在。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3