<small id='HZ80h'></small><noframes id='HZ80h'>

      <tfoot id='HZ80h'></tfoot>

      • <bdo id='HZ80h'></bdo><ul id='HZ80h'></ul>

        <legend id='HZ80h'><style id='HZ80h'><dir id='HZ80h'><q id='HZ80h'></q></dir></style></legend>
        <i id='HZ80h'><tr id='HZ80h'><dt id='HZ80h'><q id='HZ80h'><span id='HZ80h'><b id='HZ80h'><form id='HZ80h'><ins id='HZ80h'></ins><ul id='HZ80h'></ul><sub id='HZ80h'></sub></form><legend id='HZ80h'></legend><bdo id='HZ80h'><pre id='HZ80h'><center id='HZ80h'></center></pre></bdo></b><th id='HZ80h'></th></span></q></dt></tr></i><div id='HZ80h'><tfoot id='HZ80h'></tfoot><dl id='HZ80h'><fieldset id='HZ80h'></fieldset></dl></div>

        如何用自己压缩一个 IEnumerable

        How to Zip one IEnumerable with itself(如何用自己压缩一个 IEnumerable)
            <bdo id='7CGtI'></bdo><ul id='7CGtI'></ul>
            <tfoot id='7CGtI'></tfoot>

            <small id='7CGtI'></small><noframes id='7CGtI'>

                <tbody id='7CGtI'></tbody>

              <i id='7CGtI'><tr id='7CGtI'><dt id='7CGtI'><q id='7CGtI'><span id='7CGtI'><b id='7CGtI'><form id='7CGtI'><ins id='7CGtI'></ins><ul id='7CGtI'></ul><sub id='7CGtI'></sub></form><legend id='7CGtI'></legend><bdo id='7CGtI'><pre id='7CGtI'><center id='7CGtI'></center></pre></bdo></b><th id='7CGtI'></th></span></q></dt></tr></i><div id='7CGtI'><tfoot id='7CGtI'></tfoot><dl id='7CGtI'><fieldset id='7CGtI'></fieldset></dl></div>

                <legend id='7CGtI'><style id='7CGtI'><dir id='7CGtI'><q id='7CGtI'></q></dir></style></legend>
                  本文介绍了如何用自己压缩一个 IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am implementing some math algorithms based on lists of points, like Distance, Area, Centroid, etc. Just like in this post: Find the distance required to navigate a list of points using linq

                  That post describes how to calculate the total distance of a sequence of points (taken in order) by essentially zipping the sequence "with itself", generating the sequence for Zip by offsetting the start position of the original IEnumerable by 1.

                  So, given the Zip extension in .Net 4.0, assuming Point for the point type, and a reasonable Distance formula, you can make calls like this to generate a sequence of distances from one point to the next and then to sum the distances:

                  var distances = points.Zip(points.Skip(1),Distance);
                  double totalDistance = distances.Sum();
                  

                  Area and Centroid calculations are similar in that they need to iterate over the sequence, processing each pair of points (points[i] and points[i+1]). I thought of making a generic IEnumerable extension suitable for implementing these (and possibly other) algorithms that operate over sequences, taking two items at a time (points[0] and points[1], points[1] and points[2], ..., points[n-1] and points[n] (or is it n-2 and n-1 ...) and applying a function.

                  My generic iterator would have a similar signature to Zip, but it would not receive a second sequence to zip with as it is really just going to zip with itself.

                  My first try looks like this:

                  public static IEnumerable<TResult> ZipMyself<TSequence, TResult>(this IEnumerable<TSequence> seq, Func<TSequence, TSequence, TResult> resultSelector)
                  {
                    return seq.Zip(seq.Skip(1),resultSelector);
                  }
                  

                  Begin edit: After seeing the responses, I have implemented Pairwise with explicit use of the underlying Enumerator like this:

                  public static IEnumerable<TResult> Pairwise<TSequence, TResult>(this IEnumerable<TSequence> seq, Func<TSequence, TSequence, TResult> resultSelector)
                  {
                    TSequence prev = default(TSequence);
                    using (IEnumerator<TSequence> e = seq.GetEnumerator())
                    {
                      if (e.MoveNext()) prev = e.Current;
                  
                      while (e.MoveNext()) yield return resultSelector(prev, prev = e.Current);
                    }
                  }
                  

                  While certainly more complicated than my initial version, this one iterates through the input sequence once whereas the the original iterates twice.

                  End edit

                  With my generic iterator in place, I can write functions like this:

                  public static double Length(this IEnumerable<Point> points)
                  {
                    return points.ZipMyself(Distance).Sum();
                  }
                  

                  and call it like this:

                  double d = points.Length();
                  

                  and

                  double GreensTheorem(Point p1, Point p1)
                  {
                    return p1.X * p2.Y - p1.Y * p2.X;
                  }
                  
                  public static double SignedArea(this IEnumerable<Point> points)
                  {
                    return points.ZipMyself(GreensTheorem).Sum() / 2.0
                  }
                  
                  public static double Area(this IEnumerable<Point> points)
                  {
                    return Math.Abs(points.SignedArea());
                  }
                  
                  public static bool IsClockwise(this IEnumerable<Point> points)
                  {
                    return SignedArea(points) < 0;
                  }
                  

                  and call them like this:

                  double a = points.Area();
                  bool isClockwise = points.IsClockwise();
                  

                  In this case, is there any reason NOT to implement "ZipMyself" in terms of Zip and Skip(1)? Is there already something in LINQ that automates this (zipping a list with itself) - not that it needs to be made that much easier ;-)

                  Also, is there better name for the extension that might reflect that it is a well-known pattern (if, indeed it is a well-known pattern)?

                  Had a link here for a StackOverflow question about area calculation. It is question 2432428.

                  Also had a link to Wikipedia article on Centroid. Just go to Wikipedia and search for Centroid if interested.

                  Just starting out, so don't have enough rep to post more than one link.

                  Begin edit

                  For completeness, if anyone gets here after searching for Distance, Area, or Centroid, here are my functions that accept a list of Position types (assumed closed for Area and Centroid) and return the Distance(along), Area, and Centroid of the Positions:

                  public struct Position
                  {
                    public double X;
                    public double Y;
                  
                    static public double Distance(Position p1, Position p2)
                    {
                      double dx = p2.X - p1.X;
                      double dy = p2.Y - p1.Y;
                      return Math.Sqrt(dx*dx + dy*dy);
                    }
                  }
                  
                  public static class PointMath
                  {
                    public static double Distance(IEnumerable<Position> pts)
                    {
                      return pts.Pairwise((p1, p2) => Position.Distance(p1, p2)).Sum();
                    }
                  
                    private static bool IsClockwise(IEnumerable<Position> pts)
                    {
                      return SignedArea(pts) < 0;
                    }
                  
                    private static double SignedArea(IEnumerable<Position> pts)
                    {
                      return pts.Pairwise((p1, p2) => (p1.X * p2.Y - p1.Y * p2.X)).Sum() / 2.0;
                    }
                  
                    public static double Area(IEnumerable<Position> pts)
                    {
                      return Math.Abs(SignedArea(pts));
                    }
                  
                    public static Position Centroid(IEnumerable<Position> pts)
                    {
                      double a = SignedArea(pts);
                  
                      var  c = pts.Pairwise((p1, p2) => new 
                                                        { 
                                                          x = (p1.X + p2.X) * (p1.X * p2.Y - p2.X * p1.Y), 
                                                          y = (p1.Y + p2.Y) * (p1.X * p2.Y - p2.X * p1.Y)   
                                                        })
                                  .Aggregate((t1, t2) => new 
                                                         { 
                                                           x = t1.x + t2.x, 
                                                           y = t1.y + t2.y 
                                                         });
                  
                      return new Position(1.0 / (a * 6.0) * c.x, 1.0 / (a * 6.0) * c.y);
                    }
                  }
                  

                  Feel free to comment.

                  End edit

                  解决方案

                  Also, is there better name for the extension that might reflect that it is a well-known pattern (if, indeed it is a well-known pattern)?

                  Yes - it is also known as Pairwise. It has been done before, for example here. There also has been a question about it before here on SO.

                  Pairwise can now be implemented in terms of Zip for .NET 4.0, as you point out. This seems like a reasonable approach for a LINQ to Objects solution, although having a version that works on .NET v3.5 too is probably more useful to a wider audience at this point.

                  这篇关于如何用自己压缩一个 IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

                    <bdo id='DWgw5'></bdo><ul id='DWgw5'></ul>
                    <i id='DWgw5'><tr id='DWgw5'><dt id='DWgw5'><q id='DWgw5'><span id='DWgw5'><b id='DWgw5'><form id='DWgw5'><ins id='DWgw5'></ins><ul id='DWgw5'></ul><sub id='DWgw5'></sub></form><legend id='DWgw5'></legend><bdo id='DWgw5'><pre id='DWgw5'><center id='DWgw5'></center></pre></bdo></b><th id='DWgw5'></th></span></q></dt></tr></i><div id='DWgw5'><tfoot id='DWgw5'></tfoot><dl id='DWgw5'><fieldset id='DWgw5'></fieldset></dl></div>
                        <tbody id='DWgw5'></tbody>
                      <legend id='DWgw5'><style id='DWgw5'><dir id='DWgw5'><q id='DWgw5'></q></dir></style></legend>

                        • <small id='DWgw5'></small><noframes id='DWgw5'>

                            <tfoot id='DWgw5'></tfoot>