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 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
                • 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 – ∞)

                  T Offline
                  T Offline
                  TnTinMn
                  wrote on last edited by
                  #21

                  Quitter. You could have done it with some creativity using Linq.Expressions.

                  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
                    BillWoodruff
                    wrote on last edited by
                    #22

                    I like it ... when you are too clever for your own good. Me learn stuff.

                    “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

                    OriginalGriffO 1 Reply Last reply
                    0
                    • B BillWoodruff

                      I like it ... when you are too clever for your own good. Me learn stuff.

                      “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

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

                      That's the day not wasted then! :laugh:

                      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 – ∞)

                        C Offline
                        C Offline
                        CBadger
                        wrote on last edited by
                        #24

                        I had a similar problem once. (But I was more stubborn keeping it Generic) :rolleyes: So what i ended up doing was keeping with the object types, using that to filter each value and tryparse into decimal List class so that I could sum up everything. I just had to prepare for the worse so I put in my catch that if any error would occur to throw out the value and just log what went wrong, so that way when I picked up more errors (Which I did not much), i could just improve the tryparse :doh:

                        Loading signature... . . . Please Wait . . .

                        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 – ∞)

                          F Offline
                          F Offline
                          Fueled By Decaff
                          wrote on last edited by
                          #25

                          You could do it by using a class that does the adding. You would need to specify the class to do the adding and the type it uses in the moving averages class. Something like this:

                          public interface IArithmetic<T>
                          {
                          T Add(T augend, T addend);
                          }

                          public class GenericAdd<T, U> where T : IArithmetic<U>, new()
                          {
                          T adder = new T();

                          public U AddGenericTypes(U augend, U addend)
                          {
                              return adder.Add(augend, addend);
                          }
                          

                          }

                          And for each type you want to use this with you will need one of these.

                          public class AddInt : IArithmetic<int>
                          {
                          public AddInt()
                          {
                          }

                          #region IArithmetic<int> Members
                          
                          public int Add(int augend, int addend)
                          {
                              return augend + addend;
                          }
                          
                          #endregion
                          

                          }

                          These can then be used like this:

                          GenericAdd<AddInt, int> intAdder = new GenericAdd<AddInt, int>();
                          int result = intAdder.AddGenericTypes(1, 2);

                          Seems like a lot of work to me unless you know you are going to need it.

                          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 – ∞)

                            I Offline
                            I Offline
                            irneb
                            wrote on last edited by
                            #26

                            You could of course go the fully OO route and make your own "number" class. I.e. the way you'd have done it prior to generics. Then add some implied conversion overloads so you don't need to manually type-cast your int/double/decimal/etc. You could then even accommodate other more complicated data types (e.g. to generate moving averages on a candle-stick-chart with 3 values per item: open, avg, close). BTW, for the data sample I'd go with either a double linked list (LinkedList with max length) or a circular array, not an Array List as you've done in your sample code (that's implemented as a flat array so a remove from index 0 means it shifts all samples down by 1, if you reverse the order then each insert would shift all items up by 1). Seeing as mostly you'd calculate by iterating over each item in the "list" and you'd not want to move all the samples in the array each time you get a new one the linked list should suffice for this purpose. The circular array I'd only use if I know the size sample will not change after initially creating the object and if I need to reference specific items by index. You could use a Queue type for this, as well. I "think" it's implemented as a linked list anyway. Here's what I'm thinking:

                            public class MyNumber {
                            	private object \_value;
                            	public object Value {
                            		get { return \_value; }
                            		set {
                            			if ((value is int) || (value is double) || (value is decimal))
                            				\_value = value;
                            			else throw new InvalidCastException();
                            		}
                            	}
                            	
                            	public MyNumber(object val) {
                            		Value = val;
                            	}
                            	
                            	public static implicit operator MyNumber(int val) { return new MyNumber(val); }
                            	
                            	public static implicit operator int(MyNumber val) { return (int)val.\_value; }
                            	
                            	public static implicit operator MyNumber(double val) { return new MyNumber(val); }
                            	
                            	public static implicit operator double(MyNumber val) { return (double)val.\_value; }
                            	
                            	public static implicit operator MyNumber(decimal val) { return new MyNumber(val); }
                            	
                            	public static implicit operator decimal(MyNumber val) { return (decimal)val.\_value; }
                            }
                            
                            public class MovingAverage {
                            	private LinkedList \_samples = new LinkedList();
                            	private decimal total = 0;
                            	public int SampleSize { get; private set; }
                            	public decimal Value {
                            		get {
                            			if (SampleSize < 0) return (\_samples.Count > 0) ? total / \_samples.Count : 0;
                            			return (\_samples.Count >= SampleSize) ? tota
                            
                            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 – ∞)

                              G Offline
                              G Offline
                              Gary Wheeler
                              wrote on last edited by
                              #27

                              'Griff, I don't know if anyone else has said this, but: source control is your friend. Check-in early, check-in often.

                              Software Zen: delete this;

                              OriginalGriffO 1 Reply Last reply
                              0
                              • G Gary Wheeler

                                'Griff, I don't know if anyone else has said this, but: source control is your friend. Check-in early, check-in often.

                                Software Zen: delete this;

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

                                :laugh: I don't like to check in "partial" or non-working code. That's why I have hourly incremental backups on my PC - so I can only lose 60 minutes work (in theory - practice shows that it's normally quicker to regenerate than restore for an hour or so ago)

                                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

                                G 1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  :laugh: I don't like to check in "partial" or non-working code. That's why I have hourly incremental backups on my PC - so I can only lose 60 minutes work (in theory - practice shows that it's normally quicker to regenerate than restore for an hour or so ago)

                                  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 – ∞)

                                  G Offline
                                  G Offline
                                  Gary Wheeler
                                  wrote on last edited by
                                  #29

                                  Everyone has their own workflow that's best for them. That said, a lot of my team members make copies of working files, rather than use source control as a tool. I work with one guy who tends to keep everything checked out, and only checks in prior to doing a build for distribution. It drives me nuts, because he'll have days or even weeks worth of work that exists only on his machine (our source control data base gets backed up daily). I'm tempted to power off his machine and substitute a bad hard drive to teach him a lesson :( . My approach, at least in our development branches, is to work incrementally and check-in each successful increment. Most of the time I make the feature I'm working on disabled/invisible/inactive in release builds, until I think it's ready. This makes backtracking, which I do a lot more than I'd like to admit, simpler. Diff's are handy too when I've led myself down the proverbial garden path :sigh:.

                                  Software Zen: delete this;

                                  OriginalGriffO 1 Reply Last reply
                                  0
                                  • G Gary Wheeler

                                    Everyone has their own workflow that's best for them. That said, a lot of my team members make copies of working files, rather than use source control as a tool. I work with one guy who tends to keep everything checked out, and only checks in prior to doing a build for distribution. It drives me nuts, because he'll have days or even weeks worth of work that exists only on his machine (our source control data base gets backed up daily). I'm tempted to power off his machine and substitute a bad hard drive to teach him a lesson :( . My approach, at least in our development branches, is to work incrementally and check-in each successful increment. Most of the time I make the feature I'm working on disabled/invisible/inactive in release builds, until I think it's ready. This makes backtracking, which I do a lot more than I'd like to admit, simpler. Diff's are handy too when I've led myself down the proverbial garden path :sigh:.

                                    Software Zen: delete this;

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

                                    Gary Wheeler wrote:

                                    Everyone has their own workflow that's best for them.

                                    Very true. It's a fine balance, I think: I don't like a checked in version that won't run, or throws continual NotImplementedExceptions, and I try to avoid thinking of version control as a backup system! :laugh: Instead I have my NAS and automatic incremental backups that I don't even see, much less think about. I can understand both you and your team member's position: he doesn't want to issue code that he isn't sure is final in case it gets used and has to become final by default. But you want it backed up so weeks can't be lost... Perhaps a "proper" backup system for your PCs to work alongside the SVN/GIT route would be useful? I know damn well I wouldn't want to have to rebuild my current PC without good backups - it would take me days to get the software all back on and configured! :-D

                                    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

                                    G S 2 Replies Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      Gary Wheeler wrote:

                                      Everyone has their own workflow that's best for them.

                                      Very true. It's a fine balance, I think: I don't like a checked in version that won't run, or throws continual NotImplementedExceptions, and I try to avoid thinking of version control as a backup system! :laugh: Instead I have my NAS and automatic incremental backups that I don't even see, much less think about. I can understand both you and your team member's position: he doesn't want to issue code that he isn't sure is final in case it gets used and has to become final by default. But you want it backed up so weeks can't be lost... Perhaps a "proper" backup system for your PCs to work alongside the SVN/GIT route would be useful? I know damn well I wouldn't want to have to rebuild my current PC without good backups - it would take me days to get the software all back on and configured! :-D

                                      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 – ∞)

                                      G Offline
                                      G Offline
                                      Gary Wheeler
                                      wrote on last edited by
                                      #31

                                      We do have a backup system available; it requires some individual initiative to use it. Some do, some don't. Based on prior experience, our herd of cats doesn't work well with an automated backup mechanism.

                                      OriginalGriff wrote:

                                      SVN/GIT

                                      If only. We're still using SourceSafe, believe it or not. One of my 'spare time' projects (yeah, right) is to get our source control moved to something else this year. Current candidates are MS Team Foundation and SourceGear Vault. My uninformed impression is that Git wouldn't work for us, based on its apparent complexity.

                                      Software Zen: delete this;

                                      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 – ∞)

                                        J Offline
                                        J Offline
                                        James Curran
                                        wrote on last edited by
                                        #32

                                        There a issue on Connect to add IArithmetic interface to the value types, but it seems to be going nowhere. https://connect.microsoft.com/VisualStudio/feedback/details/94264/arithmetic-types-like-int-double-decimal-should-implement-iarithmetic-t (NOTE: for some reason, going to that link gives an error -- Other issues are displaying OK)

                                        Truth, James

                                        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 – ∞)

                                          Richard DeemingR Offline
                                          Richard DeemingR Offline
                                          Richard Deeming
                                          wrote on last edited by
                                          #33

                                          You can use the System.Linq.Expressions namespace to create generic operators quite easily. For example, see Marc Gravell's Generic Operators[^] from the MiscUtil library[^].

                                          public static class GenericOperator<T>
                                          {
                                          private static Func<T, T, TResult> CreateCore<TResult>(Func<Expression, Expression, BinaryExpression> body)
                                          {
                                          try
                                          {
                                          Type typeT = typeof(T);
                                          var left = Expression.Parameter(typeT, "left");
                                          var right = Expression.Parameter(typeT, "right");

                                                  if (typeT.IsEnum)
                                                  {
                                                      Type enumType = Enum.GetUnderlyingType(typeT);
                                                      var x = Expression.Convert(left, enumType);
                                                      var y = Expression.Convert(right, enumType);
                                          
                                                      Expression op = body(x, y);
                                                      if (op.Type == enumType) op = Expression.Convert(op, typeT);
                                          
                                                      return Expression.Lambda<Func<T, T, TResult>>(op, left, right).Compile();
                                                  }
                                          
                                                  return Expression.Lambda<Func<T, T, TResult>>(body(left, right), left, right).Compile();
                                              }
                                              catch (InvalidOperationException ex)
                                              {
                                                  string message = ex.Message;
                                                  return delegate { throw new InvalidOperationException(message); };
                                              }
                                              catch (ArgumentException ex)
                                              {
                                                  string message = ex.Message;
                                                  return delegate { throw new InvalidOperationException(message); };
                                              }
                                          }
                                          
                                          private static Lazy<Func<T, T, TResult>> Create<TResult>(Func<Expression, Expression, BinaryExpression> body)
                                          {
                                              return new Lazy<Func<T, T, TResult>>(() => CreateCore<TResult>(body), true);
                                          }
                                          
                                          private static readonly Lazy<Func<T, T, T>> \_add = Create<T>(Expression.Add);
                                          
                                          public static Func<T, T, T> Add
                                          {
                                              get { return \_add.Value; }
                                          }
                                          
                                          ...
                                          

                                          }


                                          "These people looked deep within my

                                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                          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