Skip to content

Commit

Permalink
+ extension method IEnumerable<IEnumerable<>>.Flatten2() to simplif…
Browse files Browse the repository at this point in the history
…y `.SelectMany(i => i)`, the other extension method with name `Flatten()` already taken by language-ext https://github.com/louthy/language-ext/blob/2328e0732b3579f29ae3c52c570760fdeae8e9d2/LanguageExt.Core/Immutable%20Collections/Arr/Arr.Extensions.cs#L19 @ ExtensionMethods.cs

@ c#/shared
  • Loading branch information
n0099 committed May 18, 2024
1 parent fa16951 commit 359969f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion c#/crawler/src/Worker/ForumModeratorRevisionCrawlWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where e.IsCrawling
?.GetAttribute("href")?.Split("/home/main?id=")[1].NullIfEmpty())
.OfType<string>();
return memberPortraits.Select(portrait => (type, portrait));
}).SelectMany(i => i);
}).Flatten2();
}

private async Task Save(
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/Consumer/HashConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static byte[] GetThumbHashForMatrix(Mat mat)
return rgbaMat.GetArray(out Vec4b[] pixels)
? ThumbHashes.Utilities.RgbaToThumbHash(mat.Width, mat.Height, pixels
.Select(vec => new[] {vec.Item0, vec.Item1, vec.Item2, vec.Item3})
.SelectMany(i => i).ToArray())
.Flatten2().ToArray())
: throw new InvalidOperationException("Failed to convert matrix into byte array.");
}
};
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/Consumer/QrCodeConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public QrCodeConsumer(IConfiguration config, FailedImageHandler failedImageHandl
imageKeyWithMatrix => imageKeyWithMatrix.ImageId,
ScanQrCodeInImage)
.ToList();
db.ImageQrCodes.AddRange(imageQrCodeEithers.Rights().SelectMany(i => i));
db.ImageQrCodes.AddRange(imageQrCodeEithers.Rights().Flatten2());
return imageQrCodeEithers.Lefts();
}

Expand Down
4 changes: 2 additions & 2 deletions c#/imagePipeline/src/ImageBatchConsumingWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void MarkImagesInReplyAsConsumed
.Where(i => i.ImageInReply is not {HashConsumed: true, QrCodeConsumed: true, OcrConsumed: true}),
imageWithBytes => imageWithBytes.ImageInReply.ImageId,
DecodeImageOrFramesBytes(stoppingToken))
.Rights().SelectMany(i => i).ToList();
.Rights().Flatten2().ToList();
try
{
IReadOnlyCollection<ImageKeyWithMatrix> ExceptConsumed
Expand Down Expand Up @@ -200,7 +200,7 @@ ImageKeyWithMatrix DecodeFrame(ImageFrame<Rgb24> frame, int frameIndex)
var scriptGroupings = await forumScripts.AsNoTracking()
.GroupBy(e => e.Fid, e => e.Script)
.ToListAsync(stoppingToken);
var scripts = scriptGroupings.SelectMany(i => i).Distinct().ToList();
var scripts = scriptGroupings.Flatten2().Distinct().ToList();
var recognizedTextLinesKeyByScript = new Dictionary<string, List<ImageOcrLine>>(scripts.Count);
scripts.ForEach(script => recognizedTextLinesKeyByScript[script] = []);

Expand Down
4 changes: 2 additions & 2 deletions c#/imagePipeline/src/Ocr/JointRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class JointRecognizer(
var detectedEithers = _paddleOcrProvider
.DetectMatrices(matricesKeyByImageKey, failedImageHandler, stoppingToken).ToList();
var recognizedResultsViaPaddleOcr =
recognizedEithersViaPaddleOcr.Rights().SelectMany(i => i).ToList();
recognizedEithersViaPaddleOcr.Rights().Flatten2().ToList();
var recognizedEithersViaTesseract = RecognizeMatricesViaTesseract(
recognizedResultsViaPaddleOcr,
detectedEithers.Rights().SelectMany(i => i).ToList(),
detectedEithers.Rights().Flatten2().ToList(),
matricesKeyByImageKey,
stoppingToken)
.ToList();
Expand Down
3 changes: 3 additions & 0 deletions c#/shared/src/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static void AddRange<T>(this ICollection<T> list, IEnumerable<T> items)
this IEnumerable<KeyValuePair<TKey, TValue>> first,
IEnumerable<TKey> second) =>
first.IntersectBy(second, pair => pair.Key);

public static IEnumerable<T> Flatten2<T>(this IEnumerable<IEnumerable<T>> source) =>
source.SelectMany(i => i);
}
public static partial class ExtensionMethods
{
Expand Down

0 comments on commit 359969f

Please sign in to comment.