I am working on rebuilding my blog here on GitHub.
viceroypenguin / superlinq Goto Github PK
View Code? Open in Web Editor NEWExtensions to LINQ to Objects
License: Apache License 2.0
Extensions to LINQ to Objects
License: Apache License 2.0
public static IEnumerable<TSource> Replace<TSource>(this IEnumerable<TSource> source, Index index, TSource value);
Used to replace a single item at a specific index while streaming a sequence.
Debate: Add .LoopJoin() and .HashJoin() as aliases to .Select() and .Join(), for completeness/equivalence to .MergeJoin()?
I updated to the latest package and see MaxElementsBy has been removed. In the doc I see that the recommended method to use instead is PartialSortBy but no example is provided.
I want to return all of the items that share the maximum count (there can be more than one and I don't want to set a limit). So before I had code like:
var maxItems = items.MaxElementsBy(item => item.Value);
So now it looks like the code should be like the following to achieve the same results:
var maxItems = items.PartialSortBy(int.MaxValue, (item) => item.Value);
I switched to this package from MoreLinq as I agree with the idea of deprecating Linq methods that the common library adds. But in this case the base method has different functionality (MaxBy
only returns one item). PartialSortBy
with int.MaxValue
was not intuitive to figure out but if it works the same I guess I will use it
For each current method, add an equivalent method for IAsyncEnumerable
s.
Progress:
For example:
Lines 141 to 146 in f52cca2
These methods are not listed as obsolete in the sync versions; should there be backlog items for them?
Figure out a way to add a reduced (or zero) -allocation version of Batch
and Window
(and other buffer calls). Possibly by receiving an array or by allocating only a single array and passing it as a ReadOnlySpan<>
to a projection method.
List<T>
has some useful finding methods that should be implemented for IEnumerable<T>
IndexOf<T>(this IEnumerable<T> source, T value[, Index index])
LastIndex<T>(this IEnumerable<T> source, T value[, Index index])
FindIndex<T>(this IEnumerable<T> source, [Index index,] Func<T, bool> predicate)
FindLastIndex<T>(this IEnumerable<T> source, [Index index,] Func<T, bool> predicate)
(Last)IndexOf
to proxy to Find(Last)Index
Allow slicing an I(Async)Enumerable by a System.Range
(see morelinq/MoreLINQ#748); ElementAt/Take implemented in .NET 6, but backport for IEnumerable
A review of all current unit tests should be taken to confirm that TestingSequence
is being used everywhere.
I was using GetShortestPath
for a certain online Christmas challenge, but it was returning unstable results. After switching to QuikGraph, my code worked great. Either I am using GetShortestPath
wrong, or it has a bug. Could you help looking into it?
Here is an xUnit test demonstrating the error. It constructs a graph, shuffles the outgoing nodes, and computes the shortest distance from AA to UM. It can easily be seen that going through QT, the shortest distance is 3. However, it yields distances such as 6, 9 and 14 (and also 3).
using QuikGraph;
using QuikGraph.Algorithms;
using SuperLinq;
using Xunit;
namespace AdventOfCode.Tests;
public class GraphTests
{
class Valve
{
public string Name { get; init; }
public Valve[] Next { get; set; }
}
[Fact]
public void Test()
{
var data = """
EJ => MC
WC => OW, RU
NP => VR, KL
AA => QT, AP, EZ, AK, XV
VO => KM, RF, HS, LJ, IA
CB => UI, UP
TE => JT
CZ => UP, OW
LJ => DV, VO
UP => SK, CB, CZ
FP => OW, RE
KM => SE, VO
DV => LJ, UM
FL => AH, TS
VR => DM, TF, NP
IA => VS, VO
RF => VO, JF
RT => UM, SE
RU => AR, WC
SE => GU, KM, CX, RT
MC => EJ, AR
TF => AH, VR
CX => SE, TO
GL => UY, KL, CY
GU => SE, EZ
VS => XN, IA
EZ => AA, GU
GK => FI, HZ
JT => TE, XN
DM => VR, HZ
AR => UI, RU, MC
XN => XP, JT, VS, GT, CY
CY => XN, GL
QT => UM, AA
KL => GL, NP
SK => XV, UP
OW => CZ, WC, FP
AK => AA, HS
XV => AA, SK
GT => XN, UM
FI => JF, GK
UY => JF, GL
UM => DV, GT, RT, QT
IQ => HZ, AH
JF => RF, FI, UY, RE, TS
TS => JF, FL
AH => IQ, FL, TF
HS => AK, VO
HZ => IQ, DM, GK
TO => CX
XP => AP, XN
AP => XP, AA
RE => JF, FP
UI => AR, CB
""";
var lines = data.Trim().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
var nodes = new Dictionary<string, Valve>();
// Create vertices
foreach (var line in lines)
{
var name = line.Split(" => ")[0];
nodes.Add(name, new Valve { Name = name });
}
// Create edges
foreach (var line in lines)
{
var name = line.Split(" => ")[0];
var outNodesNames = line.Split(" => ")[1].Split(", ");
var outNodes = outNodesNames.Select(outName => nodes[outName]).ToArray();
nodes[name].Next = outNodes;
}
// Compute shortest path a few times
for (var i = 0; i < 1000; i++)
{
foreach (var node in nodes.Values)
{
node.Next = node.Next.RandomSubset(node.Next.Length).ToArray();
}
// Expect AA -> QT -> UM
Assert.Equal(3, SuperEnumerable.GetShortestPath<Valve, int>(
nodes["AA"],
(v, c) => v.Next.Select(n => (n, 1)),
nodes["UM"]
).Count());
}
}
}
Benchmarking shows that current code can be improved by up to 50% with separate versions for 2/3/4 inputs
See #156
EquiZip
was removed as being redundant to Enumerable.Zip
; however, Enumerable.Zip
has behavior matching ZipShortest
, not EquiZip
. EquiZip
should be restored as an operator.
ZipShortest
should be reviewed further as an independent operator or proxy function for corelib Enumerable.Zip
I created a PR (#70) about simple overload for ToArray()
and ToList()
with lenght
parameter. With lenght
we can avoid multiple creations of array/list when rewriting IEnumerable to these collections.
Reference System.Interactive and remove duplicate methods
It would be nice to have another overload of GetShortestPath
/GetShortestPathCost
that receives a Func<TState, bool> predicate
instead of a TState end
for identifying the end position.
Credit to https://thomaslevesque.com/2019/11/18/using-foreach-with-index-in-c/
It's a small convenience, but I thought I'd suggest adding .WithIndex()
to this lib in case you like it.
using System.Linq;
...
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
{
return source.Select((item, index) => (item, index));
}
foreach (var (item, index) in collection.WithIndex())
{
DoSomething(item, index);
}
Benchmarking shows that current code can be improved by up to 50% with separate versions for 2/3/4 inputs
Newbie question...
I've recently started to work with IAsyncEnumerable<T>
, and found the lack of ToListAsync
, SelectAsync
, FirstOrDefaultAsync
etc. in official .NET 6 a little surprising.
I then looked into System.Linq.Async
via https://github.com/dotnet/reactive and found it quite impenetrable, and ended up writing my own, simple ToListAsync
method to satisfy my immediate, simple needs.
Mark Heath's excellent article then helped me understand System.Linq.Async
a lot better, so I might use that package next time. But I also saw a link to this repo, which initially looked promising re. providing async versions of familiar LINQ methods. And there's documentation!
But... the docs don't include ToListAsync
, SelectAsync
, FirstOrDefaultAsync
etc. How could this be?
Then I found...
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
:-)
I wonder if this reliance on https://github.com/dotnet/reactive is a good thing... Perhaps it's just a temporary thing(?) It seems to me that we are now reliant on another package, with its own issues (bugs, lack of docs).
In the fullness of time it might be nice if SuperLinq
was a one-stop shop, in charge of its own destiny, without the reactive
dependency.
If the reactive
dependency is likely to continue for a while, should this be signposted on the front page of the repo?
This will require selectively hiding methods with Index
/Range
parameters
See (morelinq/MoreLINQ#827)
In order to improve allocations (ex: ArrayPool.Rent()
) or to copy data to a specific pre-existing place, it may be useful to copy a sequence. Following APIs to be implemented:
void CopyTo<T>(this IEnumerable<T> source, Span<T> span);
void CopyTo<T>(this IEnumerable<T> source, IList<T> list);
void CopyTo<T>(this IEnumerable<T> source, IList<T> list, Index index);
Semantics:
void CopyTo<T>(this IEnumerable<T> source, IList<T> list);
will throw an exception if the source
is longer than array
. If source
is shorter than array
, then the remainder of array
will be untouched.Also, you might want to backlog using a common generator to generate sync and async versions of the same method, instead of needing to copy the code over.
Originally posted by @Arithmomaniac in #2 (comment)
If, during the processing of traversing, we run out places to go, we should throw an exception instead of returning the last path that we got to.
The operator name .Scan()
is used by System.Interactive(.Async), however behavior is different than expected. Original behavior should be restored under .ScanEx()
according to commonly expected behavior of .Scan()
.
For other notes, see:
There is a difference between EnumerableEx.Repeat()
and MoreEnumerable.Repeat()
. The MoreEnumerable version should be restored.
Add a new version of Lead/Lag that return IEnumerable<(TElement current, TElement? lead/lag)>
for easier code reading of consumers.
See #156
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.