Emacs Tricks With Go

I am still going through some Go tutorials. I am using emacs as my editor.

There are some things that the go mode does not handle well; I don’t know if this is a shortcoming in the mode, or if I am using an old version, or if there is some conflict with another mode I am using. It’s actually a minor point.

It does not indent switch statements the way I want. All the “case” statements and everything inside them is indented as far as the “switch”. Hitting tab does not work. I have to hit “space” for every line. It gets pretty tedious.

But I found a way to do it:

  • set-mark-command (C-@) and highlight the region
  • indent-rigidly (C-x TAB)
  • hit the arrow key as many times as I need. This will indent all the lines the same amount.

 

I also learned something about opening files. I had a directory with a file called “time.go” and “showtime.go”. When I tried find-file (C-x C-f), or switch-to-buffer (C-x b), it kept picking up “showtime.go”, even though I wanted “time.go”. I was able to open “time.go” with find-alternate-file (C-x C-v). Then I had to re-open “showtime.go”. It’s not a perfect solution, but it worked for me.

These commands are not unique to the Go mode.

You’re welcome.

August 2020 Update

This month I have been going through some Go tutorials on Pluralsight.

Actually, just one, but it has been taking a while. The instructor has this (IMHO) bad habit of wiping out the contents of the file he is working on and replacing them with new content without warning. And sometimes he does not go over helper functions that he writes and are called from his main functions. It makes it frustrating.

I think I will keep an eye on Go. I bookmarked an “awesome-go” repo (note to “awesome” repo maintainers: These are a lot easier to follow when the libraries are categorized, and the lists and sub-lists are alphabetized). I started looking at a web framework called Buffalo, but to install it you have to install npm and other Javascript garbage. I hate Javascript and would like to keep it out of my life as much as possible.

(Before anyone goes on and one about how I should just accept that you have to deal with Javascript to do web development: I use Office and Sharepoint every damn day at my job. And not by choice. I am getting tired of doing things because I have to. I want to start doing things because I want to.)

I was thinking of ignoring Go, but I found a post on the Java subreddit about the latest Jetbrains State of the Developer Ecosystem survey. Granted, the respondents are users of Jetbrains products, so it is not representative of all developers. But I decided to take a look. There is a section about Go. Buffalo is not the most common web framework. The winner is one called Gin (here are links to the website and github repo). I might look into this later. The github repo and the website both state that Gin has a “martini-like API”, which on the surface is a meaningless statement. I googled “golang martini”, and apparently there was a Go web framework called Martini. The repo page says it is deprecated, and the last commit was in 2016. Note to whoever is doing the marketing for Gin: Comparing your project to a dead project is not the best selling point.

There is a rules engine written in Go called Grule. The main reason to build a web app is to work with a rules engine. My day job involves a proprietary rules engine that I am kind of sick of dealing with. (Thanks for the opportunity, multi-national corporation, but at some point I will probably move on to something different.) There is also Drools (written in Java) and Clara (written in Clojure).

One issue I have at the moment is that the Grule page has this tidbit: “Business Analysts can easily read and verify a set of rules.” Just about every technology that pitches itself as something that “the business people can use so you don’t have to be a developer” never lives up to that promise. Score one for Clara.

You’re welcome.

July 2020 Update

This past month, I spent more time going through tutorials on Pluralsight. At least, when I have time. There was a release for the app I work on at my job, and there were a lot of defects.

Some of the tutorials take a while. I like to type in the code as they do them. But I might still be going too fast for the material to sink in. Right now I am going for breadth rather than depth. I don’t think I want to do Java forever.

Right now I am going through some Spring Boot tutorials. So far it looks interesting. I am a bit leery of doing anything Spring related because of the database abstractions. Sometimes it can be hard to find the SQL that populates a list. There are all the implementations that call controllers that call DAOs, some of which call other DAOs, and then the SQL is being pulled out of some XML file. I get tired of the whole “the toe bone is connected to the foot bone is connected to the ankle bone is connected to the shin bone” routine. I just want the SQL that populates some list.

Frankly, the app I work with is not using the most current practices. I am grateful to have a job, but I feel like the world is passing me by. But sometimes I wonder if the app I work with can use a lot of the new Java features. You can’t use the Functional Big Three (map, filter, reduce) while reading or writing from a database, because that is a side effect. And with the application I work with, there are a lot of loops within loops. And then sometimes even more loops inside those. Can the big three handle nested data structures? I haven’t done as much Clojure as I would like, but in the twitter-retriever app I did some calls to doseq and a LOT of loop-recur.

On the other hand, putting logic in the views has been a bad idea for a while, and does not require anything from Java 8 to change.

Other languages I am thinking about looking into are Go, Elixir and Kotlin.

May 2020 Update

I got a Pluralsight membership for a year. It was 30% off.

Right now I am going through some Java 8, Spring and microservices tutorials. My Java skills are out of date. After that, I think I will look at some Go again. There is one on microservices in Go that looks interesting.

I still would like to get through all the books in “The Little Schemer” series and then go through SICP. But sometimes I wonder if spending all that time on Scheme may have been a mistake. Perhaps I should have just been building some web apps in Clojure. Maybe going for “enlightenment” was not the best idea.

I have also been struggling with updating my laptop to the latest version of Ubuntu. I think the issue might be that I have snap disabled. There were a few stories on Hacker News that convinced me to disable snap (see here and here), and now I cannot upgrade.

For my next laptop, I plan on buying one with Linux pre-installed. I might get a Dell, but since they only offer Ubuntu I might go with another vendor. I am looking at System76 (submissions to Hacker News at this page) or Purism (Hacker News submissions at this page). They both offer systems with either Ubuntu or their own Linux. I think in both cases their OSes are derived from Ubuntu. Purism’s is PureOS, and System76’s is PopOS.

One thing I would really like is to be able to disable the mousepad. If I don’t attach a USB keyboard, I always hit the mousepad with my thumb. In all seriousness, who came up with that gesture anyway? It’s not like anyone ever lifts up their mouse and puts it down.

You’re welcome.

April 2020 Update

I spent time in April learning Go using the Go courses on Pluralsight. Pluralsight was free for April, so I decided to learn something new.

Starting function names with capital letters feels weird. It seems verbose. And using mutexes for concurrency feels like a step backwards. I don’t know if I will drop Clojure or the JVM for Go, but I will keep an open mind.

The Go tooling seems really nice. You can find race conditions before deploying. It has testing out of the box. It has a framework for robotics called Gobot. I do not know much about robotics, so I have no idea how Gobot ranks compared to ROS, which does have client libraries for Go (this is hosted on github, and does not appear to be part of the official ROS distribution) and Common Lisp (GitHub repo here).

It is something I will keep an eye on.

You’re welcome.

March 2020 Update

I have not made any more progress on “The Seasoned Schemer”.

My employer has offered some training in Java 8 and Spring Boot microservices, so I will spend time on that.

I may also start looking into building a web app with Grails. I would like to maintain my Groovy/Grails knowledge. I will probably build a quick app in Grails and then redo it in Clojure/Luminus. I will also look into making some rules with the Clara rules engine. One reason I want to make a web app is that I think it would be a good way to enter data for use by a rules engine.

You’re welcome.

Chapter 9 of ‘The Little Schemer’

I finally finished chaper 9 of The Little Schemer. This was about partial functions, total functions, and ended with the applicative-order Y combinator.

Frankly, what I understood in this chapter I did not see the point of, and I am not sure there is much point in the parts I do not understand. I think a total function returns a unique value for every input, and partial functions do not. Some of the functions they described as partial sometimes did not return values. Which to me sounds like a badly-designed function.

Maybe Godel’s incompleteness theorem has seeped into our collective consciousness to such a degree that the idea that some functions cannot be computed seems obvious.

Maybe I am not smart enough to be one of those super high-level math/language theory people. I realize I do not have the inclination to do so. I do not have much interest in conjectures about functions that do not return values. I will have to ask a few people I know who have been through SICP if this is the sort of thing in that book. I will finish The Little Schemer and go on to The Seasoned Schemer in a while.

I went through the part about the Y-combinator a second time, and I am not too sure I understand it or see the point. Is there a point? According to some guy named Mike, the point of the Y-combinator is:  The Y combinator allows us to define recursive functions in computer languages that do not have built-in support for recursive functions, but that do support first-class functions.

Are there languages that fit that description? I did some googling, and I found a Hacker News discussion about “The Why of Y” on Dreamsongs (an interesting website I hope to explore in depth someday). As one commenter points out, the article goes into the How of Y, but never the Why.

The Seasoned Schemer says you only need to understand the first eight chapters of The Little Schemer to go through TSS. So hopefully I should be fine. I was hoping to get through TLS more quickly. I am more busy at work, and chapters 8 and 9 were a lot harder (and had more concepts that were new to me) than the first seven.

I have not gone through these yet, but here are a few more links I found about total and partial functions:

You’re welcome.

Update on ‘The Little Schemer’

I am almost through with The Little Schemer. I got through the first seven chapters fairly quickly. There are no exercises per se. They will ask questions, invite you to figure out the answer, and show you. I admit, a few times I peeked, but only after trying to get it myself.

There were a few things in chapter 8 that were mind-bending. One was using higher-order functions for code-reuse.

In chapter 3, the authors introduce a few functions. One is “rember”, which removes an element (or member) from a list:

Another is “insertR”, which inserts a new element to the right of an old element in a list:

The third is “insertL”, which inserts a new element to the left of an old element:

The functions are pretty much identical. In chapter 8, they make a base function, and then make three more to cover the three different requirements.

It works just like the original:

This reminds me of something that Eric Normand showed in Purely Functional, where he puts anonymous functions in the values of a map. I wrote about that in August of 2018.

The truly mind-bending part was the “collectors”. Dark arts, these are.

They define a function that does the usual removing of members. It takes an additional parameter which is a function that does some calculations/manipulations on the resulting list. But in the recursive calls, the function uses inline lambdas to make additional collections.

Here is their explanation from page 140; the function is (multirember&co a lat f), where “a” is an atom, “lat” is a list of atoms, and “f’ is a function: It looks at every atom of the lat to see whether it is eq? to a. Those atoms that are not are collected in one list ls1 ; the others for which the answer is true are collected in a second list ls2 . Finally, it determines the value of (f ls1 ls2 ).

Here is the multirember&co function:

Here is the function we will use in our test:

The second argument is unused in last-friend. So when you use last-friend, it will count how many elements are not equal to the first argument. If it returned the length of y, it would tell us how many times the first argument is in the list.

Here are the tests in Racket:

Later, they use collectors on functions that work on multi-level lists. There is a recursive call within the collector.

I think I understand this, but I am not entirely sure about it. I was able to figure out some of the collector examples as they went along. Truly this is unholy power.

You’re welcome.

On To The Little Books

I finished the poker program from Chapter 15 of Simply Scheme, and I finished Chapter 24.

I have decided I am willing to let MengSince1996 be the King of Berkeley, and move on to Daniel Friedman’s “Little” books.

The first is “The Little Schemer“. This seems to deal with recursion. The first sequel is “The Seasoned Schemer“. The co-author of these books is Matthias Felleisen, one of the founders of Racket. On his Northeastern University page, he states that “The Seasoned Schemer” deals with higher-order functions, set! and continuations.

Next is “The Reasoned Schemer“. This bridges functional programming and logic programming. I work with a rules engine in my day job. I wonder if that is what this is about.

The next book is “The Little Prover“. According to the book’s page on the MIT Press site, this book “introduces inductive proofs as a way to determine facts about computer programs.” Honestly, I am not too clear what that means at this point.

The most recent book in the series is “The Little Typer“. For this book, Friedman and co-author David Thrane Christiansen made a language for the book using Racket.

I will probably use Racket for all these books instead of straight Scheme because I want to be able to run a tests and not type and re-type function invocations every time I make a change. In order to use tests, I had to require the R6RS packages into a program using “#lang racket/base”. I do not think there is a way to use the Racket unit test libraries in an R6RS program. So there are a lot of prefixes.

I like prefixes before function names. It is a bit more typing, and maybe it is not as clean, but I like to know where everything is coming from. One of the things that I did not like about Rails was there were no import or require statements. Everything was everywhere. That made tutorials look very interesting, but it was frustrating finding documentation for classes if you wanted to see what else the gems could do.

I plan on getting through these faster than I took to get through Simply Scheme.

See also my mentions of these books in a post from a few years ago.

You’re welcome.