Newsletter 7 – 10/2020

This month we have a lot of of good posts and great news. A good month for those who love to read.

Featured

Hacktoberfest 2020 – Don’t forget to do your registration to Hacktoberfest 2020 if you want to participate.

GitHub CLI 1.0 is now available – GitHub CLI can help you a lot, it can open PR, list issues, and review PRs! It looks amazing, and if you like me is a guy that loves doing everything from the terminal, you’ll love it.

WEB HISTORY by Jay Hoffmann – This series of posts is telling the web history and it’s fantastic for those who love tech history! Don’t wait to start reading because the posts are huge! The last post you can find here: Chapter 4: Search

The Life of a Data Byte I’ve recommended a similar post from the same author on my first newsletter, if you didn’t read yet, what are you waiting for? Just read it, it’s fantastic.

NVIDIA to Acquire Arm for $40 Billion, Creating World’s Premier Computing Company for the Age of AI – NVIDIA has acquired ARM. Recently good news is coming around ARM, on our August edition we already talked about it because Apple will start using ARM in its processors. This movement from NVIDIA put them in a nice position on IA development.

Supporting Linux kernel development in Rust – Post talking about the challenge of adopting Rust on Linux kernel development, including some points of may need attention. It’s a simple post with some parts technical.

If everyone hates it, why is OOP still so widely spread? – In recent years there has been much discussion around OOP and the rising of functional programming. This post takes a brief around some of the object-oriented advantages and historical background.

AVIF has landed – A new image format for the web was released and the quality of the images looks amazing.

Can one person run an open source project alone? – Every open source project has a governance model, the most famous model is BDFL, this fabulous post discuss some models, its advantages, and drawback. A good read for those who like or make contributions to open-source projects.

Under Deconstruction: The State of Shopify’s Monolith – A excellent post from Shopify engineering describing the current state of Shopify’s monolith deconstruction, for those who love a post with architectural things, it’s a delicious cake.

A Picture of Java in 2020 – A great report from JetBrains about java ecosystem in 2020.

Triage with Me – 11 issues & 2 PRs in 1.5 hours – If you are interested in start contributing to open source, triage issues/PRs can be a good place do start. In this video the creator of Code Triage shows how he do this task while recording everything, in 1.5 hours dealing with 11 issues and 2 PRs for a not well-known code base. This is a wonderful material.

MISC

Why I Actively Discourage Online Tooling like jwt.io and Online JSON Validators – I always use online tools like JSON/YML validators. This post made me start thinking, and probably next time I’ll think twice before validating anything in this kind of service.

Seamlessly Swapping the API backend of the Netflix Android app – Netflix swapped the android app backend from a monolith service to a totally new microservice (written from the ground), and no user ever noticed the difference. This post explains how was the process of migrating and how they tested it to grant 100% compatibility.

Writing a Ractor-based web server – Ractor is finally coming and people are starting building things using it. This post describes how to implement a Ractor-based web server.

Database of Databases – Want to have a quick introduction to some database? This site offers a nice summary of any database.

Make A Language. Part One: A basic parser – A nice tutorial about writing your own language in Rust. This series of post in under development, but already looks amazing.

Where TDD falls short, find yourself a good REPL – TDD is awesome and everyone knows it (or should know), but sometimes you need some… complement, and the REPL is the right tool to help you TDD, especially when dealing with code that you don’t know.

Why senior engineers get nothing done – You don’t need exactly be a senior engineer, if you know a lot about some business of your company probably you start to get nothing done, because your time will be spent in other things. This post talks a bit about this problem that engineers face, and simple practices to help you.

Old, Good Database Design – This one it kind “back to basics”, it reviews some concepts around database constraints and why it’s necessary.

Interview with José Valim, creator of Elixir – A nice interview with Valim, the creator of Elixir. It was a very nice conversation and includes some curiosities about Elixir, like, where the name Elixir come from?

Simple & effective G1 GC tuning tips – If you work with Java applications, that post is for you! Simple and effective tunings to your G1 GC.

Clojure Journey VI – Data

Data is the core of all computer application, an information system basically receives information as input, executes operations and produces some output. Data is the core of computation, we execute operations using data, and as programmers we need to have ways of represent data at computer-level.

With this requirement, engineers and computer language hackers, created a way of represent data at software level classifying them in different types, and this type will represent a range of possible data and the way that it will behave when dealing with hardware.

All programming languages has a set of datatypes to deal with basic data, and imagine a language without them is almost impossible, Clojure of course has set of datatypes.

This post can look kind of boring but it’s necessary to study it before we start learning about functions in the next posts. It’ll do my best to this post don’t look boring and be short.

Hosted Types?

As a hosted language on JVM, most of its datatypes are from Java, and as we know, its heavy-tested for more 20 years, with excellent performance and interoperability, as we will see, Clojure strings are java.lang.Strings and so on.

To check type of something in Clojure, just use the function type, let’s start exploring with numbers, Clojure has it’s integer numbers that we already used in our journey when dealing with arithmetical operations:

=> (type 1)
java.lang.Long

As we see, the type is mapped to a Java type (remember that Clojure is build on top of JVM), other common types behave like this:

=> (type "Hello String")
java.lang.String
=> (type true)
java.lang.Boolean
=> (type 42.42)
java.lang.Double
Clojure primitives are java types

As supposed, you can use string to represent text, boolean to represent true/false and double to represent fractional numbers.

Numbers

Most of languages includes a float type, but as you should know, floats is not good enough, so, doubles are default in Clojure.

=> (type (+ 41.0 1.0))
java.lang.Double

floats are not banned at all, and Clojure has it, but it’s not the default, if you want to use it, you need to make a type conversion:

=> (type (float (+ 41.0 1.0)))
java.lang.Float

Just to keep talking about math, Clojure has a type that is not common on most languages, it’s called ratio that is useful to represent fractions:

=> (type 1/3)
clojure.lang.Ratio

And as we already seen, we represent integer numbers with integer type:

=> (type 1)
java.lang.Long

We can do mathematical operations on number as expected, just remember to use the polish notation, as we see in the last post.

=> (- 52 10)
42

One of the most important things to note here, is that if one number is fractional, the result is fractional, but you can do operations with integers and doubles interacting.

=> (- 52.0 10)
42.0

Text

You can represent strings in Clojure just putting any text inside double quotes:

=> (type "I'm a text")
java.lang.String

With the function str you can convert almost everything in String, just try:

=> (type (str 1))
java.lang.String

Clojure has it character type too, just type \ before a single char:

=> (type \t)
java.lang.Character

Logical Values

Logical values are represent by pure true and false, no complexity here!

user=> (type true)
java.lang.Boolean
user=> (type false)
java.lang.Boolean

And do logical operations with it too:

=> (and false false)
false
=> (and true true)
true
=> (or false true)
true
=> (not true)
false

In Clojure we only have two false values false and nil that we will see next.

Keyword

Keyword is a difficult topic to explain, folks coming from Ruby or lisp already know what I’m talking about. They look like string, but aren’t string, but are dedicated to be used as identifiers, they are just themselves, and you common will see as a map key for example.

They start with : fallowed by a text:

=> (type :keyword)
clojure.lang.Keyword

On the next post we’ll see more about keyword when dealing with maps.

Symbols

Symbols usually refers to something else, like functions, values. During the eval process they are evaluated to the value that its referring. To better understand it, let’s do and example.

We define a variable called hello, with the String “Hello Otavio”:

=> (def hello "Hello Otavio")
#'user/hello

Now if we just print the hello, as expected it will the string binded.

=> (println hello)
"Hello Otavio"

And if we check the type of hello:

=> (type hello)
java.lang.String

It will return the type of what is binded to hello and not the type of it, this occurs because evaluator first eval hello and look for what is binded, before call type. To check the true type of hello we need to delay the evaluation as we saw in the last post:

=> (type 'hello)
clojure.lang.Symbol

And that is our symbol. We already used a lot of symbols in this post, other example is the functions that we used:

=> (type 'println)
clojure.lang.Symbol
=> (type '+)
clojure.lang.Symbol
=> (type '-)
clojure.lang.Symbol

As we will see in future, symbols can have a namespace separated from the name by forward slash.

Fun fact from Clojure doc: There are three special symbols that are read as different types – nil is the null value, and true and false are the boolean values.

Regex

Regex has its own datatype in Clojure too, to represent a regex just use a hash fallowed by a string:

=> (type #"regex")
java.util.regex.Pattern

And with no surprise we will have a java datatype Pattern represented.

I will not dive deep into how to use regexs, but in the standard library we have good functions already built to deal with them, check this post if you want to learn more.

And Finally…The emptiness, the void, the villain!

Pay attention on nil monster or it will destory you!!

We’re talking about nil, the value that represent nothing, the emptiness. For those who already code something, you probably know it, in some languages are called Null, nil or None, it can be called a billion dollar mistake (watch this excellent talk anyway).

The rule is simple, nil is type nil and to represent nil just type nil:

=> (type nil)
nil

Next steps

On the next post we will see the collections, and understand each one and its use cases, for now to don’t extend this post we’ll finish here.

As a nice exercise for this post, check Clojure cheatsheet and you’ll see nice functions to deal with these datatypes.

Conclusions

Clojure has simple datatypes, and a uncommon datatype called ratio, one of the interesting things its that Clojure uses Double by default, so we avoid to use Floats.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Clojure Journey V – Simplicity, Evaluation and Syntax

Most of the time that I heard someone talking about Lisp family languages, probably was something like “that ugly language, why people program with that?”, “why so many parentheses?”, unfortunately since I started to learn Clojure, I hear it twice more.

This post is the fifth of a long journey on learning Clojure, and the first to really talk about code, but it can be served to learn about any language of the LISP family, this post will talk about syntax of the language, and the most important, we’ll understand why LISP languages look like this, the philosophy behind.

When you learn every language, most of the books in the first chapters is like a cake recipe, a long and boring list of all statements of the language, control flow, loops, var declarations, and much more.

Back to the day that you started to learn programming, probably you started with simple math and strings:

1 + 1;
=> 2
"Hello World";
=> "Hello World"

After that you learned about variables:

var age = 23;

Some control flow:

if (10 > 5) {
  "Hello World"  
}

And the most strange thing in every language, the for loop:

for (var i = 0; i < 10; i++) {
  console.log(42)
}

Probably you’re questing yourself about what I’m talking about, but have you looked at this common languages statements and noticed that they didn’t follow any logic or consistency in the syntax? The traditional for statements is not simple, we’re just used to view it all day. These syntax is not simple, and that’s the point.

Simplicity

In LISP family languages all the code follows the same logic, and after you’re capable of understand one expression, you’ll be able to understand the entire language. It doesn’t have many special forms, or something different from the rest, and that’s why “simplicity”is one of the worlds in this post title! Let’s take a look at some Clojure code, starting from the classical:

=> (println "Hello World")
Hello World

Let’s see some arithmetic:

=> (+ 1 2 3 4)
10
=> (= 1 2 3)
false
=> (+ (* 2 2) (* 3 3))
13

Can you imagine how if looks?

=> (if (> 2 1) "Greater" "Smaller")
"Greater"

What all these expressions have in common? They follow the same pattern. Let’s take a look:

LISP family syntax

In the image, the expression xxx is read as a list, the first element can be called “function position”, and the rest as arguments. When evaluating it will always try to evaluate the first item as a thing to invoke (you can think like a function, but sometimes can be other things like macros), and the rest as arguments the invoked.

Positioning the “function” before its arguments it’s called prefix/polish notation, the expression (f x y) looks a lot like the traditional mathematics notation f(x, y). All lisp expressions use polish notation.

Evaluation

During the process of evaluating expressions, it will evaluate each argument of the expression and then invoke the function of the first position with the result of evaluating each argument, in other words, it will evaluate all expressions from the innermost to the outermost recursively, forming a evaluation tree. Let’s assume (+ (* 2 2) (* 3 3)) as our example:

Evaluation Tree

For those who already studied programming languages design probably realized that this evaluation tree looks a lot like an AST (Abstract Syntax Tree), and it’s kind off.

You may have noticed that the numbers in the bottom of the evaluation tree are evaluating themselves, and it leads us to another important topic.

Different from other languages, Clojure doesn’t have statements, it has only expressions that evaluates to a value, and its programs is composed of n expressions. Every form not handled specially by any macro or special forms is considered a expression by the compiler and it will be compiled and has it’s value returned.

Most literal Clojure forms evaluate to themselves, except symbols and list, that’s why numbers was evaluated to themselves in the evaluation tree image. Other common examples of forms that evaluates to themselves are, booleans, strings, characters and keywords. Symbols are resolved based on a set of rules, but in general, it will look for what was “binded” to the symbol.

=> "String"
"String"
=> 42
42
=> :keyword
:keyword

It will try to evaluate everything, but we have a way to delay this process and say “don’t evaluate it, just return it raw”, just put an apostrophe before your expression:

=> '+
+
=> '(+ 1 2)
(+ 1 2)
=> 'variable
variable

Syntax

If you read the text so far, probably you already know how to write an expression and how literals are evaluated, you also know how to delay a evaluation using the apostrophe, now we can talk about one of the most interesting thing in LISP family languages, the “code as data and data as code” foundation.

Languages that are from LISP family, have a data structure called list, lists are basically zero or more forms enclosed in parentheses (1 2 3 42) is a example of one list with integers 1, 2, 3 and 42, and now comes the plot… Other examples of list is (+ 1 2 3) or (println "Hello World").

OMG! It’s a LIST

All the programs are composed of lists, and LISP/Clojure is defined in terms of evaluate its own data, and not a stream of keywords like other languages. LISP stands for LIST-Processing, and it explain this name.

Languages that have its programs represented by its own data are called “homoiconic”, and LISP family languages are one of them, based on this the affirmation “code is data and data is code” emerges and it is commonly used when talking about Lisp/Clojure.

When writing Clojure probably you’ll use lists as a data structure to store data, but if you try to use it to store a list of integers, probably you’ll get error during the evaluation proccess:

=> (1 2 3 4)
Execution error (ClassCastException) at user/eval2018 
(REPL:1)
java.lang.Long cannot be cast to clojure.lang.IFn

If we take a look at the error, java.lang.Long cannot be cast to clojure.lang.IFn, this error is saying that a integer can’t be cast as function and this error occurs because we tried to evaluate a list without any function in the first position, by default when evaluating any list, the Clojure reader tries to find a thing to invoke in the first position, that’s why when evaluating a list just as data structure, clojurists use the apostrophe to tell Clojure to not try to evaluate it:

=> '(1 2 3 4)
(1 2 3 4)
=> '(+ 1 2 3 4)
(+ 1 2 3 4)

And that’s the way that you usually will see programmers using lists as data inside Clojure programs, in a future post we’ll talk about how to use it.

Let’s finish our study about syntax with an experiment, Clojure has a function in it’s core named eval, if we pass a valid list, it will evaluate it:

(eval '(+ 1 2 3))
=> 6

This is the core of the REPL, and with Clojure simplicity, we can write our own in a simple way:

(loop [] (println (eval (read))) (recur))

Combining a function to read user input read, one to eval this input eval, print the result of evaluation with println and finally loop infinitely doing it. Run it and you’ll have your own read-eval-print-loop (REPL).

Invoking REPL

Conclusions

This a long post but essential on our journey, we ha been introduced to Clojure syntax and the logic behinds it. We learned about the famous “code is data and data is code” jargon, beautiful behind lists and built our own REPL.

LISP/Clojure syntax is beautiful and simple, it can afraid people because it’s different from what we’re used to, but if we study we start to understand it fast!

Now that we know the syntax, we can take a look in data types on the next post, and after that advance to functions. Some topics are missing in this post like special forms, but in a future post we’ll talk about it.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Useful Links

Evaluation, Clojure docs

The Reader, Clojure docs

Syntax, Clojure docs

Homoiconicity, Wikpedia

Polish Notation, Wikpedia

S-expressions, the Syntax of Lisp

Lecture 1A: Overview and Introduction to Lisp

Why code-as-data?

Hitchhiker’s Guide to Clojure

Newsletter 6 – 09/2020

Featured

Amazon Braket, Explore and experiment with quantum computing – Quantum computing is coming and Amazon releases it quantum computing service, now is just try yourself.

What every developer should know about database consistency – A simple and illustrated tutorial about database consistency, if you want to learn more abut databases and consistency models, you can start there.

How to use the Rust compiler as your integration testing framework – This post brings a nice point of view, with rust’s strong type system and some tricks you can avoid integration tests and let rust compiler is your integration framework, just write unit test and ship! This point of view can create a nice discussion, what’s your opinion?

Getting started with contributing to open source – A great post with nice tips to start contributing to open source project, if you want to get started it can help you a lot.

Categories for the Working C++ Programmer – A nice post explaining the basics of category theory to programmers. It has examples in c++ too, and for surer it’s worth reading it.

Things You Want to Do in Git and How to Do Them – It’s always good to review the basics, and make it fresh again in your head, this post can looks too basic, but it reviews simple concepts about git, about how to do a revert or a rebase correctly.

Peter’s Adventures in Ruby: Garbage Collection in Ruby – Nice post talking about how Ruby garbage collector and memory allocation works. Peter is writing a nice series about ruby internals too, check it out.

Misc

New Case Studies About Google’s Use of Go – A short and simple post by Rob Pike talking about Go use cases inside Google.

Rachel by the Bay · Tripping over the potholes in too many libraries – When should use a library and when should just code it? This is a interesting question, some people defend the idea that always install library and some people are more purist and defend the idea of always code simple logic. This is a short opinionated post, but bring’s the question to us.

Ractor – Ruby’s Actor-like concurrent abstraction – Ruby’s Ractor is close to be released (With Ruby 3 in December), and a nice preview (and tutorial) is given in this document.

Scientists rename human genes to stop Microsoft Excel from misreading them as dates – I don’t have much to say about that news, it’s surprising!

When Should You NOT Use Rails? – When use x or y framework is a common discussion among teams and developers. This opinionated post brings a nice point of view about when you should or not use Rails framework.

How Sidekiq really works – Sidekiq is a popular library to do background processing in Ruby. Is very common to find an application using the traditional sidekiq + rails. This post explains in a easy way how it works under the hood. (I love posts explaining things under the hood).

Effective Debugging of Memory Leaks in Ruby – Sometimes find a memory leaks is not a rocket science, sometimes. This post is short and it tells you how to find memory leaks in ruby applications with simplicity.

Upgrading GitHub to Ruby 2.7 – Keep your applications up to date is important in every language, this post talks a little about how GitHub they did update their Ruby version.

Mocking Techniques for Go – Every time that I needed to made a mock in Go I had difficulties, this post is very clearly about how to do great mocks, with different techniques. Its a kind of post that I save to consult later.

Write your Own Virtual Machine – Tutorial to write your own virtual machine to run assembly language in only 250 lines of C code? That’s great and interesting!

Implementing traceroute in Gotraceroute is a very common tool when debuggind network things, this post explain how traceroute works under the hood and help you make your own implementation using Go.

5 Elixir Tips Learned in Code Review – Five simple tips to Elixir programming, this post is not a rocket science, but its useful.

Check my last posts:

Follow my blog to get notified every new post: