我想知道shift是否是更高阶函数.
chartoInt :: Char -> Int
chartoInt c = ord c
Inttochar :: Int -> Char
Inttochar n = chr n
shift :: Int -> Char -> Char
shift n c = Inttochar (chartoInt c + n)
Run Code Online (Sandbox Code Playgroud) def tracer(fn):
def traced(x):
print('Calling', fn, '(', x, ')')
result = fn(x)
print('Got', result, 'from', fn, '(', x, ')')
return result
return traced
def fact(n):
if n == 0:
return 1
return n * fact(n-1)
new_fact = tracer(fact)
new_fact(2)
Run Code Online (Sandbox Code Playgroud)
我在 pythontutor.com 上使用了这段代码来更好地理解高阶函数,但我很难理解为什么new_fact(2)在步骤 8 中映射到traced? 换句话说,traced函数如何知道参数是2?
向大家学习Haskell("高阶函数"一节,"有些高阶有序"这一小节)描述了一个函数applyTwice,它在一个参数上调用两次函数:
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
Run Code Online (Sandbox Code Playgroud)
但是我需要一个函数,它可以在某个参数上应用某些函数任意次数.例如applyN 3 f x,相当于f $ f $ f x.我如何在Haskell中编写重复应用程序的功能?请发布任何可能的解决方案,使用递归,高阶函数或其他任何东西.
我有一个字典数组
var details:[[String:String]] = [["name":"a","age":"1"],["name":"b","age":"2"],["name":"c","age":""]]
print(details)//[["name": "a", "age": "1"], ["name": "b", "age": "2"], ["name": "c", "age": ""]]
Run Code Online (Sandbox Code Playgroud)
现在,我想从值是空字符串的数组中删除字典。我已经通过嵌套的for循环实现了这一点。
for (index,detail) in details.enumerated()
{
for (key, value) in detail
{
if value == ""
{
details.remove(at: index)
}
}
}
print(details)//[["name": "a", "age": "1"], ["name": "b", "age": "2"]]
Run Code Online (Sandbox Code Playgroud)
如何使用高阶函数(Map,Filter,Reduce和FlatMap)实现此目标
我想在 Javascript 中使用 map() 或 reduce() 函数打印斐波那契数列。我在网上找不到任何关于它的东西。我不太确定我在 map() 中的情况。
我基本上在写
fib(n){
return new Array(n-1).fill(1).map((_,i) => *This is where I am confused* ) ;
}
Run Code Online (Sandbox Code Playgroud) 我在理解OCaml中高阶函数的函数类型签名时遇到麻烦。
fun f -> f 3
(int -> a) -> a
Run Code Online (Sandbox Code Playgroud)
我处理的方式是该f 3部分将一个int作为输入并返回由函数定义的类型,该类型f表示为a。所以真的fun f是一种类型(int->a)。但是,最后a一个来自(int -> a) -> a哪里?
syntax ocaml type-inference higher-order-functions type-signature
我有以下Haskell表达式:
let increment x = 1 + x
in \xs -> map chr (map increment (map ord xs))
Run Code Online (Sandbox Code Playgroud)
我知道上面的表达式接受一个列表,并将ord函数应用于列表中的每个元素。对于字符列表,结果将是整数列表(ASCII值)。然后,increment函数将列表中的每个整数递增1。然后,chr函数将每个整数转换回其对应的字符。
而且我还有以下表达式:
map chr . map (1+) . map ord
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚上面是否等于第一个表达式。但是,当我在GHCI中尝试上述表达式时,出现错误:
map chr . map (1+) . map ord ['a', 'b', 'c', 'd', 'e']
Run Code Online (Sandbox Code Playgroud)
我不确定为什么该表达方式无效。由于使用了函数组合,因此该表达式不会被评估为:
(map chr (map (1+) (map ord ['a', 'b', 'c', 'd', 'e'])))
Run Code Online (Sandbox Code Playgroud)
随着map (1+)越来越列表作为的结果map ord,并map chr得到一个列表作为的结果map (1+)?
我有一个这样的功能:
def getSomething: (String, Future[String]) => String = {
case (name, surname) if (name == "Joe", surname.map(s => s == "Doe")) => "Is ok"
}
Run Code Online (Sandbox Code Playgroud)
但是编译器说他需要executionContext这里的map函数。我试图做一些魔术:
def getSomething (implicit e: ExecutionContext): (String, Future[String]) => String{...}
Run Code Online (Sandbox Code Playgroud)
或者
def getSomething: (String, Future[String])(implicit e: ExecutionContext) => String{...}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。是否可以将隐式参数传递给这样的功能?或者我可以用其他方式来做吗?
我已经阅读了很多文章,但仍有一些我难以理解的事情。我不明白的地方在哪里?我的问题在代码中。我希望我问对了。
fun main() {
/*
1- Is it argument or parameter in numbers{} block?
where is it sent to the argument? why do we send "4" if it is parameter?
Are all the functions I will write in (eg println) sent to the numbers function? But this HOF can
only take one parameter.
*/
numbers {
/*
2-How does this know that he will work 3 times?
According to the following 3 functions? Is there a for loop logic??
*/ …Run Code Online (Sandbox Code Playgroud) 假设我们有一堂课:
class Foo {
var1: string = 'var1';
var2: string = 'var2';
hello(request: A): Promise<B> { }
world(request: C): Promise<D> { }
}
Run Code Online (Sandbox Code Playgroud)
我想实现执行以下实例方法的函数Foo:
const foo = new Foo();
const executeFoo = (methodName: string, firstParam: any) => { // <- I'm stuck in this arrow function.
return foo[methodName](firstParam);
};
executeFoo('hello', testParam); // testParams is type of A, then return type should Promise<B>.
executeFoo('world', testParam2); // testParams2 is type of C, then return type should Promise<D>.
Run Code Online (Sandbox Code Playgroud)
有没有办法定义 的类型executeFoo …
javascript higher-order-functions typescript function-definition
haskell ×3
arrays ×2
javascript ×2
dictionary ×1
implicit ×1
kotlin ×1
lambda ×1
ocaml ×1
python ×1
scala ×1
swift ×1
syntax ×1
typescript ×1