Timmy Xiao

home · blog

Untitled 2024

2024-01-02

Happy new year! A semi not important brain dump mainly from December 2023 that I wanted to write down.

defaultdict(int) vs Counter

A difference that I discovered between defaultdict(int) and Counter that caused a problem during my attempt on an Advent of Code solution was that Counter doesn't add a key when you query a missing key. However, defaultdict(int) does!

Generating a new shared pointer of this in C++

I made a ray tracer recently. One of things that I wanted to do is to create a new shared pointer from a shared pointer of an object. You can do that with std::enable_shared_from_this.

Tangent: here is a cool discussion I found about affine and vector types in Rust!

Moving multiple lines of text in Neovim

A feature that I saw in Eclipse (probably other editors support this) that I wanted to replicate in Neovim is to move up/down multiple lines of text. It is different from yanking and putting as moving is something you can see in-place.

Here it is:

vim.keymap.set('v', '<A-j>', ":m '>+1<CR>gv=gv")
vim.keymap.set('v', '<A-k>', ":m '<-2<CR>gv=gv")

This binds moving selected text in visual mode to Alt-j and Alt-k.

vim.o vs vim.opt

In Neovim, vim.o.* and vim.opt.* are different! I thought they were the same. vim.o is a string and vim.opt is a table which allows more idiomatic lua code! I should maybe move from vim.o to vim.opt when I am bored.

I discovered this difference when I wanted to add something to listchars. vim.o.listchars += eol:↲ didn't work! From my discoveries, in vimscript, set listchars+=eol:↲ seems to add an comma which doesn't happen in lua. Therefore I opted (aha) to use vim.opt.listchars:append({ eol = ↲ }) instead.