A lambda is an expression that acts as an anonymous function. Use this tag if you would like to discuss the appropriateness of a lambda or anonymous function in your code.
1
vote
2answers
46 views
Read a txt file and split into 2 csvs based on regex
So I have been told that my code is not good and needs refactoring. I'm struggling to see what I need to do. How do I specify the success and failure conditions for lambda? Also don't get what the ...
1
vote
1answer
34 views
Use an optionally passed-in block without repeating yourself
I need to open and read several files for a project. Some of them are plain text files, and I just want a standard File.open to read from. One of them is an XML ...
2
votes
4answers
245 views
Checking whether a list is sorted, ascending or descending
What is the best way to conditionally use an operator? All 3 methods seemed acceptable to me, and the last 2 better.
If you're curious: I've got a list which is unsorted, and many other lists which ...
-1
votes
0answers
41 views
Get or generate a GUID for content stored in a CMS
The code below is trying to get a GUID in a Content stored in a CMS. If it's not stored, then a new one is generated, stored and returned.
I'm sure many of us are having troubles when it comes to ...
1
vote
1answer
50 views
Lambda to collect properties in a child object
I have a class:
public class ProductInvoice {
//Some other properties.
private List<ProductEntry> productEntries = newArrayList();
//otherMethods
}
...
5
votes
3answers
139 views
Filtering units by a dynamic Lambda expression
I have a simple if statement which, based on user input, determines which expression is going to be utilized. It looks like it can be done better.
...
1
vote
1answer
69 views
Routing Java objects using conditional consumers
As per a previous question here and answers I got, I used it to get a new implementation. I found that I have to make two implementations match once "to ignore other consumers after matching once" and ...
11
votes
1answer
137 views
Mandelbrot Streams
A recent Mandelbrot question (Hi @EBrown) in C# inspired me to build my own using the Java 8 parallel streams to implement the parallelism that's useful for computing each pixel value.
The intention ...
4
votes
2answers
46 views
Function for timing executions (inspired by MATLAB tic toc)
Inspired by MATLAB's tic/toc I've written a short helper function for calculating execution time.
...
0
votes
0answers
162 views
Generic method to pass lambda expressions performed on DTO's to Entity Framework
The following are generic methods that I use to perform lambda queries on DTO objects. The main method applies the expression to entities and returns DTO objects mapped from the entities (entities ...
10
votes
2answers
154 views
Return lambda expression based on parameters
This function returns a lambda expression based on the parameters sent. I am sure there is a better way to do this.
...
5
votes
3answers
201 views
Duplicate words in a text
Here is a simplified implementation to obtain the duplicate words in a text using lambda expressions.
...
3
votes
1answer
113 views
Event-listener implementation
While writing a C++ GUI application more or less from scratch I needed some form of an event-listener system, preferably using lambdas. An event should be able to have multiple listeners and the user ...
2
votes
1answer
56 views
Sort files by the type of file
I need to sort files by the type of the File. To accomplish the task I first wrote the code below:
...
11
votes
6answers
3k views
3
votes
1answer
51 views
Catch all checked exceptions and turn into unchecked exception
I am currently trying to learn Lambdas and following the book Java SE8 for the really Impatient and just finished Chapter 1 of it. This is the question, I am trying to get done with:
Didn’t you ...
7
votes
1answer
122 views
Proper checked exception handling with lambda functions
I'm having trouble finding a clean way to handle checked exceptions thrown by a lambda function passed as a parameter in Java.
I'm writing a Client which will interact via RMI with a Server to play a ...
0
votes
1answer
56 views
Repetitive code for a projected field in a lambda
I have a simple projection in my code. Taking away the majority of the fields which are not of interest, it looks like the following:
...
5
votes
2answers
1k views
Optimize method to iterate to over Map<String, List<Object>>
I have a Map<String, List<Object>> multiFieldMap and I need to iterate over its value set and add the value to ...
5
votes
1answer
125 views
Multiple functions to establish a fallback value for a property until not null
I have a property (called "Head") that can be set externally. When "Head" is not set, it uses Unity3D's MonoBehaviour functions to try to find the best value for ...
11
votes
2answers
488 views
Printing removed items using lambdas and streams
I'm struggling to make my lambdas readable.
I've seen various approaches. Below are three different examples that all do the same thing.
Forgive the example: I suspect there are better actual ...
2
votes
2answers
102 views
Comparing triangles using Java 8 streams
I'm trying to get more familiar with Java 8 streams as they seem to be very powerful (and shorter), so I rewrote a method to use streams. However I'm not very satisfied with it and would like some ...
2
votes
1answer
502 views
Asynchronous connect() with one function call, using C++11 lambdas and Boost.Asio
I've wanted to write a C++11 class for making IPv4 TCP connections and figured that it probably could be done with just one static function using some shared_ptr and lambda "magic". The following code ...
7
votes
2answers
268 views
Simple calculator implemented with lambdas
I was searching for a way to exercise the Java 8 concepts I've been learning, especially pertaining to Lambdas. Incidentally, I hadn't used any swing in a while and wanted a refresher. This is the ...
12
votes
3answers
534 views
The Beauty and the Strings
This originally appeared in Facebook's hacker cup 2013. Solving it was fun, though I find myself looping through similar data quite often.
Challenge:
Print the maximum beauty of strings.
...
4
votes
1answer
341 views
Java 8 CompletableFuture - fan out implementation
I was wondering what is the best way to implement a fan out type of functionality with Java 8 Completable future. I recently rewrote a function that had a bunch of old ...
6
votes
4answers
8k views
Using LINQ or Lambda instead of nested and multiple foreach statements
I want to use a better syntax than nested foreach statements to overwrite the initial list with items from the second list.
In the code below:
I want to ...
8
votes
3answers
152 views
Sending duplicate to heaven with Java 8
Follow up to Duplicate like a weapon, arrays like heaven.
@rolfl suggested in chat that I try to complete this challenge again in a Java 8 friendly way. I took the opportunity to also employ the ...
7
votes
2answers
971 views
5
votes
1answer
83 views
Effective use of branch prediction in terrain generator
I am trying to learn how branch optimization works.
For an experiment, I have a recursive fractal terrain generation and I have moved all if statements to binary ...
1
vote
0answers
46 views
Parallel for loop in Java - follow-up 2
The previous iteration at Parallel for loop in Java 8 - follow-up.
The changes are as follows:
MyTask is removed.
Synchronization removed. Now the user is ...
1
vote
1answer
812 views
Parallel for loop in Java 8 - follow-up
The previous and initial iteration at Parallel for loop in Java 8.
Changes are as follows:
ParallelLoopBody removed; ...
3
votes
2answers
286 views
Parallel for loop in Java 8
This is my first attempt to provide some syntactic sugar for doing mutually independent loop iterations in parallel. Thanks to Java 8 lambdas, I can write the parallel loops in pretty elegant fashion ...
3
votes
1answer
150 views
How can I avoid repeated code using lambdas?
I have a method which should accept a Function/Action and do something with that method (in my case I've added retry logic).
I'm wondering how I can remove the duplicated code (for loop, etc) from ...
4
votes
1answer
39 views
Generic Timing Class - Follow Up
I wrote a class which can time and compare functions.
I already posted it here once before, and got great suggestions from @rolfl, which I have added to my code.
my original question can be found ...
2
votes
1answer
55 views
Conditional statements with doctor and patient information
I'm focusing on trying to write clean, modular Python 2.7.5 code.
...
10
votes
3answers
906 views
Displaying information about people from a JSON file
I'm working with Python 2.7.5 and I have written the following code:
...
6
votes
1answer
61 views
Generic Timing Class
I have written a class which can time functions, and I'd like to have it reviewed. I'm interested in everything (better naming and commenting, accuracy of measurement, usability, structure, use of ...
13
votes
2answers
329 views
Collatz conjecture using Java lambdas
From the Wikipedia article:
Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3*n* + 1. Repeat the process (which has been ...
9
votes
1answer
567 views
Too much fun with EnumMap-related features and lambdas?
I have created an utilities class to handle certain EnumMap-related features, such as creating and converting between ...
10
votes
1answer
95 views
Shady Characters
As part of my familiarization with the features of Java 8, and inspired by this question, I thought I would take the 'Shady Character' problem to 'the next level'.
The basic problem is that you have ...
1
vote
1answer
298 views
Group by two lists in one map using lambda
I've got two collections: collection of rules, and collection of keys that can handle specific rules. Key have method ...
8
votes
1answer
230 views
Pokemon Type Evaluator part 2
I started anew and completely refactored the way I implemented Pokemon Type Evaluator.
Also, first use of a lambda expression (Case of using it, but still don't fully grasp how it's working). I'm ...
10
votes
2answers
220 views
A method that generates a Comparator<String> using lambda expression
Given below is an exercise from the book *Java SE 8 for the Really Impatient( by Cay S. Horstmann:
Write a method that generates a Comparator that can be normal or
reversed, case-sensitive or ...
2
votes
1answer
450 views
Is there better way to shorten this Lambda expression's result selector?
I'm new in LINQ and Lambda expression. I'm not sure is there a way to shorten the result on this o.z.cust.cust.cust.bintAccountNo. As you can see below, the more ...
10
votes
1answer
1k views
EF6-Lambda Expressions deep big-tree query
I am looking to improve the following query.
We are using EF6 (CodeFirst) and C# Lambda Expressions, and Repository/Unit of Work patterns. Also, we needed to restrict the query in a way that we ...
4
votes
2answers
747 views
Platform independant thread pool v3
This is a continuation of this question, v4 can be found here
Taking the advise given previously, the deadlock was fixed in the non-yielding code. The next problem comes from the suggestion to make ...
2
votes
1answer
304 views
Refactor Oracle Entity Framework lambda query
I had a ugly problem and wrote an ugly query.
Query creates a cache that is used to get person id when I have {full_name, id_code?, birth_date?}. By question mark I mean that it is perfectly valid ...
3
votes
1answer
529 views
Exception handling with expression lambda in Presenter class
Following is a code snippet from MVP Win Forms application.
My DAL doesn't handle exceptions and it will be propagated up to the calling method in the presenter classes where the exception will be ...
2
votes
3answers
331 views
Boost::range::for_each + functor
I would like your comments on my code in C++ (C++98 + Boost)
I have written some code to use the functors with the boost::range::for_each algorithm.
My main ...