- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我刚刚开始学习函数式编程,并且正在尝试将所学知识付诸实践。我有下面的代码,我只是不知道在哪里可以应用函数组合,在这个函数中部分应用。
有什么想法如何使用函数技术重构它吗?
function compareCodes(validateFn, moreProcessingFn, doStuffOnCodeAFn, doStuffOnCodeBFn, doSomething1Fn, doSomething2Fn, codeA, codeB, param1, param2) {
let result = null;
if (validateFn(codeA, codeB)) {
const isCodeAValid = doStuffOnCodeAFn(codeA); // returns a boolean
const isCodeBValid = doStuffOnCodeBFn(codeB); // returns a boolean
const isItAMatch = moreProcessingFn(isCodeAValid, isCodeBValid, codeA, codeB); // returns a boolean
if (isItAMatch) {
result = doSomething1Fn (param1, param2);
} else {
result = doSomething2Fn (param1, param2);
}
}
return result;
}
最佳答案
第一步是删除所有辅助变量。虽然 bool 中间变量通过其描述性名称易于理解,但至少 result
是完全不必要的。
function compareCodes(validateFn, moreProcessingFn, doStuffOnCodeAFn, doStuffOnCodeBFn, doSomething1Fn, doSomething2Fn, codeA, codeB, param1, param2) {
return validateFn(codeA, codeB)
? (moreProcessingFn(doStuffOnCodeAFn(codeA), doStuffOnCodeBFn(codeB), codeA, codeB)
? doSomething1Fn
: doSomething2Fn
)(param1, param2)
: null;
}
接下来,您可以应用一些柯里化(Currying)(您可以对每个参数执行此操作,但我认为它在可能一起使用的 block 中更有用):
function compareCodes(validateFn, moreProcessingFn, doStuffOnCodeAFn, doStuffOnCodeBFn, doSomething1Fn, doSomething2Fn) {
return function(codeA, codeB) {
return validateFn(codeA, codeB)
? moreProcessingFn(doStuffOnCodeAFn(codeA), doStuffOnCodeBFn(codeB), codeA, codeB)
? doSomething1Fn
: doSomething2Fn
: function(param1, param2) { return null; };
};
}
但仅此而已。虽然可以为条件编写自己的组合器并将多个参数并行输入到多个函数中,但在此过程中您不会获得任何好处。当然没有像组合这样的标准组合器可以帮助您。
如果您不再总是一起提供两个东西(A 和 B、1 和 2)但作为不同的参数,情况可能会有所不同。如果您修改所有函数以采用元组(此处表示为长度为 2 的数组,因为 JavaScript 缺乏对类型),我们可以做一些事情。首先我们转换自
function compareCodes(validateFn, moreProcessingFn, [doStuffOnCodeAFn, doStuffOnCodeBFn], [doSomething1Fn, doSomething2Fn], [codeA, codeB], [param1, param2]) {
return validateFn([codeA, codeB])
? (moreProcessingFn([doStuffOnCodeAFn(codeA), doStuffOnCodeBFn(codeB)], [codeA, codeB])
? doSomething1Fn
: doSomething2Fn
)([param1, param2])
: null;
}
to(我使用的是 ES6 语法,值得注意的箭头函数和解构)
const bimap = ([f, g]) => ([x, y]) => [f(x), g(y)];
const fst = ([x, _]) => x;
const snd = ([_, y]) => y;
function compareCodes(validate, moreProcessing, doStuff, doSomething, code, param) {
return validate(code)
? (moreProcessing(bimap(doStuff)(code), code)
? fst
: snd
)(doSomething)(param)
: null;
}
现在我们确实可以用组合器来解决这个问题:
const compose = f => g => x => f(g(x));
const bind = f => g => x => f(g(x), x);
const cond = pred => then => other => x => pred(x) ? then(x) : other(x);
const k = x => _ => x;
function compareCodes(validate, moreProcessing, doStuff, doSomething)
return cond(validate,
cond(bind(moreProcessing)(compose(bimap)(doStuff)),
fst(doSomething),
snd(doSomething)
),
k(k(null))
);
}
我们可以进一步对 compareCodes
进行完全无点的定义,但老实说,这不值得。
关于javascript - 可以使用 hof/compose/currying/partial application/monads 将此代码重构为更具功能性的编程风格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42660594/
export const postMoviePopular = url = dispatch => { const data = axios.get(url); dispatch(sa
我一直在学习 Haskell 中的柯里化(Currying),现在尝试以柯里化(Currying)形式编写函数的 Haskell 类型签名,其非柯里化(Currying)形式有一个 (x, y) 类型
在许多列表处理语言(以及其他语言)中,它们都有一个名为 curry 的函数,它可以完成一些巧妙的事情。我的问题是为什么他们称之为 curry ?这个名字从何而来?我唯一的猜测是来自世界各国的美味 cu
当前的 curry 函数采用接受 2 个元素的元组的函数,并允许对结果函数进行 curry 或部分应用。 let x = curry (\(x, y) -> x + y) x 1 2 -- 3 是否可
假设我有 library(functional) f = function(x, p) { x^p } f2 = Curry(f, p=2) 有没有办法找出仅给定 f2 的 p 被设置为什么? 最佳答
我试图了解柯里化(Currying)相对于 Scala 中部分应用程序的优势。请考虑以下代码: def sum(f: Int => Int) = (a: Int, b: Int) => f(a)
我有一个函数,我必须包装另一个函数(如果它存在)或替换它(如果它不存在)。这意味着参数的数量会根据情况而变化。 这就是我的意思- 列列表: const myColumns = [ {name:
我不明白为什么在无类型的 lambda 演算中允许以下 beta 减少: (λx.x y) (u v) -> ((u v) y) 具体来说,我无法理解如何传递两个参数 u和 v到单个参数 x在 λx.
Ruby 1.9's built in support of currying支持两种处理带有任意数量参数的过程的方法: my_proc = proc {|*x| x.max } 1) curry 没
根据下面的代码片段,example-func-A 和 example-func-B 之间是否存在任何有意义的区别? #lang racket/base (require (only-in racket
我是 JavaScript 的新手,试图理解 Oreilly JavaScript Cookbook 中有关柯里化(Currying)的教程。 谁能用通俗易懂的语言逐步详细解释这个程序。请务必解释在程
所以我知道你可以: > f = map (+1) > f [1,2,3] [2,3,4] 但是,如果您这样做会怎样: > g = map (+) [1,2,3] > :t g g :: Num a =
我在多篇文章和博客中看到过对柯里化(Currying)函数的引用,但我找不到一个好的解释(或者至少是一个有意义的解释!) 最佳答案 柯里化(Currying)是指将一个接受多个参数的函数分解为一系列函
所以我在我们的教科书中经过一些反复试验和研究后得到了以下功能,我可以想出一个解决方案。 def prodC1(f : Int => Int) : (Int, Int) => Int = { def
我有问题,可以简化为以下示例: let func a b c = printf "%s %s %s" a b c let partial = func "a" let something_that_r
今天开始学习 Scala,我很好奇你是否可以重载一个函数来添加柯里化(Currying),比如: def add(x: Int, y: Int): Int = x + y def add(x: Int
我正在尝试编写柯里化(Currying)函数的策略,包括通用量化函数。 Require Import Coq.Program.Tactics. Definition curry1 := forall
我想以某种方式组合函数。请在伪代码(不是 F#)中考虑这两个函数 F1 = x + y F2 = F1 * 10 // note I did not specify arguments for F1,
在 Perl 6 世界中,currying 是表示部分实例化的通用术语,它也用于 (parametrized) roles 的世界。 . 但是,尚不清楚如何实际使用它: role Zipi[::T]
我想知道是否可以在多参数组函数上使用柯里化(Currying): scala> def sum(a: Int)(b: Int): Int = { a+b } sum: (a: Int)(b: Int)
我是一名优秀的程序员,十分优秀!