我可以将一个带隐式参数的方法转换为函数吗?
trait Tx
def foo(bar: Any)(implicit tx: Tx) {}
foo _ // error: could not find implicit value for parameter tx: Tx
Run Code Online (Sandbox Code Playgroud)
我试图实现以下,最好是我可以以某种方式使它与普通调用一起工作withSelection(deleteObjects):
trait Test {
def atomic[A](fun: Tx => A): A
def selection: Iterable[Any]
def withSelection(fun: Iterable[Any] => Tx => Unit) {
val sel = selection
if (sel.nonEmpty) atomic { implicit tx =>
fun(sel)(tx)
}
}
object deleteAction {
def apply() {
withSelection(deleteObjects) // !
}
}
def deleteObjects(xs: Iterable[Any])(implicit tx: Tx): Unit
}
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,但就我所见,它并没有解决从方法到功能的问题.
Haskell 定义说:
表达式是弱头正常形式(WHNF),如果它是:
- 一个构造函数(最终应用于参数),如True,Just(square 42)或(:) 1
- 一个内置函数应用于太少的参数(可能没有),如(+)2或sqrt.
- 或lambda抽象\ x - >表达式.
为什么内置功能会得到特殊处理?根据lambda演算,部分应用函数和任何其他函数之间没有区别,因为最后我们只有一个参数函数.
haskell lambda-calculus partial-application reduction weak-head-normal-form
我用curry这种方式实现了一个函数:
function curry (fn) {
var slice = Array.prototype.slice,
args = slice.apply(arguments, [1]);
return function () {
fn.apply(null, args.concat(slice.apply(arguments)));
};
}
Run Code Online (Sandbox Code Playgroud)
当我使用上述功能执行以下操作时
function add (x, y) {
return x + y;
}
var inc = curry(add, 1);
console.log(inc(10));
Run Code Online (Sandbox Code Playgroud)
它记录undefined.11不是预期的产出吗?我的代码出了什么问题?
注意:console.log(x, y)在add功能日志中使用1 10.我不明白为什么会回来undefined.
我昨天开始使用haskell,但仍然完全迷失在这个勇敢的新世界的岸边.现在我遇到了以下问题:
让我们假设我有一些函数对整数和另一个变量做了一些魔术:
makeTuple :: Int -> a -> (Int, a)
makeTuple n x = (n, x)
Run Code Online (Sandbox Code Playgroud)
现在我想将此函数应用于列表的所有元素.到目前为止没问题,因为映射是你在python(我来自哪里)的日常面包和黄油.
makeTupleList :: Int -> [a] -> [ (Int, a) ]
makeTupleList n x = map (makeTuple n) x
Run Code Online (Sandbox Code Playgroud)
据我所知,二元函数makeTuple部分应用整数n,因此成为一元函数,可以映射到x的每个元素.到目前为止,一切都很好.
但是当makeTuple函数有另一个签名时我该怎么办,例如:
makeTuple2 :: a -> Int -> (Int, a)
makeTuple2 x n = (n, x)
Run Code Online (Sandbox Code Playgroud)
许多方式导致罗马:效果是一样的,但方式是另一种.现在显然映射不再起作用了:函数需要一个Int并获得一个.
makeTupleList2 :: Int -> [a] -> [ (Int, a) ]
makeTupleList2 n x = map (makeTuple2 n) x -- boolshit
Run Code Online (Sandbox Code Playgroud)
这是可以预料的.我的-maybe太pythonic- workaround正在使用另一个函数来传递它们应该去的参数:
makeTupleList2 :: Int -> [a] -> …Run Code Online (Sandbox Code Playgroud) 我是ES5 Function.prototype.bind和粉碎参数的忠实粉丝(基本上为函数创建默认参数).
我有点愚弄,但我不能为我的生活找出自己的构造了.这是我的游乐场:
function hello( arg1, arg2 ) {
console.log('hello()');
console.log('"this" is: ', this);
console.log('arguments: ', arguments);
}
var foo = Function.prototype.call.bind( hello,{what: 'dafuq'}, 2 );
foo( 42 );
Run Code Online (Sandbox Code Playgroud)
此日志输出如下:
hello()
"this" is: Object{ what="dafuq" }
arguments: [2,42]
Run Code Online (Sandbox Code Playgroud)
但是我不明白这个{what: 'dafuq'}物体究竟是如何作为this内部参考的foo.据我所知,我们正在创造一个绑定调用到Function.prototype.call.让我们.bind()快速检查MDN概要:
fun.bind(thisArg[, arg1[, arg2[, ...]]])
Run Code Online (Sandbox Code Playgroud)
所以,thisArgfor .call是hello函数,后跟参数列表.基本上会发生这种情况
Function.prototype.call.call( hello, {what: 'dafuq'}, 2);
Run Code Online (Sandbox Code Playgroud)
...呃现在我的大脑有点疼.我想我现在有了一个想法会发生什么,但请有人找到可靠的单词来详细解释它.
{what: 'dafuq'}变成了this reference关于什么时候你需要_一个方法来使用它作为一个函数,我对规则有点不稳定.例如,为什么以下的Foo's和Nil's 之间存在差异::?
def square(n: Int) = n * n
object Foo { def ::(f: Int => Int) = f(42) }
// ...
scala> Foo.::(square)
res2: Int = 1764
scala> Nil.::(square)
<console>:6: error: missing arguments for method square in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
Nil.::(square)
^
scala> Nil.::(square _)
res3: List[(Int) => Int] = List(<function1>)
Run Code Online (Sandbox Code Playgroud) 虽然我对数学意义上的currying有一点了解,但部分应用中缀函数是一个新概念,我在深入研究" 了解你是一个很好的Haskell "一书后发现了这个概念.
鉴于此功能:
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
Run Code Online (Sandbox Code Playgroud)
作者以一种有趣的方式使用它:
ghci> applyTwice (++ [0]) [1]
[1,0,0]
ghci> applyTwice ([0] ++) [1]
[0,0,1]
Run Code Online (Sandbox Code Playgroud)
在这里我可以清楚地看到,结果函数传递了不同的参数,这不会通过正常的方式发生,因为它是一个curried函数(是吗?).那么,Haskell对中缀切片有什么特殊处理吗?它对所有中缀函数都是通用的吗?
作为旁注,这是我使用Haskell和函数式编程的第一周,我还在读这本书.
haskell functional-programming currying partial-application operator-sections
这怎么可能,那里发生了什么?
这有名字吗?
还有哪些其他语言具有相同的行为?
没有强大的打字系统?
haskell programming-languages functional-programming function partial-application
显然有点心不在焉,我写了类似下面的内容:
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
class Foo f where
type Bar f :: *
retbar :: Bar f -> IO f
type Baz f = (Foo f, Eq f)
-- WithBar :: * -> (*->Constraint) -> * -> Constraint
type WithBar b c f = (c f, Bar f ~ b)
instance Foo () where
type Bar () = ()
retbar = return
naim :: WithBar () Baz u => IO u -- why …Run Code Online (Sandbox Code Playgroud) 我无法理解函数应用程序如何与haskell中的currying一起工作.如果我有以下功能:
($) :: (a -> b) -> a -> b
Run Code Online (Sandbox Code Playgroud)
我明白要部分应用这个功能,我需要提供(a -> b)函数($第一个参数).
为什么可以先应用一个值(即反向参数)?
($ 0) :: Num a => (a -> b) -> b
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
haskell operators partial-application dollar-sign operator-sections
haskell ×6
currying ×2
javascript ×2
scala ×2
bind ×1
dollar-sign ×1
ecmascript-5 ×1
function ×1
implicit ×1
operators ×1
reduction ×1