lambda

How can I use lambda function within itself?

人走茶凉 提交于 2021-02-06 16:00:51
问题 I have this code and don't know if what I would like to achieve is possible. _acceptor.async_accept( _connections.back()->socket(), [this](const boost::system::error_code& ec) { _connections.push_back(std::make_shared<TcpConnection>(_acceptor.get_io_service())); _acceptor.async_accept(_connections.back()->socket(), this_lambda_function); } ); Once a socket is accepted, I would like to reuse the handler (aka the lambda function). Is this possible? Is there a better way to accomplish this? 回答1:

What's the best way of ensuring a Function argument is serializable?

折月煮酒 提交于 2021-02-06 15:39:18
问题 I'm writing a serializable class that takes several arguments, including a Function : public class Cls implements Serializable { private final Collection<String> _coll; private final Function<String, ?> _func; public Cls(Collection<String> coll, Function<String, ?> func) { _coll = coll; _func = func; } } func is stored in a member variable, and so needs to be serializable. Java lambdas are serializable if the type they're being assigned to is serializable. What's the best way to ensure that

Method reference in Java 8

假装没事ソ 提交于 2021-02-06 15:26:11
问题 public class Car { private int maxSpeed; public Car(int maxSpeed) { this.maxSpeed = maxSpeed; } public int getMaxSpeed() { return maxSpeed; } } We can sort a list of cars by, Car carX = new Car(155); Car carY = new Car(140); List<Car> cars = new ArrayList<>(); cars.add(carX); cars.add(carY); cars.sort(Comparator.comparing(Car::getMaxSpeed)); If we see the signature of the method Comparator.comparing , the input parameter type is Function<? super T, ? extends U> In the above example, how is

How To Use Classic Custom Data Structures As Java 8 Streams

有些话、适合烂在心里 提交于 2021-02-06 12:46:10
问题 I saw a SO question yesterday about implementing a classic linked list in Java. It was clearly an assignment from an undergraduate data structures class. It's easy to find questions and implementations for lists, trees, etc. in all languages. I've been learning about Java lambdas and trying to use them at every opportunity to get the idiom under my fingers. This question made me wonder: How would I write a custom list or tree so I could use it in all the Java 8 lambda machinery? All the

Why sharp quote lambda expressions?

巧了我就是萌 提交于 2021-02-06 09:58:55
问题 It is a technique used frequently in On Lisp , which is on Common Lisp: > (mapcar #'(lambda (x) (+ x 10)) '(1 2 3)) (11 12 13) Why is sharp-quote needed or even possible? lambda expressions return function objects, and sharp quoting returns function objects from names. I also have heard contradictory information on whether lambda expressions are names - in particular On Lisp contradicts the standard, but his code seems to work which also contradicts the standard. In elisp it seems it's not

Why sharp quote lambda expressions?

不打扰是莪最后的温柔 提交于 2021-02-06 09:58:25
问题 It is a technique used frequently in On Lisp , which is on Common Lisp: > (mapcar #'(lambda (x) (+ x 10)) '(1 2 3)) (11 12 13) Why is sharp-quote needed or even possible? lambda expressions return function objects, and sharp quoting returns function objects from names. I also have heard contradictory information on whether lambda expressions are names - in particular On Lisp contradicts the standard, but his code seems to work which also contradicts the standard. In elisp it seems it's not

【SICP练习】5 练习1.9

ぃ、小莉子 提交于 2021-02-06 07:59:42
 以下是第一个加起两个正整数的方法,其中 inc 将参数加 1 , dec 将参数减 1 。 (define (+ a b) (if(= a 0) b (inc (+ (dec a) b)))) 用代换模型展示 (+ 4 5) 如下: (+ 4 5) (inc (+ 3 5)) (inc (inc (+ 2 5))) (inc (inc (inc (+ 1 5)))) (inc (inc (inc (inc (+ 0 5))))) (inc (inc (inc (inc 5)))) (inc (inc (inc 6))) (inc (inc 7)) (inc 8) 9 如上所示,在代换模型展示中包含了伸展和收缩两个阶段,并且伸展阶段所需的额外存储量和计算所需的步数都正比于参数 a 。因此这是一个线性递归过程。 以下是另一个加起两个正整数的方法。 (define (+ a b) (if(= a 0) b (+ (dec a) (inc b)))) 同样用代换模型展示 (+ 4 5) 如下: (+ 4 5) (+ 3 6) (+ 2 7) (+ 1 8) (+ 0 9) 9 在这个过程中并没有任何增长或者收缩,而其计算过程可用固定数目的状态变量( a )描述。这是一个线性迭代过程。 版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http:/

【SICP练习】2 练习1.6

こ雲淡風輕ζ 提交于 2021-02-06 07:47:19
 练习 1.6 这道题通过由一个新版本的 if 来引出,主要讨论的还是应用序和正则序的问题。我看到“将 if 提供为一种特殊形式”时还满头雾水,并不太清楚什么特殊形式。当再返回看 if 的语法时才发现,这在第 12 页 if 的一般表达式下面一段。如果 <predicate> 得到真值,解释器就去求值 <consequent> 并返回其值。注意,在此处已经返回其值了,并没有进行后续运算。 而通过 cond 写出来的常规过程的 if ,在解释器采用应用序求值的情况下,如果第一次运算 good-enough? 时为真,则直接返回了 guess 。 原文中的求平方根的程序: (define (new-if predicate then-clauseelse-clause) (cond(predicate then-clause) (elseelse-clause))) (define (sqrt-iter guess x) (new-if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define(sqrt-iter guess x) (if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 于是博主进行了如下测试: (sqrt

Objective-C++ 11 - Why can't we assign a block to a lambda?

别来无恙 提交于 2021-02-06 02:07:49
问题 So, I just upgraded to Xcode 4.4, and I noticed in the changelog: Apple LLVM compiler supports additional C++11 features, including lambdas Which is awesome! So I got around to coding, and I found a few things out: Lambdas are assignable to Objective-C blocks: void (^block)() = []() -> void { NSLog(@"Inside Lambda called as block!"); }; block(); std::function can hold an Objective-C block: std::function<void(void)> func = ^{ NSLog(@"Block inside std::function"); }; func(); We cant assign an

Objective-C++ 11 - Why can't we assign a block to a lambda?

馋奶兔 提交于 2021-02-06 02:02:05
问题 So, I just upgraded to Xcode 4.4, and I noticed in the changelog: Apple LLVM compiler supports additional C++11 features, including lambdas Which is awesome! So I got around to coding, and I found a few things out: Lambdas are assignable to Objective-C blocks: void (^block)() = []() -> void { NSLog(@"Inside Lambda called as block!"); }; block(); std::function can hold an Objective-C block: std::function<void(void)> func = ^{ NSLog(@"Block inside std::function"); }; func(); We cant assign an