javascript - 如何以功能方式有条件地分配给变量?
问题描述
我需要根据条件为变量赋值。我想用函数式编程范式来做这件事,因此我不能在外部范围内声明它然后重新分配。
// let foo = undefined // no-let!
if(condition) {
const foo = 1
} else {
const foo = 0
}
do_some_stuff(foo) // Uncaught ReferenceError: foo is not defined
我知道,我可以在这里使用三元表达式,
const foo = condition ? 1 : 0
但是如果我在我的条件下还有其他例程要做,比如:
if(condition) {
const foo = 1
do_stuff()
} else {
const foo = 0
do_other_stuff()
}
do_third_stuff(foo)
解决方案
可能您会使用一些代数数据类型(ADT) 对您的案例进行编码,例如Either
. 也就是说,您可以涵盖两个子案例:left和right。
从后面看代码// --> Solution starts here
。以前的代码是一个迷你标准 FP 库,使用 vanilla JavaScript 使代码可运行。检查并享受!
// Mini standard library
// -------------------------------
// The identity combinator
// I :: a -> a
const I = x => x
// Either ADT
const Either = (() => {
// Creates an instance of Either.Right
//
// of :: b -> Either a b
const of = x => ({ right: x })
// Creates an instance of Either.Right
//
// Right :: b -> Either a b
const Right = of
// Creates an instance of Either.Left
//
// Left :: a -> Either a b
const Left = x => ({ left: x })
// Maps Either.Left or Either.Right in a single operation
//
// bimap :: (a -> c) -> (b -> d) -> Either a b -> Either c -> d
const bimap = f => g => ({ left, right }) => left ? Left (f (left)) : Right (g (right))
// Lifts a value to Either based on a condition, where false
// results in Left, and true is Right.
//
// tagBy :: (a -> Boolean) -> a -> Either a a
const tagBy = f => x => f (x) ? Right (x) : Left (x)
// Unwraps Either.Left or Either.Right with mapping functions
//
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = f => g => ({ left, right }) => left ? f (left) : g (right)
// Unwraps Either.Left or Either.Right and outputs the raw value on them
//
// unwrap :: Either a b -> c
const unwrap = either (I) (I)
return { of, Right, Left, bimap, tagBy, either, unwrap }
}) ()
// --> Solution starts here
// Lifts to Either.Right if x is greater than 3,
// otherwise, x is encoded as Left.
//
// tagGt3 :: Number -> Either Number Number
const tagGt3 = Either.tagBy (x => x > 3)
// doStuff :: Number -> Number
const doStuff = x => x + 1
// doStuff2 :: Number -> Number
const doStuff2 = x => x * 4
// doStuff3 :: Either Number Number -> Either Number Number
const doStuff3 = Either.bimap (doStuff) (doStuff2) // <-- here's the decision!
const eitherValue1 = doStuff3 (tagGt3 (2))
const eitherValue2 = doStuff3 (tagGt3 (30))
const output1 = Either.unwrap (eitherValue1)
const output2 = Either.unwrap (eitherValue2)
console.log ('output1: ', output1)
console.log ('output2: ', output2)
使用管道重构
现在我介绍pipe
粘合一个或多个一元函数的组合,这使代码更加优雅:
// Mini standard library
// -------------------------------
// The identity combinator
// I :: a -> a
const I = x => x
// Pipes many unary functions
//
// pipe :: [a -> b] -> a -> c
const pipe = xs => x => xs.reduce ((o, f) => f (o), x)
// Either ADT
const Either = (() => {
// Creates an instance of Either.Right
//
// of :: b -> Either a b
const of = x => ({ right: x })
// Creates an instance of Either.Right
//
// Right :: b -> Either a b
const Right = of
// Creates an instance of Either.Left
//
// Left :: a -> Either a b
const Left = x => ({ left: x })
// Maps Either.Left or Either.Right in a single operation
//
// bimap :: (a -> c) -> (b -> d) -> Either a b -> Either c -> d
const bimap = f => g => ({ left, right }) => left ? Left (f (left)) : Right (g (right))
// Lifts a value to Either based on a condition, where false
// results in Left, and true is Right.
//
// tagBy :: (a -> Boolean) -> a -> Either a a
const tagBy = f => x => f (x) ? Right (x) : Left (x)
// Unwraps Either.Left or Either.Right with mapping functions
//
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = f => g => ({ left, right }) => left ? f (left) : g (right)
// Unwraps Either.Left or Either.Right and outputs the raw value on them
//
// unwrap :: Either a b -> c
const unwrap = either (I) (I)
return { of, Right, Left, bimap, tagBy, either, unwrap }
}) ()
// --> Solution starts here
// doStuff :: Number -> Number
const doStuff = x => x + 1
// doStuff2 :: Number -> Number
const doStuff2 = x => x * 4
const { tagBy, bimap, unwrap } = Either
// doStuff3 :: Number -> Number
const doStuff3 = pipe ([
tagBy (x => x > 3),
bimap (doStuff) (doStuff2), // <-- here's the decision!
unwrap
])
const output1 = doStuff3 (2)
const output2 = doStuff3 (30)
console.log ('output1: ', output1)
console.log ('output2: ', output2)
推荐阅读
- html - 仅使用 CSS 显示带有悬停的相邻 div
- python - 动态数组追加/导出 - Python
- python-3.x - 轴上不同位置的刻度和标签
- excel - 管理员管理的 Excel 加载项在 Office365 Excel 中不可见
- reactjs - 为现有 HOC 编写声明 d.ts 文件
- java - 获取 HBase 的所有区域,避免 Zookeeper 客户端重新连接
- javascript - 异步等待和可共享的 MongoDB 连接
- c++ - C++ 使用 ChangeParametersInfo 更改桌面不起作用 GetLastError 返回 0
- configuration - 带有简单注入器的旧 .NET 应用程序中的选项模式、配置
- python - 获取每组的天数差异