LINQ query — Data aggregation (group adjacent)

You can use Linq’s GroupBy in a modified version which groups only if the two items are adjacent, then it’s easy as: var result = classes .GroupAdjacent(c => c.Value) .Select(g => new { SequenceNumFrom = g.Min(c => c.SequenceNumber), SequenceNumTo = g.Max(c => c.SequenceNumber), Value = g.Key }); foreach (var x in result) Console.WriteLine(“SequenceNumFrom:{0} SequenceNumTo:{1} Value:{2}”, … Read more

SVG Positioning

Everything in the g element is positioned relative to the current transform matrix. To move the content, just put the transformation in the g element: <g transform=”translate(20,2.5) rotate(10)”> <rect x=”0″ y=”0″ width=”60″ height=”10″/> </g> Links: Example from the SVG 1.1 spec

Merge 2d array rows with same column value and sum another column [duplicate]

Use function array_reduce() to combine the items having the same city: $input = array( array(‘city’ => ‘NewYork’, ‘cash’ => ‘1000’), array(‘city’ => ‘Philadelphia’, ‘cash’ => ‘2300’), array(‘city’ => ‘NewYork’, ‘cash’ => ‘2000’), ); $output = array_reduce( // Process the input list $input, // Add each $item from $input to $carry (partial results) function (array $carry, … Read more

Grouping by object value, counting and then setting group key by maximum object attribute

Here’s one approach. First group into lists and then process the lists into the values you actually want: import static java.util.Comparator.comparingLong; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.toMap; Map<Route,Integer> routeCounts = routes.stream() .collect(groupingBy(x -> x)) .values().stream() .collect(toMap( lst -> lst.stream().max(comparingLong(Route::getLastUpdated)).get(), List::size ));

tech