Sidebar Menu

Projects

  • Dashboard
  • Research Project
  • Milestones
  • Repository
  • Tasks
  • Time Tracking
  • Designs
  • Forum
  • Users
  • Activities

Login

  • Login
  • Webmail
  • Admin
  • Downloads
  • Research

Twitter

Tweets by stumathews
Stuart Mathews
  • Home
  • Blog
  • Code
  • Running
  • Gaming
  • Research
  • About
    • Portfolio
    • Info
Details
Category: Blog
By Stuart Mathews
Stuart Mathews
18.Dec
17 December 2018
Last Updated: 28 December 2018
Hits: 3357

DirectX math, coursework and some films

The last couple of days have been fairly interesting. I decided to learn about Vectors and the Math behind working with them. Things like what the dot product, unit vectors are and I did a bit of cursory trigonometry. Math has become a lot more fun for me since learning about games and 3d programming. Also I learnt how to embed math equasitions in by blog:

\[ det A = \sum_{j=1}^{n} A_{ij}(-1)^{i+j} det \bar{A}_{ij} \\ \tag{Determinant of A} \\ \]

The interesting thing about learning this stuff is not that I need it to work Direct X, which I do, but more so because it's nice to know what they mean and why it works. I think that's always been by a problem: sometimes I'm more interested in the concept sometimes than actually using it. But I think it does affect a lot of people too -  only they don't spend as much time on it as I do I think.

 

I remember when one of our Australian developers went to town converting all his C++ code to use templates... I think he loved the idea of templates more than perhaps the readability or productivity that the code might afford. All because templating in C++ was interesting. I sympathise. 

Anyway, I've also started reading more up on the Software Engineering coursework I have, it actually a lot more interesting than the CPD course, probably because I'm more familiar with it. I'm hoping the CPD becomes more interesting and fun... The thing about the CPD course is that it's all just a bit fluffy, you have to reflect on your past activities and apply theory to it and its all a bit 'mushy'. My last degree was more in-your-face:  here is a database, run this python query on it and send the bytes to that file etc. much more measured and hands-on, I guess that's why I like programming and maybe why I apprciate the math I'm learning at the moment.(a bit more than Continous Professional Development!)

I hit the gym yesterday and did quite a long and extended session focusing on my legs and having run in, I decided to run back and boy was that a test of endurance. My plans to be a little more varied in my workout haven't yet come into fruition yet. 

I watched the new Aquaman Film which I actually really enjoyed. I thought that perhaps this would be a watered down superhero film because it's not a well-known character but it was good. I quite like the co-star Amber Hurd, she's beautiful. Always helps. 

I watched my favourite film, Gladiator last night and I never get used to it - its a great film. 

Anyway back to my Direct X studies, whilst reading this new book about direct X 12... I've learned about the fundamental behind Vector arithmetic, basically observing the geometric results of adding, subtracting and multiplying vectors or forces.

I also noticed that the initialization and basic start-up code I learned about in DirectX11 has been replaced with a concept of queues and lists and commands that you send and prioritise graphics instructions to the GPU. This is new.

Some interesting facts are that negating a vector flips its direction, multiplying one, extends or rather scales its magnitude in the same direction and if you add two vectors (or forces) you can see the resulting displacement of an object or just the resultant vector by parallel extending the opposite vectors(I'll look for a good picture soon).

Translation in 3d Graphics terminology merely refers to taking a vector and putting it somewhere else - the direction and magnitude of the vector remain the same. Oh, and of course, a vector is represented geometrically as [x, y, z] and if you use the coordinate system, then you can see which direction this represents.

Some of the interesting things you can do on vectors is determining the vector magnitude using a special formula(which I can't show you right now because I can't get latex to work right on my blog). No wait I can:

\[ ||u|| = \sqrt{x^2 + y^2 + z^2} \\ \tag{eq.1.1 Magnitude of vector} \\ \]

The other is normalising the vector(which creates what is called a unit vector) which can be worked-out by applying a formula which basically just reduces the magnitude of the vector to 1 but the direction is still the same but obviously if you reduce the vector's magnitude, it's x, y, z coordinates will change, and that formula works out the newly adjusted values for them .

The other interesting thing you can do with two vectors is to determine if the angle between two vectors is acute, obtuse or perpendicular. This can be done by getting the scalar product (or the dot product) which is a sort of combination of two Vectors.

The other combination is the cross-product which allows you to determine a new vector which is orthogonal to the two vectors concerned - this is also called the left-hand rule, as the left hand rule also shows you(on your hand) where the orthogonal vector will be when given the direction of the first vector(which you aim your fingers at) and then curl your fingers towards to 2nd vector and then your thumb shows you where the perpendicular vector is. Kinda cool. 

The other thing you can do with unit vectors(magnitude is 1) is that you can easily determine the angle between the vectors (but not by using the dot product as that only tells you if the angle will be perpendicular, acute or obtuse) but by using cosine if both vectors are unit vectors. The equation for doing that is

\[ uv = ||u||||v|| \cos \theta \] and ||u|| and ||v|| just mean the magnitude of the vector and as these will both be 1, you can solve for the angle which will be the angle between the two vectors. 

My mind started to have a collapse when we started going over orthogonalising vectors which is something I'll have to look into at a later stage but basically its a formula to determine perpendiculars to vectors (like the left-hand rule) but with math. And the other brain-freezer was orthogonal projection, which I'm sure is all easy but I need an alternative perspective before my brain lets these ideas in!

These are the general equasions I learnt about when using vectors:

\[ ||u|| = \sqrt{x^2 + y^2 + z^2} \\ \tag{eq.1.1 Magnitude of vector} \\ \]

\[ \hat{u} = \frac{u}{||u||} = \frac{x}{||u||} + \frac{y}{||u||} + \frac{z}{||u||} \\ \tag{eq 1.2 Unit vector} \\ \]

\[ u \cdot v = u_{x}v_{x} + u_{y}v_{y} + u_{z} + v_{z} \\ \tag{eq 1.3 dot product} \\\]

\[ u \cdot v = cos \theta  \rightarrow ||u||||v|| = 1 (unit vectors) \\ \tag{eq 1.4 angle between vectors u and v} \\ \]

\[ w = u \times v = u_yv_z - u_zv_y, u_zv_x -u_xv_z, u_xv_y - y_yv_x \\ \tag{eg 1.5 cross product of two vectors} \\  \] 

I also learned about how to store Vectors in C++/DirectX using the SIMD registers that are part of SSE2 using the XMVECTOR Structure to store 4 32-bit floating point numbers which can represent a vector's parts ie [x, y, z] and the 4th float is not used. The other key idea was that use should only use this structure if you're storing these structures globally as this ensures some alignment requirements to be enforced. When this is not the case, Ie you want to store a vector in a class, you should use the XMFLOAT2,3,4 structure to store a Vector as it holds 2,3,4 floating point numbers which can represent your vector.

The cool thing about these structures is that they are 128-bit structures and one SIMD register is 128-bit and you can do one complete SIMD operation using 128bits stored in a SIMD register which is what DirectX does with these structures that you pass in. This is for efficiency purposes. Quite interesting.

A lot of the stuff I learnt was quite interesting because the proofs were solved, not just merely stating the equations but explaining the reasons behind the equations which is the right way to do it in my opinion.

Today I read up on Matrices, which are compact forms in which you can store Vectors or any 3 or 4 valued number(such as triangle vertices!). There are also some interesting things you can do on matrices. Basically, you can add/subtract and combine matrices too. There are generic equations which allow you to do this in a compact way but I was shown how these equations were derived from their geometric interpretations which were fun.

These were the ideas behind :

  • Multiplying matrices,
  • Vector-matrix multiplication - basically combining a vector with a matrix

\[ uA = xA_{1,*} + yA_{2,*} + zA_{3,*} \\ \tag{vector matrix multimplication} \\ \]

  • Transposing of a matrix - swapping a matrices columns with its rows
  • The identity matrix - a matrix that when multiplied by a matrix does not change the matrix
  • The determinant of  a matrix - which is a number which helps determine the inverse of a matrix

\[ det A = \sum_{j=1}^{n} A_{ij}(-1)^{i+j} det \bar{A}_{ij} \\ \tag{Determinant of A} \\ \]

  • Matrix Minors - a subset of a matrix which helps find the determinant of the matrix
  • Adjoint of a matrix - also helps to find the inverse of a matrix
  • The inverse of a matrix - which is used to help find another matrix when all others are provided in an equasion (so thats why its so useful.)

Equations:  (TODO)


\[ \left[
\begin{array}{ c c }
1 & 2 \\
3 & 4
\end{array} \right]
\]

Just like with Vectors most of the concepts mentioned above can be automatically calculated using DirectX helper functions (but where is the fun in doing that!)  but I'm thankful I know what that math behind it all is. The must say the Author Frank D Luna did a great job of explaining it. 

With Matrices, you can use a XMMATRIX instead of a  XMVECTOR and again you use this only for global variables of this type. To represent matrices in classes you would represent them as XMFLOAT4x4 which allows the storage of 4x4 Floats, which is the Matrix format most used in Direct X 12

 No I'm not sure I'll remember it all, but its probably going to start making sense when we start trying to translate, rotate, scale models that themselves are represented as vertices which are 3-valued structures and thus can be stored singularly as a Vector or an array of them, or indeed an array of Matrices. 

Tomorrow I'm going to read up about transformations(translate, rotate, scale) of these structures but first I'll need to figure out how orthogonal projection works - so let me get that out of the way first.

 

 

  • Math
  • DirectX
Details
Category: Blog
By Stuart Mathews
Stuart Mathews
08.Dec
07 December 2018
Last Updated: 28 December 2018
Hits: 2906

Kim Kardashians butt and other stuff

This is mostly about other stuff and not about Kim Kardashian's butt.

I've had about 7 days off from exercise due to my coming down with a cold. I loathe it when this happens but its actually quite good to take a break from something you do so often. I think psychologically as much as physically. This has meant that I've pulled back from the gym in the mornings and missed my long run on Wednesday.  I always wonder if there was something I could have done to prevent getting sick - could I have not been eating correctly, or was it that I didn't wash my hands enough or am I not sleeping enough? Can you really prevent being ill?

This does coincide with it starting to become colder and I think there is a theory that this helps in getting ill. I've not really thought about why but there you are.

I'm also thinking that when I go back to training, that I'll switch my training program.

I'll switch from what has mainly been a heavily resistance-orientated program to a more well-rounded program. This means that I'll be focusing on a balanced set of training exercises that improve the various areas in fitness, not just strength. So this will include Flexibility, Endurance, Skill, Stamina, Strength, Speed and Power etc. My usual routine is normally doesn't include power moves, agility and speed and it's these that I'll focus on. Currently, I do 3 days of 1.5 hours of resistance exercise and 20 minutes of running and on a Saturday I do around 2h of resistance training. Usually what I do is after my main training session, is to go for a 20-minute run which sort of helps me with my endurance(and helps me get home!)

The other things that have been on my mind are finishing my next assignments for Software Engineering and Continuing Professional Development,(CPD)  both of which have deadlines in December. I've effectively had to rewrite my one assignment which was a bit of pain. The reason was that as part of the CPD I have to study a theory of learning an apply it to past CPD activities I'd done. As it turns out, I wanted to reflect on my past software development activities and projects and work etc as I thought this would be a great way to assess what I've done. Turns out, that I had far too much stuff to reflect on and the Tutor was uncomfortable accepting so much work.

So I did a whole bunch of writing for nothing in this respect. In another respect, I actually enjoyed doing it but that meant that I had to do it again but this time on a much more narrower form of CPD - My degree. That being said, I was able to reference some of the other personal projects I did so all was not lost.

I've found some time to study Understanding how the Linq query syntax works because there is a difference in understanding how it works and just using it because it works. I also found an interesting use of an extension method on a nullable type which I describe in Nullable checking in the Wild-West which was interesting. And I've also been working on my Game Engine ideas with regards to Direct X programming.

I'm also looking potentially at rewriting my C library because I don't use it anymore which is mostly because I don't write as much C and C++ as I used to and because I don't like some of the ways I implemented it. For example, I don't need the threading layer now because C11 now includes a cross-platform implementation of threading. And I also don't like my naming conventions and I'd like to add more data structures like black-red trees which is heavily used in the Linux kernel. I also started thinking maybe I should write it in C++ which lead me to write about C++ in Complex or Elaborate

I've not been able to be as productive in my programming side-projects as I'd otherwise like to these days because of the studies, training and work but I'm getting there. I drew up some pictures of some of the concepts I'm grappling within Direct X mostly to do with the idea of defining vertex data, writing that to vertex buffers and having those buffers sent through the 3D graphics pipeline to have them interpreted by vertex and pixel shaders. Pretty interesting stuff but without pictures, my mind usually goes to sleep.

I ordered a 9-cell battery for my laptop because I find that a 1-hour session on the train in the morning to work and back tends to deplete me and my battery by about between Rayners lane and Ruislip. I got it on Friday night and plugged it in and my laptop almost fainted with its new-found power(it couldn't believe it!). I'm just wondering now if I can taking something that will revitalise me at around Rayners Lane too... Anyway, its now got a big, fat bum, (a bit like Kim Kardashian's butt) see here if you like (a picture of the battery, that is)

I also ordered a new SSD drive for my ageing tablet, which I think is so old that the SSD started to fail. After spending like 4 hours figuring out how to get into the guts of my tablet, I was able to see that the SSD was one of these mini-chip-like peripherals about the size of a watch face. I inherited this tablet from a colleague who got it for free by attending a Microsoft Build event - I used it at the time to research Metro and Windows 8 Algorithms for App-DNA back in the day. It's a bit old now but seeing that I can quite happily work on any machine with an SSH connection, it's just absolutely fine. 

So, I took down the serial and looked it up on eBay and found a supplier for a 128Gb card for around 30 pounds. This I thought was reasonable and it that would be double the space I had...bonus! Turns out, it works great, fitted it and installed Ubuntu 14. Not bad, so now I'm getting some good use out of it on my desk at home.

These two are the only computer's I own - the laptop and the tablet. Though, I do have a Rasberry Pi too, which now that I think about would be a perfect project to write a game for. It's a restricted environment so I'd have to use C++ and be careful with my memory and loops. Cool!

In other news, I watched Deadpool, Office Space, Idiocracy and I'm busy reliving my childhood and watching MacGyver which is pretty awesome. I used to watch that every Friday night at 7 pm when I was a kid. Someone at work brought in season 5 DVD for me to watch which I thought was very good of him. I didn't think much of Idiocracy however it was perspective-inducing I guess. Office space was quite enjoyable and Deadpool was OK. 

I've finished playing Fable II and this has been quite a bit of fun, but finding the time do anything apart from studying and work has been difficult which I guess is why doing them is so satisfying. I'll probably move so play Metro: Last light or Dantes Inferno, however, I think its unlikely that I'll have another good attempt until I've finished my assignments..or now that I come to think about it, take some time off.

I watched the new Wreck-it Ralph at the cinema last weekend and I enjoyed it - quite like the first one. It's nice to see how my early childhood fascination with technology is now becoming mainstream as shown in this movie: Its how Ralph breaks the internet and it's cool to see topics like YouTube, Amazon, Instagram, eBay are now embraced in everyday life. This is my life, these are my friends!

Also what I really need to do is find a way to use my Perl knowledge because I've not found a good use for it of late not ever since I finished reading the Camel book earlier this year.

I've also been studying more interesting math facts recently about how to interpret really large numbers like 3212345678998765. Which, is equal to 3 Quadrillion 212 Trillion, 345 Billion, 678 Million, 9998, 765 ! But why?

Legend for the table below:

TH = Thousands, H = Hundreds, T = Tens, U = Units

TH H T                              
TH TH TH TH H T                        
TH TH TH TH TH TH TH H T                  
TH TH TH TH TH TH TH TH TH TH H T            
TH TH TH TH TH TH TH TH TH TH TH TH TH H T U    
3 2 1 2 3 4 5 6 7 8 9 9 8 7 6 5    
Quadrillion     Trillion     Billion     Million                

1000×10004=10005
   
1000×10003=10004
   
1000×10002=10003
   
1000×1000=10002

So it becomes obvious how, where and why occurrences of TH x TH x TH ...n form the named numbers Quadrillion, Trillion, Billion, Million appear in the decimal system and its the iteration of 1000x1000 n.where n is Bi, Tr, Quad etc... Pretty interesting.

The key that is missing for many students is how Bi(2) meaning 2, Tri(3) meaning 3 , Quad(4) meaning 4, relate to the number it represents.  For example, if you are told that a Quintillion(6) is 1018 , which you often are, this's not obvious that this is because of 1000x10006 which is 1018!! I think its a failure of teaching, in general, to explain why certain things are certain things - like this video demonstrates which awesomely demonstrate this failure of teaching: A negative x negative = positive.

The decimal system, in general, is a brilliant system and its all credit to the Indians! Here is something I drew up which puts this system into perspective.

That's pretty much most of what I've been up to recently. Now back to essays and word counts... 

 

  • Game development
  • C++
  • Math
  • DirectX
Details
Category: Code
By Stuart Mathews
Stuart Mathews
18.Nov
18 November 2018
Last Updated: 04 August 2019
Hits: 5223

Understanding how the Linq query syntax works

When it comes to using LINQ expression syntax, I was a relatively late adopter not needing to really understand how it works. I'd just quite happily work with Select(o=>) and SelectMany() without knowing how fundamental they are in LINQ query syntax expressions. That is until I started using languageExt(though never mind about that for now). 

I won't be talking about LanguageExt in this article, rather how types, in general, interact with LINQ. This is however crucial when considering how languageExt types interact with LINQ.

An important aspect of understanding how the LINQ query syntax works is understanding why some objects can be used in LINQ query expressions while others can't.

In most cases, we seem to be able to use them on objects that implement the IEnumerable<T> interface. Why? But before I answer why, let see the LINQ syntax query in action:

from item in ienumerable

We also have this variant:

x = from itemA in IEnumerableA
    from itemB in IEnumerableB
    select itemA + itemB;

Ok, let's try and figure out what these things are doing and why we should worry about them at all.

What are these trying to achieve? Fundamentally what IEnumerable and Linq expressions are trying to do allow you to do this:

foreach(var a in listA)
    {
        foreach(var b in listB)
        {
             yield return Process(a, b);
        }
    }

Cool? Maybe not, interesting? definitely. So this means that it's trying to prevent you from having to keep writing for-each statements like the above. Why?

Because that's boiler-plate code - common code that you tend to write over and over again. What's wrong with the boiler-plate code?

Well its tedious and if we can remove the need to write it, then there will be no errors in any of it that you write. There are other reasons but another on is that you've replaced a conditional syntax with an expression. Nevermind that for now.

So if you look at a typical the LINQ syntax query again:

x = from itemA in IEnumerableA
    from itemB in IEnumerableB
    select itemA + itemB;

How this relates to the looping constructs we saw earlier, is that each "from X in Y" is represented by a for-each X in Y.

If you can think of it that way, it'll be much easier. Then when you're IN that for-each, theoretically, the next "from X in Y" represents the next level for-each(), so you're in the inner for-each loop: 

foreach(var a in listA) // from a in listA
    {
        foreach(var b in listB) // from b in listB
        {
             yield return Process(a, b); // select Process(A,b)
        }
    }

Looked another way:

x = from itemA in IEnumerableA // foreach(var itemA in IEnumerableA) {
    from itemB in IEnumerableB //    foreach(var itemB in IEnumerableB) {
    select itemA + itemB;      //      return itemA+itemB;
                               //    }
                               // }

So we've substituted a for-each conditional for a Linq expression. 

 So we've established that "from X in Y" is executed when in a Linq Expression where Y is an IEnumerable type and X is each item that it gets in the for-each loop. Ok, that's cool, but where is this for-each stuff thats supposedly being substituted for "from X in Y" written? Don't get me wrong i love not having to write foreach statements but there is it?

The answer that this common for-each code is put into the Select() and SelectMany() extension methods for the IEnumerable type. This is so that we don't have to write those for-each statements, they have been put into Select() and SelectMany() functions elsewhere. Extension methods are after-the-fact add-ons for classes, they are static classes however.They 'hook' onto the type without yuo needing to incorporate that method into the types code/definition. Here is what I mean:

To see the image in full res please go here

This would be a good time to go for a cup of tea and then come back once this stuffs percolated in your mind(or has started an internal fight in your mind, either way)

Ok, let's break down this diagram.

Firstly Select() and SelectMany() do roughly the same thing initially and then SelectMany() just does one extra bit.

Select and SelectMany() both perform a for-each on an item in the IEnumerable, effectively extracting each item from the IEnumerable and then it runs a transformation function on each of those items it got. 

That's basically what Select() does and the result of that function is then put back into a new IEnumerable. SelectMany() is the same up to that point. We'll get to how SelectMany() differs later but for now there are  two observations now need to make about Select()'s implementation:

  1. The function that is run on each obtained item in the IEnumerable is called a mapping function and its supplied by the user. You can see its passed in as a parameter to Select and called map.
  2. The result of running of mapping function on each item has the results put into the same place of the original item but in a new IEnumerable.(the original Enumerable is untouched and in fact, IEnumerables cannot be modified which is why they are made into a similar but new IEnumerable from the first)
  3. This has effectively mapped each item in the original IEnumerable to the result of the map(item) function into a new IEnumerable. We also say that this has transformed the original item and put it into a similar but new IEnumerable as the original (but now it has the mapped() result version instead of the original item)

See, no magic, You can see the for-each stuff that's used in the Linq expressions now:

public static class EnumerableExtensions
    {
        public static IEnumerable<B> Select<A, B>(this IEnumerable<A> self, Func<A, B> map)
        {
            foreach(var item in self)
            {
                yield return map(item);
            }
        }       
    }

Note that extension methods like Select work on a Type indicated by the this operator in the first parameter to the extension method. In this instance the extension method if for the IEnumerable type.

Anyway, all this code is what happens when you see this:

x = from itemA in IEnumerableA  // foreach var itemA in IEnumerableA
    select itemA.Upper(); // this is basically the mapping function run for each item in IEnumerableA
                               
                               

the select clause (select item.Upper()) is passed in as the map() function.

Ok, but what about when you have two from clauses as the previous examples had? We've just explained that Select() makes the above work but that only has on from clause in it.

When you have two from clauses in it, thats when SelectMany() function is used:

public static class EnumerableExtensions
    {        
        public static IEnumerable<C> SelectMany<A, B, C>(
            this IEnumerable<A> self, 
            Func<A, IEnumerable<B>> bind, 
            Func<A, B, C> project)
        {
            foreach(var a in self)
            {
                foreach(var b in bind(a))
                {
                    yield return project(a, b);
                }
            }
        }
    }

As I said earlier, SelectMany() starts of the same way that Select() does, it performs a foreach on each item in the IEnumerable and runs a function on it. However this function is called a binding function and this functions results aren't passed immeditable back into the IEnumerable, they have to do a little bit more:

  1. Again it gets each item in the IEnumerable (exactly like the Select() implementation)
  2. It runs a bind() function instead of a map() function on each item - conceptually this is the same in the Select() implementation
  3. The bind() function is provided by the user and it must be designed to return another IEnumerable (though we still wont pass the result back into the IEnumerable yet)
  4. Another foreach it performed on the result of the Bind() function 
  5. Each of those items(in the newly produced list) are passed into a Project() function, another function that the user provides, now the results of this secondary function are passed back into a similar but new IEnumerable as the original.

Quick observations about the SelectMany() implementation:

  • You provide two functions in SelectMany() instead of one (as in the Select())
  • The first is like a map() in Select() but is called bind() and both operate on each item in the IEnumerable (and they both transform)
  • Where map returns a value, bind() returns an IEnumerable, those list of values are further processed by a project() function 
  • The project function is the 2nd function passed into SelectMany() - Select() doesn't have a 2nd function.

Look at my diagram again and see if this starts to make sense:

 So that's fundamentally why the Linq Syntax query works on IEnumerables (or any types that have Select() and SelectMany()).

Why this is important in languageExt is because its types behave like IEnumerables that have also implemented Select() and SelectMany() and thus appear in Linq query syntax like this:

using static LanguageExt.Prelude;
 
   Option<string> optionA = Some("Hello, ");
   Option optionB = Some("World");
   Option optionNone = None;
 
   var result1 = from x in optionA
                 from y in optionB
                 select x + y;
 
   var result2 = from x in optionA
                 from y in optionB
                 from z in optionNone
                 select x + y + z;

So Option<T>, Either<L,R> can be viewed being able to do a for-each(var item in Option<t>) ...because that's exactly what happens in the above when its used in Linq syntax query expressions!

In the same way that a for-each gets each item in a list or an IEnumerable, the for-each gets an item from the option, or either and depending on its value, will determine if the next inner for-each will run or not.

And this article goes a long way into explaining how these types can be in those Linq expressions, to begin with. In the next instalment in this functional programming series, I'll explain a bit more about how actually works, but with this introduction, you can see that it has manipulated the Select() and SelectMany() methods so that it can be used in LINQ queries.

Note: List<T>, Arraylist<T> etc are all IEnumerables

  • Programming
  • Functional programming
Details
Category: Blog
By Stuart Mathews
Stuart Mathews
06.Sep
06 September 2018
Last Updated: 06 September 2018
Hits: 3033

Running, Coffee, Chocolate and Pizza

I’ve adjusted to my new routine, I’ve successfully swapped my morning routine to an evening routine. This means I’m racing to the gym after work instead of comprimising important hours of sleep in the early morning. Studies have shown that neglecting the early morning hours of rest is deterimental to memory and learning. That’s according to the sleep scientist, Matt Walker in his book Why we sleep. So I’ve decided that I can reverse my routine.

That book has been fairly influential to me in other ways. I no longer drink coffee. Not because its unhealthy, which it isn’t really. More for the fact that it can impair your ability to sleep well and I know how impactful a bad night’s sleep has on running, gym and thinking at work. That being said, I do yearn for a warm cup of coffee from time to time. 

Someone offered me a free cup of coffee and whilst I thanked him for the gesture, I declined. After he’s puzzled gaze seemed to have lingered for more than I could stand, I qualified my ‘no thank you’ gesture with, ‘ I don’t drink coffee’ which I hesitated to mention and all of which I needn’t have done if it didn’t seem so weird that I didn’t want a free coffee. I got the same puzzled look when someone offered me a slice of pizza and another instance back when someone offered me chocolate: I always say as politely and graciously as I can, ‘ thanks very much but I’m ok’ because I don’t want to offend their goodwill but sometimes they are adamant that I’m being unusual! I like to be actively involved in my day-to-day decisions and its like my training - I stick to it. You’ve got to listen to yourself and respect your own choices and see them through. I like carrying through with my dicisions, otherwise it would’t be me!

I went for another run on Wednesday and it was great but tiring - I went down to Limehouse basin again but instead of running back up or going along the Thames, I went up another canal, adjacent to Regents canal which is the one I came down with towards Limehouse Basin Marina. It was so nice.

It was quiet and it was flat and winding. There was lots of graffiti and I’ve come to really embrace this form of street artwork on my runs. I probably wont see better quality and creative instances anywhere else other than in Shoreditch and surrounding areas.

I’m started becoming fairly familiar with these longer runs and I routinely take a gel with me and drink it at around 1 hours into the run. I’m now thinking of taking water with me too though I’ve not been one who’s been used to running with water. I’ll try it because it will make my run easier methinks. Here are the details of my last long run:

I recently started a new university degree and I’m looking forward to this year’s two modules. The first one is about continuous professional development which is about analysis of 400 hours of my last professional projects in software development with the view to assess/predict my future directions in the industry. This is a fairly interesting and a new way of doing things for me as I’m used to the more direct, technical things like learning how MongoDB works or designing mobile apps or programming full-on. I’ve not done this kind of assessment before: I’m going to have to do more reporting and analysis modules like this moving forward I think, ultimately culminating in my professional project which is the capstone of this degree.  The other module is more on par with traditional direct teaching - the software engineering bread butter stuff I’m more familiar with: teaching you software engineering best practises etc around software planning and management such as source control and requirements gathering etc. My last module was quite heavy into python and data analysis so  I’m offsetting things a bit by doing the slightly more theoretical stuff first perhaps as a strategy to recover from all the programming! I seem to recover in training by resting and I recover from studying by studying something something slightly more different and less familiar.

I think its safe to say that I wont be having much free time, especially at this level of study.

I’ve been working on my investment tracker app in my spare time, I’m learning a great deal about many things, primarily investing in general but in terms of software development I’ve learned an incredible amount about Angular 6+.

The last few features that I needed to implement were graphically representing some of the underlying investment data I have, This is primarily drawing visualisations. I’ve cobbled a pie, bar and interactive directed graph visualisations. I’ve not really learnt how to master their construction however I’ve not needed to in order to meet my requirements. So now I can easily see how various aspects of my investments relate(directed graph) and how over subscribed I am to certain aspects - a bar graph because easily shows equality inconsistencies more than I pie chart. Here is what the bar graph looks like:

The features of my investment tracker are complete. This is a great thing to achieve because its been a project that I’ve dynamically invented requirements as I’ve recognised the need. The last feature I implemented was what are called custom aspects. These are ways that you can define aspects to define an investment in a more specific way. It’s anologous to properties I guess and I sure I could represent it as this. This is how it looks:

It’s basically custom aspects where the default aspects or ‘entities’ of an investment are Factors, Risks, Groups and Regions. So now you’re not constrained to defining your investment by just four aspects - you can add as many as you like!

I’ve also strayed into the world of modal pop-ups and this has got me into trouble when I’ve got lots of data like as is the case with the shared relationships graph - the directed graph I mentioned earlier, see:

My goal now is to use it - because that’s why I developed it in the first place. Being a software developer must be one of the best jobs in the world as its all about thinking, modelling thought and solving problems in space. It’s truely fulfilling when you apply your fascinations and desires into it.

I also decided, for the first time in a long while, to fire up the XBox 360 and play a few games. I’ve purchased 3 games which is a bit OTT because one game usually takes. Like 3-6 months for me to finish. However the 3 games are all appealing and would quite probably make the succession anyway. They are Fable 3 which I’ve started and I’m enjoying tremendously as its very much Kingdoms of Amalur however I yearn for it to be more like it. The next 2 titles are in the queue and they are Castlevania, something to do with Dracula is the story. The next is Metro: Last Light which is not like the others - this is a first person shooter and It looked great when I played the preview more than I year ago - this is how long it been since I’ve played properly on my XBox!

I’ve got a day off today and I spent it developing the bar graph in my investment app. I’ve also been learning Perl and I’m fixing to use it on something. I really enjoy reading books on the train to work. I’ve not played any games today but maybe I will later now that I’ve done all things I wanted to do with the investment app. I’ve been writing perl for a while but I’m trying to improve: 

Oh and I’ve got Linux running on my Windows 10 box which is Great! I’ve installed an X server and I’m using xfce4-terminal which is far more superior than Windows’ default terminal. I’m very happy that Microsoft has invested so much in Linux this way because really this is now proper Linux within Windows the only difference is that the windows kernel is servicing the underlying the syscalls the apps request. Awesome. Not sure how this will influences the Linux kernel...as we’re not using one...

I also started to think about Parameter type validation recently.

 

  • Programming
  • Running
  • Gym
  • Perl
Details
Category: Code
By Stuart Mathews
Stuart Mathews
30.Aug
30 August 2018
Last Updated: 05 September 2018
Hits: 3071

Parameter type validation

We had a discussion about how validation firstly needs to be done and secondly how horrible it looks if its done multiple times throughout the code. It was briefly mentioned how this can be achieved using the C# type system to do on parameter. This idea in my mind wasn't appreciated and the conversation moved on without exposition on how this can be done. C++ Tends to take this approach a lot and its an approach that favours abstraction to achieve simplicity.

The idea is that if you have a function prototype like this:

int SetName( string firstName, string lastName )

Now this is a contrived example. Which apart from aall other other deficiencies in this line of code is intended by the programmer to mean subtract a from b. Another thing the programmer intends is that this function be correct when its precondition is that b > a.  As I said many deficiencies in this prototype hide intention, pre-conditions and other aspects that the function assumes or requires. This isn't about these kinds problems. I'd like to talk about how ugly the code around it looks:

Base case scenario this code is used like this:

// Is first Name or Last name empty dunno, no indication that it is or should be checked
var result = SetName(formFirstName, formLastName)

Somewhat better but now you can proliferate checking code all over the code:

// precondition check
if(!formFirstName.IsNullOrEmpty())  // basic check
    var result = SetName(formFirstName, formLastName)

But the question arises, "Where should the pre-condition checks occur? In the function or before it". I'd argue that it should be done before but I don't like seeing conditions like the above in the code. You can make the input into the function (a or b ) validate itself before its used in the function by making a new type which cannot be constructed when the a or b is invalid:

int SetName(Person person)

And so in the construction of Person you do your validation and this also as a side effect does the post-condition validation for the function.

 

But what if your constructor throw an exception during construction - that's bad and so we wont use one! How do we get around that? Easy. Smart constructors. This highlights 3 key aspects:

  1. You can force validation of function inputs by using new a new type to validate construction of that type.
  2. You can avoid exception which might have occurred during validation when constructing this new type.
  3. (Bonus) Can also enforce immutability of the input so that functions can be more pure (the input cannot be changed in-place).
namespace FunctionalProgramming
{
    /// <summary>
    /// This is an immutable object because:
    /// a) Has private setters and that means its state cannot be changed after creation
    /// b) All properties are immutable - either strings or native types or System.Collections.Immutable types
    /// c) Any change that needs to take place must result in a newly create object of this type
    /// </summary>
    public class Person
    {
        public string FirstName { get; }
        public string LastName { get; }

        /// <summary>
        /// Notice this is a private constructor so a Person cannot be created normally...there must be another way in!
        /// It must be created indirectly via .Of() or .New() which then can use the private constructor below
        /// </summary>
        /// <param name="firstName">the persons first name</param>
        /// <param name="lastName">The persons last name</param>
        private Person(string firstName, string lastName)
        {
            if (!IsValid(firstName, lastName))
            {
                throw new ArgumentException("Invalid input");
            }
            FirstName = firstName;
            LastName = lastName;
        }

        /// <summary>
        ///  This pretends to be a constructor by being the only method allowed to call the constructor.
        ///  The actual and real constructor is only called if input is valid to this function 
        ///  As a result it gaurentees no exceptions can ever be thrown during creation of the object through
        ///  this method.
        /// </summary>
        /// <param name="firstName">Persons first name</param>
        /// <param name="lastName">Persons last name</param>
        /// <returns></returns>
        public static Option<Person> Of(string firstName, string lastName) 
            => IsValid(firstName, lastName) 
                ? Option <Person>.Some(new Person(firstName, lastName)) 
                : Option <Person>.None;

        private static bool IsValid(string firstName, string lastName)
            => string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(lastName);

    }
    
}

Something like that anyway ;-)

 

Details
Category: Blog
By Stuart Mathews
Stuart Mathews
19.Aug
19 August 2018
Last Updated: 21 August 2018
Hits: 2661

A commentary on Leadership

I saw a book about leadership on a shelf recently and it made me think about it. Firstly, and this is what made me curious about it – is that I’ve never really wanted to think about it, then realising that, I wondered why. 

Maybe its because I’ve associated with it some aspects that I’m not interested in. For example, when I think about leadership I think about popularity, I think about successful social and group dynamics. I think about being well-liked and perhaps even being in control of people. Now, all those things I’ve never really been into. Either because I’m never found myself being particularly successful or useful at these things, and then sort of favoured other things.

But I’ve never really needed to worry about these things and perhaps because I’ve associated these things with leadership, I’ve never really practised leadership in this regard. Now my definition of leadership could well be the problem and perhaps I’ve avoided leadership because this is the way I’ve defined it. I dunno but this is a quite interesting retrospective.

Why am I like this or why am I the way I am? Very interesting thought. Are these aspects of leadership that I’ve associated with it correct? Why am I so interested in it now?

To answer the first question is to say I don’t know but perhaps its because I’m less inclined to be sociable. Or perhaps that I’m not that interested in being ‘boss’ or ‘father’ or anyone that is in control of a situation or group of people or whatever – I just don’t know for sure.  As for my second question, this is the journey I intend to discover. And finally, why am I interested in it now? I dunno that answer either but perhaps it will become clearer soon.

In my mind, perhaps my questioning of those traditional aspects of what I’ve (rightly or wrongly) defined leadership to be is due to my increasing maturity and experience in life. Perhaps it's not having to have actively spent time thinking about leadership. I’ve been far more concerned about other things: making sure I can deliver some outcome, making sure that I don't get things wrong. Making sure that I’m enjoying myself. And ultimately, making sure I’m achieving these goals.

Most of my goals aren’t to ensure that that people around me do their work or that they go about their work in a certain manner or way. I leave that to them, they either can or they can’t. I see myself as one of these people too – It's up to me to sort myself out. There is no spoon feeding in adulthood.

I’ve never looked to anyone really to make me better or to help me out. I’ve not looked for charity for anything. Sure, I do notice advice/help along the way in my endeavours and I do learn from it, and sometimes I help others in this way too (as I’m already here doing it) and people around me either learn from what I do or don't – its up to them - but this is not the primary focus in my line of work. My primary focus is to do and to do it well. How do I do it well, by trying by persevering and by adapting to needs and changes and being excited about seeing how well I do and at the same time see how much I’m enjoying the experience. Here is an example:

I’m a programmer, I’m an introvert and my job is primarily about learning and writing programs. There are many countless ways and technologies to use, each technology has its behaviours and ins-and-outs. Every couple of months something I learnt in the last job is no longer used, so it's now the next thing. It's an uphill battle, much like determining what the next fashion will be. To stay relevant, I need to be able to do the next thing, to figure it out, to deliver it, to become comfortable with it. And I enjoy it, I enjoy learning so It’s ok that what I learnt a while back is obsolete – I enjoyed it when it wasn’t obsolete and I enjoyed learning how to tame it when it was new and unwieldy. Now, during this time, I’ve not thought about leadership, that’s everyone else’s problem.

There really is no time to spend doing something else. Perhaps that something else is leadership.

I do think this has given me a unique insight: I’m not a trained leader. That being said, I know certain things that people expect from a leader. I’ve seen it, I’ve needed it. I’ve been a consumer of leaders for years. I eat them for breakfast!

I’ve started to realise what I think leadership is:

  1. Leaders are expected by non-leaders to be more experienced and thus have more insight than they do. (experience)
    • If I don’t know something I’ll do ask someone who does.
  2. Leaders are expected by non-leaders to make the decisions for them or be heavily in favour of the ultimate decision (A big player in the resultant decision)
    • I need a decision to be made so that I can carry on doing the work.
  3. Leaders are expected by non-leaders to know what to do next.
    • I need to know what the priority is.
  4. Leaders are expected by non-leaders to have advice for them that is that’s satisfactory to them.
    • I’m concerned, worried, frightened and I need you to alleviate that for me. (unblock me please!)
  5. Leaders are expected by non-leaders to be more capable than themselves.
    • I need you to have a known and good track record of success.

I’m sure there is a lot of things missing and I might be wrong but this is my take on the matter. Here’s what's revealing:

I took out that book and started to read it. One thing it said(and this book tends to say a lot) is that anyone can be a leader. And to this extent, this is true. If you look at the above all you need to do is been in a similar or roughly similar position before(experience), decide what you’d do in that situation(decision maker or unblock me please) and then decide what you’d do next(priority) and lastly be confident that you’re ok with what you said because you’ve got some success in this area.

So let's distil what it takes to be a leader real quick and dirty;

  • Be in a position of experience (Done it before or near enough)
  • Make the decision that you’d make normally and offer them it to others (Offer as advice what you’d do)
  • Take the next thing that needs to be done and do it – or off it to others. (offer as advice what you’d do next)
  • Empathise with other’s inability and solve that problem for them (Empathize and offer a relief)
  • Know that If you were left to do it yourself, you’d be able to do it. (Know that that’s how you’d do it)

And even further:

Done it before or near enough; Offer as advice what you’d do; offer as advice what you’d do next; empathize and alleviate the pain; know why you’d succeed if you were to do it yourself in this way.

Or even better a storey:

I’ve done it before, this is what I’d do now and what I’d do next. I can relate to how you feel. I’d do it this way because…

What’s also interesting is that perhaps it people that have not been self, set out ‘doers’ in one of those 5 positions that need help/guidance in those 5 positions: (Experience, Immediate guidance, Priority guidance, never experienced this before(unblock me) and assurance that it will work out.

Now as I've said this is dirty - it's not what leadership is, its what it is to me and it may be wrong because my definition is wrong. Also, I've left out things like having "vision" and other stuff like that which this book goes on about. That could be a big part of it!

This is not the end-all of it but its a useful insight and commentary. 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Load more...