# Samrat Man Singh

Email: mail@samrat.me

Hi! I’m Samrat, a programmer from Nepal.

# Summary of recent reading(October-December 2020)

### 2021-01-01

• Storyworthy by Matthew Dicks: This is a book about how to tell better stories, by an award-winning Moth storyteller. The book also talks about how to find better stories from your life.

Besides surgically analyzing what goes into crafting a great story, the book is filled with great stories from the author’s own life and so is really entertaining to read.

• The Biggest Bluff by Maria Konnikova: This book documents the journey of a journalist who goes from knowing almost nothing about poker to becoming a pro player. It’s an enjoyable book which strikes a good balance between the poker lessons the author is learning and lessons that poker is teaching her about life outside of the game.

• Memoirs of an Imaginary Friend by Matthew Dicks: An enjoyable story, told from an interesting perspective. I read this shortly after Storyworthy(by the same author) so I found myself appreciating some of the story-crafting techniques show up in the book.

• Creative Selection by Ken Kocienda: An Apple insider’s account of the development of the iPhone, iPad and WebKit. The book feels like it could use a little more editorial touching up– there are some weird analogies and tangents– but on the whole I enjoyed reading this book.

Most stuff out there about iPhone-era Apple don’t have any substance beyond painting Steve Jobs as a visionary genius. While this book does show admiration towards Jobs, it is mostly an account of all the nuances that programmers like the author of the book were thinking about, and how the team there approached designing and building software.

# Summary of recent reading(July-September 2020)

### 2020-10-01

• Tiassa by Steven Brust: Part of the Vlad Taltos series. This one was really good. It is written from the perspectives of different characters in the story– not first-person like the other books in the series, but the focus shifts between different different people in each chapter, and you get to learn about other characters.

The story itself is also pretty cool. The Jhereg cook up a really large hoax– an imminent Jenoine attack– just to rope in Vlad, but Cawti prevents that from happening.

• Hawk by Steven Brust: This may have been the best book in the series so far. Spoilers ahead. Vlad is trying to make a deal with the Jhereg to stop trying to kill him. And it works. Sort of. There’s a good amount of suspense and the build up to the end is amazing. I’ve said this before, in an earlier summaries post, but man Brust sure can write. Even after being more than ten books in, each book still feels fresh.

• Vallista by Steven Brust: A mystery where Vlad has to figure out how a time-warping manor works, in order to rescue Devera.

• The Wandering Earth by Liu Cixin: a collection of short sci-fi stories from the author of The Three-Body Problem. All are thematically quite similar to each other(and to The Three-Body Problem) but they still feel quite fresh; I enjoyed all of the stories in the book.

• Julian by Gore Vidal: a historical novel around the life of the 4th century Roman emperor Julian. The book is written as his memoir, with some commentary by two philosophers who knew him. I enjoyed this book. I find historical fiction to be a really interesting genre and it seems pretty underappreciated. Creation by the same author is also a good read.

# Summary of recent reading(April-June 2020)

### 2020-07-04

• Deep Work(Audiobook) by Cal Newport: Somewhat of a re-read. I read most of the book a few years ago but never finished it. Like most of the books in the category, most of the value from reading this book comes just from getting nudged into assessing the things you value and improving your current systems even if just by a smidge.

• Jhegaala by Steven Brust: Entertaining read but I found the ending to be a bit unsatisfying.

• Iorich by Steven Brust: Another great story in the series. I don’t have anything specific to say about the book itself, but the way larger themes build across these books while the individual books are each feel “episodic” makes you really admire Brust for the way he has constructed this series.

# Summary of recent reading(January-March 2020)

### 2020-03-31

• The Start-Up of You by Reid Hoffman and Ben Casnocha: A career book by a LinkedIn cofounder. Emphasizes networking as an essential tool to advance your career; unsurprisingly advocates using LinkedIn to do this.

• Ultralearning by Scott Young: The book describes an approach to learning by immersing yourself in projects which will teach you the skills you are interested in. It also discusses incorporating techniques like retrieval practice, spaced repetition into the projects to speed up your learning process.

A lot of the learning techniques bit will be already familiar to you if you’ve read any other books about learning, but I do quite like the idea of project-based learning.

• Range by David Epstein: the main message of the book is that the world could do with a little less of everyone rushing to specialize and more of people and organization who cultivate breadth.

Most of the book reads like the typical non-fiction book with a lot of the same examples(like the Polgar sisters who also appear in Ultralearning above) but for a change, the author also talks about his own experiences and perspectives. The chapters talking about the value of improving what the book calls match quality– finding what kind of work suits you– were pretty good.

• Digital Minimalism by Cal Newport: Contrary to what I thought coming into the book, the suggestions in the book are not at all extreme. The book urges you to be intentional about how you use your smartphone and social media. It tries to make the case that when it comes to using these technologies there is actually a lot at stake– your mental wellbeing but also how these technoligies can distract you from the things you really value in life.

• The Undoing Project by Michael Lewis: biographical book about the Daniel Kahneman and Amos Tversky. Also, goes into their research into the systematic ways in which human minds make errors. I can seem why Lewis’s other books(Money Ball, Liar’s Poker, etc) are so popular– his writing style is very engaging. This is probably also a good book to read prior to diving into Kahneman’s Thinking, Fast and Slow, which I’ve been told requires some effort to read.

• The Wright Brothers by David McCullough: a biography of the inventors of the airplane. This is an excellent book bringing to life the characters involved. It also serves as a reminder of just how amazing of feat the brothers achieved not too long ago.

Among other things, excerpts from the letters the brothers wrote to each other and to other family members, and diary entries give a good glimpse into their minds and lives.

I highly recommend this book.

• The Idea Factory: Learning to think at MIT by Pepper White: the book is about the author’s experience at MIT as a graduate student. There are some points in the book where the author significantly expands his personal frontiers in learning to think; those parts of the book were most interesting.

# WebAssembly branching instructions, by example

### 2020-03-29

I have been familiarizing myself with WebAssembly by writing small WAT files and looking at the WASM output for some simple C functions. WebAssembly Studio is a great tool for doing this kind of exploration.

Branching in WebAssembly takes some getting used to– for one thing, the target of your branch instructions are relative to the current scope. That is, the target of the br 1 refers to the scope one level up from the current scope and the same target will be referred to as different indices depending on where the branch is being called from.

(block ;; <- this is the target block for the br below
(block ;; <- if you wanted to refer to this, you would use 0.
...
br 1
))

The other thing is that the branching behaviour is a bit different depending on whether the target is a block or a loop. If the target is a block, control exits from the block. While if the target is a loop, the loop continues.

I thought I’d jot down some notes on my understanding of how branching works in WASM along with some examples.

## Blocks

The branch instructions take an index– this index specifies the number of levels outward to go from the current scope:

(block
local.get 0 ;; get 0th local, and put into stack
i32.eqz ;; pop stack and check if it is zero, put result(boolean) into stack
br_if 0 ;; branch out of 0th block if top item in stack is true
i32.const 42
local.set 1)

As mentioned in the comment in the code block, if the 0th local is zero, control reaches the instruction after the block.

Let’s look at a function involving nested blocks. Suppose we want to encode the following logic in WASM:

if (x == 0)
return 42;
else if (x == 1)
return 99;
else
return 7;

One way to achieve this would be with the following function:

(func $foo (param i32) (result i32) (local i32) (block (block (block ;; x == 0 local.get 0 i32.eqz br_if 0 ;; x == 1 local.get 0 i32.const 1 i32.eq br_if 1 ;; the else case i32.const 7 local.set 1 br 2) i32.const 42 local.set 1 br 1) i32.const 99 local.set 1) local.get 1) If you actually compile the C or Rust equivalent of the code above, this is not the WASM you’ll get but this serves nicely for our purposes. Let’s start with the “else” case first. We set the local to 7 and branch out of the block two levels up. This is the outermost block, which makes the next instruction the local.get 1. So we get the local we just set and return. In the x == 0 case, we branch out from the innermost block, and the next three instructions are: i32.const 42 local.set 1 br 1 Again we set the local to 42. And the index we pass to br is 1. Remember though that we branched out from a block, so branching out with index 1 means we are branching out of the outermost block again. ## Loops A branch instruction referring to a loop behaves a bit differently– if the index passed to a branch instruction is a loop, control passes to the beginning of the loop instead of exiting as happens in a block. Here is an example showing the iterative factorial algorithm: ;; int result = 1; ;; while (i > 0) { ;; result = result * i; ;; i = i - 1; ;; } (func$iterFact (param i64) (result i64)
(local i64)
i64.const 1
local.set 1
(block
local.get 0
i64.eqz
br_if 0
(loop
local.get 1
local.get 0
i64.mul
local.set 1
local.get 0
i64.const -1
i64.add
local.tee 0
i64.eqz
br_if 1
br 0))
local.get 1)

Let’s focus on just the branching statements.

The first br_if, outside the loop exits the block if the argument passed is 0.

The br_if inside the loop also exits the block(and hence also the loop), although note that this time the same block is referred to as index 1 instead of 0.

The final br 0(the penultimate line in the code block above) branches to the beginning of the loop, thus continuing the loop.