Effect-TS 中的过滤选项:实用指南

风磊小哥_7584

风磊小哥_7584

2024-09-12

1274人浏览

转载

effect-ts 中的过滤选项:实用指南

effect-ts 提供了各种方法来过滤选项内的值,允许您对可选值应用转换、谓词或检查。这些函数有助于确保仅保留相关数据,同时丢弃 none 值或不满足指定条件的值。在本文中,我们将探讨用于过滤选项的四个关键函数:o.partitionmap、o.filtermap、o.filter 和 o.exists。

示例 1:使用 o.partitionmap 对选项进行分区

概念

o.partitionmap 函数允许您基于返回 either 的映射函数将 option 划分为两个 options 的元组。 either.left 值划分到第一个选项中,而 either.right 值划分到第二个选项中。如果原来的option是none,那么两个分区都是none。

代码

function filtering_ex01() {
  const some = o.some(1); // create an option containing the value 1
  const none = o.none(); // create an option representing no value
  const toeither = (n: number) => (n % 2 === 0 ? e.left(n) : e.right(n));

  console.log(pipe(some, o.partitionmap(toeither))); // output: [none, some(1)] (1 is odd, so it goes to the right)
  console.log(pipe(none, o.partitionmap(toeither))); // output: [none, none] (since the option is none)
}

解释

  • pipe(some, o.partitionmap(toeither)):由于 1 是奇数,所以 toeither 函数返回 e.right(1),将 1 放在第二个 option 中,结果是 [none, some(1) ].
  • pipe(none, o.partitionmap(toeither)):由于原来的option是none,所以两个分区都是none,导致[none, none]。

当您需要应用对值进行分类的映射,同时将它们分为两组(满足条件的组和不满足条件的组)时,此函数非常有用。

示例 2:使用 o.filtermap 进行映射和过滤

概念

o.filtermap 函数将转换函数应用于选项内的值。如果函数返回 some,则保留该值;如果返回 none,则该值将被过滤掉。如果原始 option 为 none,则结果仍为 none。

代码

function filtering_ex02() {
  const some = o.some(1); // create an option containing the value 1
  const none = o.none(); // create an option representing no value
  const toeven = (n: number) => (n % 2 === 0 ? o.some(n) : o.none());

  console.log(pipe(some, o.filtermap(toeven))); // output: none (since 1 is not even)
  console.log(pipe(o.some(2), o.filtermap(toeven))); // output: some(2) (since 2 is even)
  console.log(pipe(none, o.filtermap(toeven))); // output: none (since the original option is none)
}

解释

  • pipe(some, o.filtermap(toeven)):由于1不是偶数,所以toeven函数返回none,结果为none。
  • pipe(o.some(2), o.filtermap(toeven)):值 2 是偶数,因此 toeven 函数返回 some(2),结果为 some(2)。
  • pipe(none, o.filtermap(toeven)):由于原来的option是none,所以结果还是none。

当您想要根据特定条件转换和过滤选项内的值时,此功能非常有用。

示例 3:使用 o.filter 通过谓词过滤选项

概念

o.filter 函数检查 option 内的值是否满足给定的谓词。如果谓词满足,则返回原始option;否则,返回 none。如果原来的option是none,那么它仍然是none。

代码

function filtering_ex03() {
  const some = o.some(1); // create an option containing the value 1
  const none = o.none(); // create an option representing no value
  const iseven = (n: number) => n % 2 === 0;

  console.log(pipe(some, o.filter(iseven))); // output: none (since 1 is not even)
  console.log(pipe(o.some(2), o.filter(iseven))); // output: some(2) (since 2 is even)
  console.log(pipe(none, o.filter(iseven))); // output: none (since the original option is none)
}

示例 4:使用 o.exists 检查谓词

概念

o.exists 函数检查 option 内的值是否满足谓词,如果满足则返回 true,如果不满足则返回 false。如果 option 为 none,则返回 false。

代码

function filtering_ex04() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value
  const isEven = (n: number) => n % 2 === 0;

  console.log(pipe(some, O.exists(isEven))); // Output: false (since 1 is not even)
  console.log(pipe(O.some(2), O.exists(isEven))); // Output: true (since 2 is even)
  console.log(pipe(none, O.exists(isEven))); // Output: false (since the original Option is None)
}

解释

  • pipe(some, o.exists(iseven)):由于 1 不是偶数,因此不满足谓词,因此结果为 false。
  • pipe(o.some(2), o.exists(iseven)):值 2 满足谓词,因此结果为 true。
  • pipe(none, o.exists(iseven)):由于 option 为 none,所以结果为 false。

当您需要快速检查以确定 option 内的值是否满足条件而不转换或过滤 option 本身时,此函数非常有用。

结论

effect-ts 中的过滤选项允许根据条件或转换灵活处理可选值。无论您是使用 o.partitionmap 对值进行分区、使用 o.filtermap 应用转换、使用 o.filter 检查谓词,还是只是使用 o.exists 验证条件,这些工具都提供了强大的方法来控制选项的处理方式。通过使用这些函数,您可以有效地管理可选数据,确保仅保留或执行相关值。

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Uvicorn 报错与问题解决教程
Uvicorn 报错与问题解决教程

本专题汇总了Uvicorn服务器常见问题与报错解决的教程合集,涵盖启动失败、端口占用、模块导入错误、事件循环冲突及热重载失效等典型故障的排查思路。同时整理了多进程与Worker配置、SSL/HTTPS证书报错、跨域与代理配置等实战解决方案,助你快速定位并解决Uvicorn运行中的各类问题。

2026.11.02

0

14

Uvicorn使用教程
Uvicorn使用教程

本专题汇总了Uvicorn使用教程与启动运行指南,涵盖其作为Python ASGI服务器的定位与特点,以及命令行启动(uvicorn main:app --reload)、参数配置(host/port/workers)、程序内嵌运行与Gunicorn结合部署等核心用法。同时整理了热重载调试、日志配置与生产环境部署优化等技巧,助你快速掌握Uvicorn的运行与配置。

2026.11.02

0

17

Uvicorn安装配置教程
Uvicorn安装配置教程

本专题汇总了Uvicorn的安装配置教程与部署指南,涵盖通过pip安装Uvicorn、启动FastAPI/Starlette应用、开发模式热重载(--reload)及多进程工作模式配置等核心操作。同时整理了结合Gunicorn的生产部署方案、HTTPS与反向代理配置及性能调优技巧,助你快速完成Uvicorn服务的安装与上线部署。

2026.11.02

0

17

Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

100

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

80

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

240

10

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

140

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

140

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.1万人学习