简体   繁体   English

在PureScript中对两种类型使用#

[英]Using # with Either types in PureScript

I like to use # to pass a value through several functions since it makes for more readable code: 我喜欢使用#通过多个函数传递值,因为它使代码更易读:

1 # (\n -> n * 2) # (\n -> n + 1)

However, I would like do this with an Either : 但是,我想用Either做到这一点:

(Right 1) ??? (\n -> n * 2) ??? (\n -> n + 1)

A Right value should be unpacked for each function, whereas a Left value should simply get passed through unchanged. 应该为每个函数解包一个Right值,而Left值应该简单地传递不变。 In other words: 换一种说法:

(Right x) ??? f == Right f(x)
(Left x)  ??? f == Left x

Is there already an operator for this? 已经有操作员了吗?

You are looking for <#> which is an operator alias for Functor's mapFlipped . 您正在寻找<#> ,它是Functor的mapFlipped的运算符别名。 The Functor instance for Either applies a function to the content of Right , but leaves Left values untouched. Either的Functor实例将一个函数应用于Right的内容,但Left值保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM