lambda

Kotlin filter lambda array using iteration index

孤街醉人 提交于 2021-02-07 12:17:33
问题 I would like to filter an array into an array of every nth item. For examples: fun getNth(array: Array<Any>, n: Int): Array<Any> { val newList = ArrayList<Any>() for (i in 0..array.size) { if (i % n == 0) { newList.add(array[i]) } } return newList.toArray() } Is there an idiomatic way to do this using for example Kotlin's .filter() and without A) provisioning a new ArrayList and B) manually iterating with a for/in loop? 回答1: filterIndexed function is suited exactly for this case: array

Kotlin filter lambda array using iteration index

北城以北 提交于 2021-02-07 12:17:05
问题 I would like to filter an array into an array of every nth item. For examples: fun getNth(array: Array<Any>, n: Int): Array<Any> { val newList = ArrayList<Any>() for (i in 0..array.size) { if (i % n == 0) { newList.add(array[i]) } } return newList.toArray() } Is there an idiomatic way to do this using for example Kotlin's .filter() and without A) provisioning a new ArrayList and B) manually iterating with a for/in loop? 回答1: filterIndexed function is suited exactly for this case: array

Passing a lambda with moved capture to function

此生再无相见时 提交于 2021-02-07 11:40:30
问题 I recently struggled with a bug hard to find for me. I tried to pass a lambda to a function taking a std::function object. The lambda was capturing a noncopyable object. I figured out, obviously some copy must happen in between all the passings. I came to this result because I always ended in an error: use of deleted function error. Here is the code which produces this error: void call_func(std::function<void()> func) { func(); } int main() { std::fstream fs{"test.txt", std::fstream::out};

Cannot initialize std::variant with various lambda expressions

大城市里の小女人 提交于 2021-02-07 11:23:07
问题 I'm playing with std::variant, lambdas and std::future , and got super weird results when I tried to compose them together. Here are examples: using variant_t = std::variant< std::function<std::future<void>(int)>, std::function<void(int)> >; auto f1 = [](int) { return std::async([] { return 1; }); }; auto f2 = [](int) { return std::async([] { }); }; variant_t v1(std::move(f1)); // !!! why DOES this one compile when it SHOULDN'T? auto idx1 = v1.index(); //equals 1. WHY? variant_t v2(std::move

Aggregating more than two properties Java 8

泪湿孤枕 提交于 2021-02-07 10:59:34
问题 To be very simple I have class Per{ int a; long b; double c; String d; } Let say I have 3000 Object of Type Per and collected in a List<Per> pers Now I want to achieve:- Skip if object is null or d is null or blank sum of a sum of b aggregated value of operation performed on c Old way is int totalA = 0; long totalB = 0l; long totalC = 0l; for (Per per : pers) { if (per.d != null && !per.d.trim().equals("")) { totalA += per.a; totalB += per.b; totalC += someOperation(per.c); } } someOperation

Java: sorting an array with lambda expression?

て烟熏妆下的殇ゞ 提交于 2021-02-07 09:17:05
问题 I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order. The way I am trying to do this with lambda is as follows: Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1); The issue with this is that my compiler says: Error:(12, 32) java: method sorted in interface java.util.stream.IntStream cannot be applied to given types; required: no arguments found: (x,y)->Int[...]== -1 reason: actual and formal

Java: sorting an array with lambda expression?

百般思念 提交于 2021-02-07 09:16:11
问题 I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order. The way I am trying to do this with lambda is as follows: Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1); The issue with this is that my compiler says: Error:(12, 32) java: method sorted in interface java.util.stream.IntStream cannot be applied to given types; required: no arguments found: (x,y)->Int[...]== -1 reason: actual and formal

Do we need fixed point combinators in C#?

删除回忆录丶 提交于 2021-02-07 08:57:15
问题 I was playing with recursive lambdas in C# and have found two approaches to do this on the web. One approach uses fixed point combinator and the other does not. In the code below f1 is built using combinator, and f2 is defined directly. My question is, do we need fixed point combinators in C# or the language already provides all we need, so we can leave them alone? class Program { static Func<T, T> F<T>(Func<Func<T,T>,Func<T,T>> f) { return x => f(F(f))(x); } static void Main(string[] args) {

C# Compiled lambda expressions instance creation and/or garbage collection?

我只是一个虾纸丫 提交于 2021-02-07 08:32:26
问题 Consider the following code sample: using System; using System.Linq.Expressions; public class Class1<T, Y> { public Class1(Expression<Func<T, Y>> mapExpression) { GetValue = mapExpression.Compile(); } public Func<T, Y> GetValue { get; protected set; } } public class DataClass { public long Data { get; set; } } Now suppose that I make in different places new instances of Class1, e.g. var instance1 = new Class1<DataClass, long>(x => x.Data); var instance2 = new Class1<DataClass, long>(x => x

C# Compiled lambda expressions instance creation and/or garbage collection?

自古美人都是妖i 提交于 2021-02-07 08:31:27
问题 Consider the following code sample: using System; using System.Linq.Expressions; public class Class1<T, Y> { public Class1(Expression<Func<T, Y>> mapExpression) { GetValue = mapExpression.Compile(); } public Func<T, Y> GetValue { get; protected set; } } public class DataClass { public long Data { get; set; } } Now suppose that I make in different places new instances of Class1, e.g. var instance1 = new Class1<DataClass, long>(x => x.Data); var instance2 = new Class1<DataClass, long>(x => x