有点类似Stack Overflow问题Compose和andThen方法,我一直在通过Twitter的Scala学校教程,很快就遇到了一个评论者所遇到的同样的问题(这很棒,因为我上床认为我的问题已经解决了).
在本教程中,它定义了两种方法:
def addUmm(x: String) = x + " umm"
def addAhem(x: String) = x + " ahem"
Run Code Online (Sandbox Code Playgroud)
同时Scala中的较新版本,你不能要求他们撰写这样:addUmm(_).compose(addAhem(_)),公认的答案(以及一些其他的答案似乎基于这样的事实,以铰链addUmm和addAhem一些方法,而不是功能,当试图创建一个问题打电话给我.我上床睡觉满意,成功跑了:
scala> ((s: String) => s + " umm").compose((s: String) => s + " ahem")
res0: String => java.lang.String = <function1>
Run Code Online (Sandbox Code Playgroud)
凉.问题在于,虽然无法编写方法是有道理的,但当我知道评估的价值相同的时候Function1:
val a = (s: String) => s + " umm"
val b = (s: String) => s + " ahem"
val c = a(_).compose(b(_))
Run Code Online (Sandbox Code Playgroud)
好吧,最后一行咳出与原始问题相同的错误,即使这次是部分应用函数,而不是方法.原始问题中的一个答案(高排名,但不是接受的答案)似乎暗示它与部分应用程序如何扩展有关,解释是什么?
对于Scala新手来说,a(_).compose(b(_)) …
我正在关注这个博客,在haskell中编写一个简单的http服务器,
使用情况>>>对我来说并不清楚.这段代码片段做了什么?
handleHttpConnection r c = runKleisli
(receiveRequest >>> handleRequest r >>> handleResponse) c >>
close c
Run Code Online (Sandbox Code Playgroud)
在这个链接上,我看到了<<<
let h = arr (++ "!")
<<< arr foo
<<< Kleisli bar
<<< arr id
Run Code Online (Sandbox Code Playgroud)
haskell arrows category-theory function-composition category-abstractions
这与这个问题有关:如何进行功能组合?
我注意到方法引用可以分配给声明为的变量Function,因此我假设它应该具有andThen或compose函数,因此我希望我们可以直接组合它们.但很显然,我们需要把它们分配给声明为可变的Function第一(或类型转换调用之前)之前,我们可以称之为andThen或compose他们.
我怀疑我可能会对这应该如何运作有一些误解.
所以我的问题:
andThen方法之前,为什么我们需要先键入或将其分配给变量?示例代码如下.
public class MyMethods{
public static Integer triple(Integer a){return 3*a;}
public static Integer quadruple(Integer a){return 4*a;}
public int operate(int num, Function<Integer, Integer> f){
return f.apply(num);
}
public static void main(String[] args){
MyMethods methods = new MyMethods();
int three = methods.operate(1, MyMethods::triple); // This is fine
// Error below
// int twelve = methods.operate(1, (MyMethods::triple).andThen(MyMethods::quadruple));
// But this one is …Run Code Online (Sandbox Code Playgroud) 我正在研究功能组合并有一个例子:
Function<String, String> test = (s) -> s.concat("foo");
String str = test.andThen(String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
该示例按预期编译并运行。但是,如果我使用 更改组合的顺序compose(),则需要显式转换:
String str = test.compose((Function <String, String>)
String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
如果没有显式转换String::toUpperCase为Function <String, String>,则会出现编译器错误:
Error:
incompatible types: cannot infer type-variable(s) V
(argument mismatch; invalid method reference
incompatible types: java.lang.Object cannot be converted to java.util.Locale)
String s = test.compose(String::toUpperCase).apply("bar");
^-------------------------------^
Run Code Online (Sandbox Code Playgroud)
问题是为什么compose()需要显式强制转换,而andThen()在这种情况下不需要?
此问题的灵感来自另一个问题的答案,表明您可以使用定义为以下内容的函数从列表中删除每个元素:
removeall = filter . (/=)
Run Code Online (Sandbox Code Playgroud)
使用铅笔和纸张来处理它的类型filter,(/=)并且(.),该功能有一种类型
removeall :: (Eq a) => a -> [a] -> [a]
Run Code Online (Sandbox Code Playgroud)
这正是你根据合同所期望的.但是,对于GHCi 6.6,我得到了
gchi> :t removeall
removeall :: Integer -> [Integer] -> [Integer]
Run Code Online (Sandbox Code Playgroud)
除非我明确指定类型(在这种情况下它工作正常).为什么Haskell推断出函数的这种特定类型?
为什么andThen只存在Scala中的单个参数函数?
以下代码有效:
val double = (x: Int) => x * 2
val timesFour = double andThen double
Run Code Online (Sandbox Code Playgroud)
但为什么没有andThen多参数函数的方法呢?
val multiply = (x: Int, y: Int) => x * y
val multiplyAndDouble = multiply andThen double
<console>:10: error: value andThen is not a member of (Int, Int) => Int
Run Code Online (Sandbox Code Playgroud)
当然,添加这种方法是微不足道的.它是否有理由从标准库中省略?
我刚刚注意到以下内容很容易解决:
val double = (x: Int) => x * 2
val timesFour = double andThen double
Run Code Online (Sandbox Code Playgroud)
但我仍然想知道为什么andThen没有andThen
我尝试在Haskell中开发一个简单的平均函数.这似乎有效:
lst = [1, 3]
x = fromIntegral (sum lst)
y = fromIntegral(length lst)
z = x / y
Run Code Online (Sandbox Code Playgroud)
但为什么以下版本不起作用?
lst = [1, 3]
x = fromIntegral.sum lst
y = fromIntegral.length lst
z = x / y
Run Code Online (Sandbox Code Playgroud) 例如,可以使用Yoneda获得循环融合:
newtype Yoneda f a =
Yoneda (forall b. (a -> b) -> f b)
liftYo :: (Functor f) => f a -> Yoneda f a
liftYo x = Yoneda $ \f -> fmap f x
lowerYo :: (Functor f) => Yoneda f a -> f a
lowerYo (Yoneda y) = y id
instance Functor (Yoneda f) where
fmap f (Yoneda y) = Yoneda $ \g -> y (g . f)
loopFusion = lowerYo . fmap f . fmap g …Run Code Online (Sandbox Code Playgroud) 我想弄清楚使用 Pinia store 而不是仅使用纯 ts 可组合函数(例如
const userName = ref('')
export default function useUser() {
const setUserName(name: string) => {
userName.value = name
}
return {
userName: readonly(userName),
setUserName
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用
const {userName, setUserName} = useUser()
Run Code Online (Sandbox Code Playgroud)
因为,例如在此 Vitesse 示例https://github.com/antfu/vitesse/blob/main/src/stores/user.ts中,Pinia 的用法看起来非常相似。
感谢您的澄清:)
有时我有两种形式的功能:
f :: a -> (b1,b2)
h :: b1 -> b2 -> c
Run Code Online (Sandbox Code Playgroud)
我需要组合物g.我通过将h改为h'来解决这个问题:
h' :: (b1,b2) -> c
Run Code Online (Sandbox Code Playgroud)
你能告诉我(如果可能的话)一个函数m,这样:
(h . m . f) == (h' . f)
Run Code Online (Sandbox Code Playgroud)
或者另一种处理这种情况的方法.谢谢.