在Swift文档中,Apple说:
闭包是自包含的功能块,可以在代码中传递和使用.Swift中的闭包类似于C和Objective-C中的块以及其他编程语言中的lambdas.
我认为是第一类功能的定义
他们也这样说:
闭包可以从定义它们的上下文中捕获和存储对任何常量和变量的引用.这被称为关闭那些常量和变量.Swift为您处理捕获的所有内存管理.
我认为这是封闭的定义,而另一种缺陷是一流的功能,但Apple似乎把它们放在一起并称之为封闭.
我误解了什么吗?或者是Apple调用闭包和一流的函数闭包?
我已经编写了这个示例代码,并且想知道我是否在撰写评论中是对的?
// 'a' takes a first class function, which makes 'a' a higher order function
func a(ch: () -> Void){
print("Something")
ch() // 'ch' is a first class function
print("Ended")
}
func closureFunc(){
var num = 2
a({
// access to 'num' is possible by closures
num = num*2
print(num)
})
}
closureFunc()
Run Code Online (Sandbox Code Playgroud) 假设我们在一个类中有几个重载函数:
func appendToABC(string s: String) -> String {
return "ABC \(s)"
}
func appendToABC(duplicatedString s: String) -> String {
return "ABC \(s)\(s)"
}
Run Code Online (Sandbox Code Playgroud)
我们有一些API可以将函数作为参数:
func printString(function: (String) -> String) {
print(function("ASD"))
}
Run Code Online (Sandbox Code Playgroud)
我们如何将appendToABC函数之一作为参数传递给printString函数?
我已经考虑过使用闭包来包装函数,但它看起来并不好看
printString { appendToABC(duplicatedString: $0) }
Run Code Online (Sandbox Code Playgroud) 关于第一类函数的小知识是它支持传递函数作为参数,我们也可以将它们作为另一个函数中的值返回...我是Swift编程语言的新手,任何人都可以用一个例子来详细说明.
我有一个函数,它接受一个函数和一个数字,并返回函数的数字应用程序和一个立方体函数:
(defn something [fn x]
(fn x))
(defn cube [x]
(* x x x))
Run Code Online (Sandbox Code Playgroud)
当我按如下方式调用该函数时,它可以工作:
(something cube 4)
Run Code Online (Sandbox Code Playgroud)
但是这会返回一个错误:
(something Math/sin 3.14)
Run Code Online (Sandbox Code Playgroud)
但是,这有效:
(something #(Math/sin %) 3.14)
Run Code Online (Sandbox Code Playgroud)
解释是什么?
我正在尝试动态调用返回不同类型的函数struct.
例如,让我们采用以下代码.
struct A {
Name string
Value int
}
struct B {
Name1 string
Name2 string
Value float
}
func doA() (A) {
// some code returning A
}
func doB() (B) {
// some code returning B
}
Run Code Online (Sandbox Code Playgroud)
我想将函数doA或doB作为参数传递给将执行函数和JSON编码结果的泛型函数.如下:
func Generic(w io.Writer, fn func() (interface {}) {
result := fn()
json.NewEncoder(w).Encode(result)
}
Run Code Online (Sandbox Code Playgroud)
但当我这样做时:
Generic(w, doA)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
cannot use doA (type func() (A)) as type func() (interface {})
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这种动态调用?
在"Clojure in Action"中进行以下示例(p.63):
(defn basic-item-total [price quantity]
(* price quantity))
(defn with-line-item-conditions [f price quantity]
{:pre [(> price 0) (> quantity 0)]
:post [(> % 1)]}
(apply f price quantity))
Run Code Online (Sandbox Code Playgroud)
评估REPL:
(with-line-item-conditions basic-item-total 20 1)
Run Code Online (Sandbox Code Playgroud)
结果抛出以下异常:
Don't know how to create ISeq from: java.lang.Long
[Thrown class java.lang.IllegalArgumentException]
Run Code Online (Sandbox Code Playgroud)
在评估应用程序后,似乎抛出了异常.
exception clojure first-class-functions higher-order-functions
我理解闭包的广义概念(函数在定义时与环境快照一起存储),作为一等公民的函数意味着函数可以像语言中的任何其他数据类型一样传递和返回.
有趣的是,我所使用的所有语言都具有一等公民的功能,例如Python,Javascript,Scheme似乎总是有闭包.
事实上,传递和返回函数的闭包是实现函数作为语言的一等公民的一种方式,但我不确定是否能够编写它们是函数作为一等公民的直接和不可避免的后果.
提出更具体的条款:
你能提供一个实际的例子,其拥有一流的功能的语言,但它是不是可以写倒闭?
javascript python functional-programming first-class-functions
在C++中,我可以动态创建一个接口的实现(理想情况下绑定本地范围的变量.)不确定如何更好地解释它,所以我将放下我希望代码看起来像(大致):
// Given the following:
class Visitor
{
virtual void visit(const Data& data) = 0;
}
class DataStore
{
void visitData(Visitor& visitor) { /** Invokes visitor with each item of data. */ }
}
// Imagine one would write something like:
void inSomeFunction(DataStore& store)
{
std::string myName = "bob";
class MyVisitor: public Visitor
{
public:
MyVisitor(const std::string& p): person(p);
virtual void visit(const Data& data) override
{
std::cout << person << " is visiting " << data << std::endl;
}
private: …Run Code Online (Sandbox Code Playgroud) 将函数模板作为参数传递给另一个函数模板总是有点棘手。通常,人们必须创建一个 lambda 对象来调用原始函数。
template <typename It>
void f(It, It) {}
void g(std::vector<int>::iterator, std::vector<int>::iterator) {}
template <typename C, typename F>
void callA(C&& c, F callable) {
return callable(std::begin(c), std::end(c));
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个std::vector<int> c,我不能直接传递f给callA因为f是一个模板而不是一个函数:
callA(c, f); // f has an unresolved overloaded function type and cannot be deduced
callA(c, std::distance); // same problem
callA(c, g); // works because g is a proper function, not a template
callA(c, [](auto a, auto b) {return f(a,b);}); …Run Code Online (Sandbox Code Playgroud) c++ templates first-class-functions language-lawyer higher-order-functions
我刚刚开始使用Angular 2和TypeScript,我似乎无法弄清楚如何使用回调函数,我知道这可能是一个愚蠢的问题但是给出了这个常规的javascript代码:
someOnject.doSomething('dsadsaks', function(data){
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
TypeScript中的等价物是什么?