Using linq to append list to list from select method - list

I have a collection A of let's say 100 items. From that list I want to perform a where clause which can rule out let's say 20 of items.
Is there a way to use Select clause or something else on items in which I could use external method that returns 2 items.
I would need to end up with 160 objects from the original list.
What I currently have is
public List<A> ToAList(B item)
{
return new List<A> {new A(), new A()};
}
If I make this call
originalList.Where(x => true).Select(y => ToAList(y)).ToList();
I end up having a list of 80 (from pseudo example) two-item A lists instead of a list containing 160 objects A.
I am looking for a way to avoid loops. Just plain Select or AddRange trick that could result in one list.

You can use SelectMany:
originalList.Where(x => true).SelectMany(y => ToAList(y)).ToList();

Related

How to remove a "List of items" from a list?

I have two lists : _bufferCarnetList and _deletedWords.
When the user hits OK: I want to remove from _bufferCarnetList the items contained in _deletedWords.
I have tried using the .remove method, but nothing happens.
_bufferCarnetList.remove(_deletedWords).
Is there a way to do it in a direct way, or should I iterate the list and remove each item separately ?
Use the removeWhere method:
_bufferCarnetList.removeWhere((e) => _deletedWords.contains(e));

Running a sequence in batches using asSequence()?

I have a function:
private fun importProductsInSequence(products: List<Product>): List<Result> =
products.asSequence()
.map { saveProduct(it)}
.takeWhile { products.isNotEmpty() }
.toList()
Is there a possibility to rewrite this sequence so it works in batches? In example, a list of 1000 products is passed to this method, and the sequence is taking 100 products, saves them, then next 100 until products.isNotEmpty() condition is met?
takeWhile is not needed here, as the products list size doesn't change even after you iterate over the sequence. products.isNotEmpty() will always be true
Kotlin has chunked method that can give you objects in batches as required
private fun importProductsInSequence(products: List<Product>): List<Result> =
products.asSequence()
.chunked(100)
.map { saveProducts(it)} // saveProducts method would take list of products and return list of Result
.flatten()
.toList()
You neeed to use chunked
private fun importProductsInSequence(products: List<Product>): List<Product>{
products.asSequence().chunked(100).onEach{ saveProduct(it)}.flatten().toList()
chunk will partition the list to the size provided, the last portion will have less elements than previous one.

How drools to iterate over a list

I'm new to Drools6.4.0.FINAL and want to use it to iterate over a list of items and process my business logic
my business data return List ,I want insert it into KieSession
List<MyObject> list = service.queryList(Map<String,Object> param);
kSession.insert(list);
kSession.fireAllRules();
my drl file like this :
import java.util.List;
import xxx.xxx.MyObject;
rule "rule 1"
salience 1
activation-group "ctoc_order_rule"
when
$mo:MyObject(orgunitid_lev1 == 58094);
then
$mo.setBusiness_type_id(201);
$mo.setBusiness_type_name("business201");
update($mo);
end
But this not fire my rules,How can I do to fire a List ? thanks
If you want to match a java.util.List, insert a List and write a pattern List(...).
If you want to match a xxx.xxx.MyObject, insert a MyObject and write a pattern MyObject(...).
If you insert a List and have pattern MyObject(...), it cannot match. It would be possible to match a List and extract the elements (using from), but matching (generic) container classes is somewhat of an anti-pattern. What if you have several kinds of List containing various object?

Iterate over an existing map against a list of tuples scala

I have a list of tuples that I must change the values for in a map that contains those tuples. So if I have a list such as List((0,2), (0,3)) with a map that looks like this: Map((0,2) => List(1,2,3), (0,3) => List(1,2)), I need to access the matching map tuples with the tuples listed in the list, then remove a number from the mapping.
So in the example above, if I wanted to remove 2 from the mapping, I would get Map((0,2) => List(1,3), (0,3) => List(1)).
Design wise, I was thinking of pattern matching the map, but I've read some answers that said that may not be the best way. The tough part for me is that it has to be immutable, so I was thinking of pattern matching the list, getting the map value, change the value, then recreate the map and recursively call the function again. What do you think of this implementation?
This could be a way to remove 2 from your Map:
val newMap = oldMap.mapValues(list => list.filter(_ != 2))
Or more generally:
def filterInMap(element: Int, oldMap: Map[(Int,Int),List[Int]]) =
oldMap.mapValues(list => list.filter(_ != element))
This way there's no need to mutate anything at all. mapValues transforms just the values of your Map and returns a copy of the original without mutating it at all. filter then gets the job done by only allowing elements that don't match the element we would like to remove.
Bonus: even more generally:
def filterInMap[A](element: A, oldMap: Map[(A,A),List[A]]) =
oldMap.mapValues(list => list.filter(_ != element))

How to check and change the value of a list?

I would like to check all list values in a list and change them if necessary.
p.e.
I want to check the next lists if there are values higher or lower then the next values:
min-value = 6
max-value = 22
mylist = ['4-8','25','16-19','21-32']
if one of the list values is below the min-value or higher then the max-value, the list values must be changed to the min-value and max-value. p.e. in example, the new list must be:
mylist = ['6-8','22','16-19','21-22']
if the entire value of the list item is below the min-value or higher then the max-value the list item can be removed.
How can I check my list values and change them?
There are two approaches. In the procedural one, you iterate over the list items and modify or skip the element:
let newlist = []
for element in mylist
" Parse element.
if ! OutsideBounds(element)
call add(newlist, AdjustBounds(element))
endif
endfor
In the functional programming approach, you use the built-in map() to modify elements (i.e. adjust the bounds), but that one cannot remove elements. So just empty those elements and then do a second pass with filter() to remove them. Note that both functions modify the original lists, so use copy() if you need to keep the original.
call filter(map(mylist, 'AdjustBounds(v:val)'), '! OutsideBounds(v:val)')
I hope I don't need to tell you how to write the AdjustBounds() and OutsideBounds() functions...