Looking Into SBT

I have spent the past couple of days mostly looking at the Scala build tool (sbt). It seems to be the most commonly used build tool for scala.

Honestly I am neither impressed or unimpressed by it and that is a good thing. Some things just should not be exciting. They should just work with the minimal fuss and really get out of your way. Build tools are necessary evil of many computer languages. I think the go language has pretty much my favorite one as it is also the compiler.

SBT is config is as minimal as you want it to be and my real goal was to figure out how to have multiple projects in a single sbt build files along with setting up dependencies between them.

Turns out that is pretty easy to do. I did make about 4 pages of hastily scribbled notes as I went through the documentation. At one point I thought they might be good as the basis for a blog post but eventually I realized I would not be adding much to the already good documentation at the sbt website.

Next I am probably going to take a bit of a dive into ScalaJS again. Mostly because I want to write a few bits of example code that would be better done if displayed visually. I am much more familiar with HTML5 rather than JavaFX so it makes sense to use ScalaJS.

Scala Tree

Over the past few weeks I have been taking a bit of a look a Scala. Well it is really more of a revisit as I played with Scala about 7 years ago. I have watched a few videos and more importantly read through the 3rd edition of the Programming in Scala book.

Just reading and watching does not let you learn a language, it just give you an idea that you may, or may not, like using it.

To really gain an understanding of the language you have to code in it. Honestly you have to code in it with a vengeance. Not just write a bit of code and decided one way of the other. It really does take thousands of lines of code to find the gritty points or gain an appreciation about how all the features combined together in both the good and the bad ways.

I am just starting this journey and this blog post is about the first 40 lines of code.

The first program I decided to write is a little directory listing program that indents name of files and directories based on the recursive depth we find ourselves at. Both Unix and Windows have the shell command called tree, should you want a well implemented version.

This post discusses my implementation looks at some of the code before talking about some of my conclusions.

Representing the directory structure

Each item in a directory is itself either a File or a Directory. To model this I decided to use case classes. Case classes add a few extra features to a normal class. Their equality is correct and the easy destructuring in match statements without any extra work is useful. I have seem plenty of example code that uses them in recursive data structures such as this.

Lets look at the code.

So we create a base class Node and then create two concrete classes. The DirNode is the one that goes recursive as it contains a list of subNodes of type Node.

I really like this code as it reminds me of playing with Haskell a long time ago. I think you could declare this layout in Haskell with less keystrokes but not less lines. This Scala code is very concise.

The main function

Every program needs and entry point and in Scala that takes the form of a traditional main function contained in an object. I currently think of objects as singletons but get the feeling they are more complicated than that so expect to shift my thinking on them as I write more code. Here is the main function

Just get the current directory path create a File from it and give the File to the makeNodeTree function. Finally put the result of this into my display function. The Paths object is part of java.nio of which I honestly know very little. File has been part of Java a little longer. Most of my knowledge of Java is from android programming I did a while back and even then most of it was done using LibGDX meaning I was abstracted away from much of the Java library functionality that comes with the JVM.

makeNodeTree
This functions takes a file and create a node structure from it. A file can be either a normal file such as one containing text of a directory. Here is the code.

Initially this function was a little longer. Instead of a map I had an explicit for loop and used a ListBuffer. I find for loops really easy to parse and they often look concise and convey their meaning easily. I am however aware that this years of C++ experience talking.

Using a map rather than a for loop conveys more information than seeing a for statement. It tells me the return values from makeNodeTree are going to be collected together and returned. Seeing a reduce tells me something else and it is just my lack of familiarity with them holding me back. Of course for loops are still very useful and used quite a bit in Scala code as far as I can see so it is really a balancing act.

I was pretty happy to get it down to just three lines.

display
This is the final function. It takes the node structure and prints it out on the display with the correct indentation. I am implementation leaking a bit here as I accept an indentation value in this function. This hints are a recursive function, ideally you would not include this parameter in a public interface, instead the display function should call another private function that takes the indent function. Honestly I am OK with this given this is just learning code. Here is the code.

So this is my first match statement. We match and we destructure out the values in each case class. I add a little bar ‘|’ onto nested directories to give a visual indicator. Funnily enough I reached for the foreach function for the sub nodes rather than a for loop so my brain is halfway there. foreach tells me nothing is returned so I am executing for side effects, in this case displaying things onto the terminal.

Conclusions
Overall I am pretty happy with the program. Yes it only displays from the current directory it is run from and yes there are a huge number of features that could be added. Just look at the Tree command on your system.

That is not the point though. I wanted to write something simple to let me start to internalize the language and identify what I need to improve.

Why am I dong this
Why have I written so much for a 37 line program? Well the answer to that is to force myself to think about the code I am writing on different levels.

Explaining the code in text is equivalent to a code review. makeNodeTree was a little longer before I started writing this post. As I explained the code I reduced to code it contained to something that was almost trivial to explain. If something is obviously correct then you are in a good place to be. A good first step is to make it trivial to understand.

Doomg this self code review through a post lets me think about my though processes while coding. I clearly need to use more built in iteration functions rather than relying on the classic for loop. It results in cleaner code. Honestly I have known this from my first days of exploring Haskell many years ago but have never really force myself to internalize them.

Will I continue to write the long form explanation of code. As long as I think I am getting some benefit from them I suspect I will be motivated to do so.

Full code
If I get enough sample projects I may eventually put them on github but for now here is the full source code. All 37 lines of it.

The End of my Thinking Month

So my thinking month is not just about over, we head rapidly into august and I should not start to pick up the momentum a bit.

I took a holiday to somewhere hot in July which was a total break from everything normal combined with an exercise in controlling the crazily energetic children we seem to have.

I am going to avoid tech talk for the most part in this post other than to say I continue to experiment with Scala. Yep enjoying it but definitely not written enough code to have a proper opinion on it.

During July I re-read some of the essays at paulgraham.com. A few years back I went off his writings as I started to feel he was almost using it as a recruitment vehicle for his angel funding business making me relate less  to his writings. On revisiting his site I  still find his earlier stuff much more interest and am surprised how much I agree with.

I honestly don’t know if it is his writings that influence the way I think or just many people, of which I am just one, arrive at the same conclusions if they think enough about topics. Perhaps friends who read his works have influenced my thinking. It is just not possible for me to know even though I would love to know the answer.

My tolerance for complaining is at an all time low. Given I live in the UK  and recent vote to leave the EU has triggered a mass of complaining and fear mongering both before and after the vote, it is fair to say it has not been the most enjoyable time to have a low tolerance in complaining.

What I have found fascinating about it is how little people listen to each other. Debate on these sort of things is almost pointless as most people will not change their minds, holding to whatever view they have as tightly as possible. Just for fun I spent time arguing for both sides depending who I was talking to and found the inability to shift even slightly fascinating. I guess their views on this were ingrained into their identity.

See this is what happens when I stop coding I start to get much more interested in things closer to psychology of people and how they act. Perhaps I should have  just played computer games!

So this month I start to code again.

Scala

Part of my rethinking is to reconsider the tools I use. I flip between quite a few different languages and never seem to quite master them. Yep this post is going to be one of those programming language posts.

The funny thing is I have not looked at scala for nearly 8 years. Just recently it just keeps on appearing in subtle ways as I browse the web or talk to people. Enough for me to take a look at it again, I usually ask the questions?

  • Can it deploy to android?
  • Can it deploy to the web?
  • Is it fairly succinct and sufficiently high level?
  • Are there a good set of libraries?

Scala pretty much ticks all those and I am left asking myself why I have revisited this language in a while. I am neither enthusiastic or pessimistic about Scala just is looks like a pragmatic language to use that ticks all the required boxes.

So I plan to spend a little bit of my “thinking” month experimenting with it and seeing how I find it 8 years out since I last touched it.

Thinking

I have been thinking a great deal over the past couple of weeks, of course this means I have produced absolutely nothing. I am ok with that and I am specifically writing off the rest of this month for more thinking. Well this month is pretty busy with many different things so even if I was not thinking it would not be a productive time.

I have travelled quite a bit of ground while on this “great” think and am trying to figure out what is relevant to this blog while trying to avoid the empty promises that are so often associated with these type of posts. So in a slightly disjointed way here goes…

Goal driven: It would be fair to say I have not been goal driven over the past few years. By character I am pretty easy-going with a what will be will be kind of attitude. I don’t feel compelled to prove my intelligence in code an attribute I often see in programmers. Coding, at home, is often a journey or exploration of ideas that I am not too worried about sharing. However I also enjoy releasing stuff onto the world even if the polish it not that good.

Combining the two in one project is never a good idea for me. An example of this might be deciding to write my own physics engine for a platform game I want to write. The goal is the game but more of my time will be spent exploring writing a physics engine. The result being I don’t achieve the goal of releasing an actual game.

So I am going to try to differentiate the two types of projects a little more.

A return to paper: I often sit at sit down at my desk to work on a project but don’t really know what next. To counter this I am going to start to plan a little more so I always have direction on where to go next. I will do this on paper as whenever I attempt this in an electronic form it never seems to work out too well. I won’t go into detail on everything but I will have a goal, along with a feature set and a list of tasks to tackle to get there. Not a detailed list and one that is flexible to change.

I am going to be a bit adaptive on this and try to figure out what works.

Look Ma no data: I currently have no data on where to go. To rectify this I need some easy win projects. Things that take a month not six months to complete. Of course that means each one will take about 2 months. This is not just external data but also internal data. How do I feel when I am creating this type of app or game. Does it still interest me.

The internal data is really important to me. Some stuff I think I will find interesting doesn’t seem to pull me in. Other stuff, like natural language processing, seems to pull me in even though on paper it shouldn’t. Is my internal compass for projects I would like work on at home based on a version of me from 10 years ago. I think everyone should ask these questions from time to time.

It doesn’t have to be complex: I am drawn to complex problems like a moth to the moon. My working day can often be involve quite complex thinking. This means at home I should not really judge the stuff I am doing at home by its complexity. If I am constantly doing complex stuff I will end up burning out.

OK the final one: I seem to have an unhealthy obsession with tooling in particular text editors and over the years have spent a fair bit of time setting up Vim/Emacs. It is fun to do but something to be done in spare time. Spending 4 evening messing with my Emacs config rather than just using Atom or WebStorm for JS development doesn’t really make much sense.

Honestly I could go on listing out these thoughts but I know they are a little disjoint and probably not that interesting to the reader. I am thinking on where to take this blog, particularly about blog frequency but nothing I want to put to text just yet. The indulgence of writing about the changes I am trying to make will probably make it into at least a couple more posts.

JavaScript 6 – finally usable?

Some time over the past few months the support for JS6 has improved to the point where it looks like it is possible to code in it and not worry about if a certain feature is supported. It is not quite 100% in any browser but it is pretty close.

I took a bit of a dive into JS6 and enjoyed what I found a year or so ago. Many of my complaints about JavaScript just went away. Of course at the time browser support was not quite there.

Web Assembly also seem to be making good progress. I suspect this is going to be incredibly good for JavaScript as a whole. Being able to code up your CPU  intensive parts in  C++ but for everything else you remain in the productive world of JavaScript. Yes I am thinking physics engines in games.

That is assuming that Web Assembly and JavaScript can talk to each other in some sane way? I haven’t looked.

In terms of productivity at home I have been struggling. Work has been busy (I really enjoy my work) and home life also seems quite busy at the moment. This means by the time I slump in front of a computer my brain is pretty frazzled. Usually I browse a bit and head off to watch some Netflixs. I think the period is starting to come to an end and I will hopefully resume some fun coding at home.

For some reason I have flipped from vim to Emacs when knocking out random bits of text, like this post. I am not yet sure why and when I figure it  out I will let you know.

Messing with a mini C++ game engine

After spending a few hour over a couple of nights messing with C++ which included reminding myself how to use CMake and getting familiar with QTCreator, I have finally got something on the screen.

Well it is a series of red squares so not I am not going to post an image. The main.cpp file looks like this:

As you can see it is fairly concise and about the level I was aiming for. Of course it is backed by several hundred lines of library code and even then I build on top of the SDL2 library. But a user of my mini library would be up and running with very little code. Pretty much create a State class and add four lines of code to the main function and you are good to go.

Of course the library does do much more than display images at the moment and I am not planning on rapid development as it is a for fun, when I feel like it kind of project.

Yes I call the library Caffeine, I always do that for throw away code and rename things if I think they are going to stay around for more than a few weeks. Caffeine is just too hard to spell…

Thinking a little on C++

Since C++ 11 arrived on the scene my opinion of C++ as a programming language to use for messing around with at home has changed.

I used to hold the opinion that is was a little verbose and that combined with slower build times and the pain of deployment it was off-putting, usually I would head towards Java, JavaScript or python when coding at home.

In many instance I still hold that opinion but mostly because of the huge number of easy to reach libraries provided by the aforementioned languages.

However with C++ 14 most of the verbosity I used to hit has now been removed. The tooling has improved, although it is still not great and importantly the open source IDEs have improved to the point where I find them quite productive.

Importantly being a professional C++ programmer by day it is fair to say I am familiar with it. Familiarity breeds productivity when it come to programming languages.

All this means I have been revisiting my thoughts on what I call slow C++. The idea that if as a C++ programmer you can forget all about efficiency of the running code you  can focus on using C++ productivity in terms of getting stuff done. Obviously you can go back and speed things up when you need to.

To test this idea I have been playing with writing a little gaming library build on top of SDL2. It on about a couple of hundred of lines of code or so but so far I am quite enjoying creating it. The underlying goal is ease to use.

Obviously there are plenty of 2D gaming libraries and this one is really just a place for me to try out my ideas. Perhaps I will push it up to github at some point but that is predicated on me continuing to work on it for more than a few evenings.

Yep, I Did it again

Yep I have restarted the blog again, mostly because I forgot my login details, had not set up my email address properly and lost the piece of paper that I had written the details down on. Of course it will turn up pretty soon as I don’t need it.

There was not much here although I did have a few blogs in draft form that I had been working on so it is a bit annoying.