Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I hate it when I am too clever for my own good...

I hate it when I am too clever for my own good...

Scheduled Pinned Locked Moved The Lounge
csharpdatabase
39 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #1

    So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    P L Kornfeld Eliyahu PeterK B Mike HankeyM 17 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Part of this EnumOperators[^] might be of use, and I think I saw someone else with something similar. But, yes, 'twould be nice if there were an INumeric interface... maybe in the next framework.

      This space intentionally left blank.

      OriginalGriffO 1 Reply Last reply
      0
      • P PIEBALDconsult

        Part of this EnumOperators[^] might be of use, and I think I saw someone else with something similar. But, yes, 'twould be nice if there were an INumeric interface... maybe in the next framework.

        This space intentionally left blank.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        :laugh: I was making it generic to make my life easier in future! (And I hate runtime type checking) Thanks for that though, even if I'm not going to use it (I've already changed it back to fixed datatype)

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Ah yes, the good old "you can't do math with generics". There are some little libraries that allow you do it, implemented like a bunch of these:

          static T Add(T a, T b)
          {
          if (typeof(T) == typeof(int))
          return (T)(object)((int)(object)a + (int)(object)b);
          else if .. etc
          }

          which makes some people angry, but it works. Btw, that's my favourite test code for decompilers. That weird double cast is often decompiled incorrectly. However, I'm going to have to file this all under YAGNI. Apart from highly exceptional circumstances, there are only two types you're ever going to put into your MovingAverage: int and double.

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            Ah yes, the good old "you can't do math with generics". There are some little libraries that allow you do it, implemented like a bunch of these:

            static T Add(T a, T b)
            {
            if (typeof(T) == typeof(int))
            return (T)(object)((int)(object)a + (int)(object)b);
            else if .. etc
            }

            which makes some people angry, but it works. Btw, that's my favourite test code for decompilers. That weird double cast is often decompiled incorrectly. However, I'm going to have to file this all under YAGNI. Apart from highly exceptional circumstances, there are only two types you're ever going to put into your MovingAverage: int and double.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You are probably right - chances are I'll never use anything except a series of int, returning a decimal. But it's the principle, damnit! :laugh: I like generics where I *might* reuse them, and feel slightly dirty hardcoding datatypes... ;)

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              :laugh: I was making it generic to make my life easier in future! (And I hate runtime type checking) Thanks for that though, even if I'm not going to use it (I've already changed it back to fixed datatype)

              Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Oh, and there's this: A Simple Moving Average Algorithm[^]

              This space intentionally left blank.

              OriginalGriffO 1 Reply Last reply
              0
              • P PIEBALDconsult

                Oh, and there's this: A Simple Moving Average Algorithm[^]

                This space intentionally left blank.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                A lot like mine:

                public class MovingAverage
                    {
                    private List<int> samples = new List<int>();
                    private int total = 0;
                
                    public int SampleSize { get; private set; }
                    public decimal Value
                        {
                        get 
                            {
                            return samples.Count == SampleSize? (decimal)total / SampleSize : decimal.MinValue;
                            }
                        }
                    public MovingAverage(int sampleSize = -1)
                        {
                        SampleSize = sampleSize;
                        }
                    /// <summary>
                    /// Add a new sample to the average
                    /// </summary>
                    /// <param name="value"></param>
                    public void Add(int value)
                        {
                        if (SampleSize > 0)
                            {
                            while (samples.Count >= SampleSize)
                                {
                                int remove = samples\[0\]; 
                                samples.RemoveAt(0);
                                total -= remove;
                                }
                            }
                        samples.Add(value);
                        total += value;
                        }
                    }
                

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

                  Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                  Kornfeld Eliyahu PeterK Offline
                  Kornfeld Eliyahu PeterK Offline
                  Kornfeld Eliyahu Peter
                  wrote on last edited by
                  #8

                  OriginalGriff wrote:

                  I don't fancy doing that in SQL

                  Why? SQL is extremely good in arithmetic...

                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                  "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                  P 1 Reply Last reply
                  0
                  • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                    OriginalGriff wrote:

                    I don't fancy doing that in SQL

                    Why? SQL is extremely good in arithmetic...

                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    SQL is neither moving, nor average.

                    This space intentionally left blank.

                    Kornfeld Eliyahu PeterK 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

                      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                      B Offline
                      B Offline
                      Brisingr Aerowing
                      wrote on last edited by
                      #10

                      Sounds like article fodder to me.

                      <voice type="Ebeneezer Scrooge"> Bah. dumb bugs </voice>

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        SQL is neither moving, nor average.

                        This space intentionally left blank.

                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu Peter
                        wrote on last edited by
                        #11

                        SQL has top(n) and order by for moving and avg(column) for average...

                        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                        P 1 Reply Last reply
                        0
                        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                          SQL has top(n) and order by for moving and avg(column) for average...

                          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          Maybe I should have included the joke icon. But also, is TOP part of the SQL standard?

                          This space intentionally left blank.

                          Kornfeld Eliyahu PeterK 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Maybe I should have included the joke icon. But also, is TOP part of the SQL standard?

                            This space intentionally left blank.

                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu Peter
                            wrote on last edited by
                            #13

                            TOP as is not part of the standard... There is, however implementations in every SQL I know of... And of course - just for the fun - it's different in each an every of them... DB2 - select * from table fetch first 10 rows only MSSQL - select top(10) * from table MySQL - select * from table limit 10

                            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                            J 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

                              Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                              Mike HankeyM Offline
                              Mike HankeyM Offline
                              Mike Hankey
                              wrote on last edited by
                              #14

                              I like to call those little divrersions "Flights of fancy" or "Magic Mike moments" :)

                              My site: Everything Embedded Relax...We're all crazy it's not a competition!

                              1 Reply Last reply
                              0
                              • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                TOP as is not part of the standard... There is, however implementations in every SQL I know of... And of course - just for the fun - it's different in each an every of them... DB2 - select * from table fetch first 10 rows only MSSQL - select top(10) * from table MySQL - select * from table limit 10

                                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                J Offline
                                J Offline
                                Jorgen Andersson
                                wrote on last edited by
                                #15

                                How about oracle? Without using a subquery that is. :)

                                Wrong is evil and must be defeated. - Jeff Ello[^]

                                Kornfeld Eliyahu PeterK 1 Reply Last reply
                                0
                                • J Jorgen Andersson

                                  How about oracle? Without using a subquery that is. :)

                                  Wrong is evil and must be defeated. - Jeff Ello[^]

                                  Kornfeld Eliyahu PeterK Offline
                                  Kornfeld Eliyahu PeterK Offline
                                  Kornfeld Eliyahu Peter
                                  wrote on last edited by
                                  #16

                                  I don't know Oracle from my experience but a short Googleing shows that it has it's own syntax - as expected... Oracle - select * from table where rownum <= n

                                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                  "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                  J 1 Reply Last reply
                                  0
                                  • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                    I don't know Oracle from my experience but a short Googleing shows that it has it's own syntax - as expected... Oracle - select * from table where rownum <= n

                                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                    J Offline
                                    J Offline
                                    Jorgen Andersson
                                    wrote on last edited by
                                    #17

                                    Can't do that. Rownum is assigned in the beginning before ORDER BY. The result would be n random rows. Think of it as being processed in this order: 1. The FROM/WHERE clause goes first. 2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause. 3. SELECT is applied. 4. GROUP BY is applied. 5. HAVING is applied. 6. ORDER BY is applied.

                                    Wrong is evil and must be defeated. - Jeff Ello[^]

                                    Kornfeld Eliyahu PeterK 1 Reply Last reply
                                    0
                                    • J Jorgen Andersson

                                      Can't do that. Rownum is assigned in the beginning before ORDER BY. The result would be n random rows. Think of it as being processed in this order: 1. The FROM/WHERE clause goes first. 2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause. 3. SELECT is applied. 4. GROUP BY is applied. 5. HAVING is applied. 6. ORDER BY is applied.

                                      Wrong is evil and must be defeated. - Jeff Ello[^]

                                      Kornfeld Eliyahu PeterK Offline
                                      Kornfeld Eliyahu PeterK Offline
                                      Kornfeld Eliyahu Peter
                                      wrote on last edited by
                                      #18

                                      As I told I do not know Oracle form my own experience - found that bit in Google...

                                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                      J 1 Reply Last reply
                                      0
                                      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                        As I told I do not know Oracle form my own experience - found that bit in Google...

                                        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                        J Offline
                                        J Offline
                                        Jorgen Andersson
                                        wrote on last edited by
                                        #19

                                        It's just one of my pet peeves, that Oracle lacks a limit clause. It's easy enough to fix with a subquery, I just believe I shouldn't need to.

                                        Wrong is evil and must be defeated. - Jeff Ello[^]

                                        1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          So, I'm doing some analysis code, and I need a couple of moving averages - one over the whole sample, one over the last 30 samples, and one over the last ten samples. Now, I don't fancy doing that in SQL, so I'm doing it in C#, and I decide the obvious thing to do is create a MovingAverage class that you Add samples to, and it sorts itself out. Easy peasy. So I knock up the class framework, and the code that will use it, and then go back to fill in the class. And decide to make it generic because hey, I might want to use it again. Change everything to use generics - easy - and off we go...except...you can't sum generics, because they are based on object which doesn't implement arithmetic operators. And you can't restrict generics to classes that support arithmetic either... So either I restrict it to just primitive types (int, double, blah blah blah) or I drop the whole idea...and you can't use primitive types as generic constraints...and it wouldn't work if you could, because primitive arithmetic is implemented via static inline functions at compile time, so you couldn't use 'em in a generic if you wanted to! So...change it back Griff, change it all back... :doh:

                                          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                                          A Offline
                                          A Offline
                                          Andy Brummer
                                          wrote on last edited by
                                          #20

                                          Since this borders on a programming answer, I'm not going to give you the answer directly, but look at they way the two parameter sum function is implemented.

                                          Curvature of the Mind now with 3D

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups