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

news/2024/9/20 19:45:41
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 验证条件,这些工具都提供了强大的方法来控制选项的处理方式。通过使用这些函数,您可以有效地管理可选数据,确保仅保留或执行相关值。 以上就是Effect-TS 中的过滤选项:实用指南的详细内容,更多请关注我的其它相关文章!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/62670.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

C++ 数据算数类型

▲ 《C++ Primer》 P30▲ 《C++ Primer》 P38

blender 模拟三键鼠标 alt+鼠标左键 代替 中键 旋转视图,shift+alt+左键 平移视图

blender 模拟三键鼠标 alt+鼠标左键 代替 中键 旋转视图,shift+alt+左键 平移视图--------------------------------------------- 生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯! https://pengchenggang.gitee.io/navigator/ SMART原则:目标必须是…

用户验收测试指南5过渡阶段的UAT

5 UAT的位置 在本书的这一中心章节中,我们将从准备工作的细节中抽身出来,在沉浸于我们的分步方法的细节之前,先从大局出发。UAT 在更大的计划中处于什么位置?它的核心功能和属性是什么?它的总体贡献是什么? 本章涉及的主题作为一系列过渡的 IS 生命周期 过渡规划 作为过渡…

API接口12种安全措施

1. 使用HTTPS:确保数据传输过程中的安全性。2. 使用OAuth2:一种授权框架,用于授权第三方应用访问服务器上的用户数据。3. 使用WebAuthn:一种网络认证标准,用于安全地进行用户认证。4. API进行签名加密:对API请求进行签名以确保请求的完整性和真实性。5. 黑白名单:限制访…

opencascade Bnd_Range源码学习区间计算

opencascade Bnd_Range 前言这个类描述了由两个实数值限定的 1D 空间中的区间。 一个区间可以是无效的,这表示区间中不包含任何点。 方法 1 默认构造函数。创建一个无效区间。 Bnd_Range() ; 2 构造函数。创建最小最大值区间 Bnd_Range(const Standard_Real theMin, const St…

P2414 [NOI2011] 阿狸的打字机

题目思路 将每一个输出的串放入一个 Trie 树中。 考虑离线处理询问 \((x, y)\),对于每一个 \(y\) 集中处理所有的 \(x\),\(y\) 在 Trie 树上走,走过的点标记一下,结果就是 \(x\) 字符串结尾节点在 fail 树上的对应节点的子树的标记数量。 记得在节点离开的时候撤销标记。 代…

0920

线代 舒尔公式,化上三角,下三角,对角阵 范德蒙德行列式 X型行列式,{主对角中下标之和为(2k+1)的两项乘积-副对角中下标之和为(2k+1)的两项乘积【需与前面两项下标号相同】}的连乘 宽对角,a2=4bc,a2≠4bc计组 MAR位数说明存储单元位数 MDR位数说明字长 编译器:将高级语…

米尔STM32MP2核心板首发新品上市!高性能+多接口+边缘算力

米尔发布基于STM32MP257设计的嵌入式处理器模块MYC-LD25X核心板及开发板。核心板基于STM32MP2系列是意法半导体推出最新一代工业级64位微处理器,采用LGA 252 PIN设计,存储配置1GB/2GB LPDDR4、8GB eMMC,具有丰富的通讯接口,适用于高端工业HMI、边缘计算网关、新能源充电桩、…