Frectonz
4.03K subscribers
2.08K photos
92 videos
32 files
1.79K links
A place for me to talk about my projects, stuff i find on the internet and what I am thinking about.

By @frectonz
Download Telegram
Frectonz
I've completed "Scratchcards" - Day 4 - Advent of Code 2023 I did this one in golang. Part 2 took me way too long to figure out. [Part 1 Solution] [Part 2 Solution]
Notes

1. Don't use strings.Split() to split white space, instead use strings.Fields(), read more about it in the golang docs.
2. If you are iterating over an array while you are modifying it, do not use ranges, just do a regular for loop and use indexes.
🔥4
There has been a sudden uptake in subscribers of this channel, I think it's the similar channels thing that's causing it.
🔥10
Here is another tech telegram channel for yall.
I think he mainly does ios stuff,
Checkout his channel

@storyOfXcode
Frectonz
Photo
In my experience golang is a pretty meh language but no variable shadowing thing annoys me a lot.

Go needs to implement variable shadowing.

While you are programming you have some mental model of the data you're working on, let's say it's a list of numbers and you wanna sum them up.

Ok so the flow of the program is

1. You ask the user to provide the numbers. Let's say that the numbers are separated by commas.

You store the numbers in a variable you called nums
nums := getInput()
This will be holding our numbers as a comma separated string.

getInput is not a function in go, I just forgot how you read from stdin in go.

2. You split the input you got on commas, to extract the individual numbers you got.

Here's where things go wrong in a programming language that doesn't have variable shadowing. Ideally you would be able to do this

nums := getInput()
nums := strings.Split(nums, ",")
Instead you have to do this

nums := getInput()
numsSplit := strings.Split(nums, ",")
So what's the big deal, notice when I was talking about the "numbers" I was calling them "numbers" all the time, no matter what kind of data type they happen to be at the current stage. Notice how they were transformed from a comma separated string, into an array of strings, but we would still refer to them as "numbers".

That's why variable names are helpful, they name the higher order way we think about the data.

If go had variable shadowing we would still be able to call the array of strings we got after splitting it, nums and we wouldn't have needed to come up with the name numsSplit.

There is a reason why I am using the word "shadowing" here. You might be asking why not just re-assign the variable, like what we do in dynamic languages. That's not what I am talking about, re-assigning the variable would only change the data it's holding on to, shadowing would let you change both the data and type.

In vscode terms when I hover on nums from the first step, I should see string, in the second step nums would be []string.

3. At this point you would iterate over the nums and convert them from a string to an int.

With variable shadowing, you would still call this nums but now it would be holding on to a []int (an array of ints).

Without variable shadowing, you would be forced to come up with a new variable name like numsArray or even numsAsInts or sth like that.

4. In the last step you would just sum up numbers.
It's crazy how much we unironically say

"Constant Variable"

😂
😁11
Frectonz
I've completed "Scratchcards" - Day 4 - Advent of Code 2023 I did this one in golang. Part 2 took me way too long to figure out. [Part 1 Solution] [Part 2 Solution]
Only manged to do part 1 today.
I couldn't get part 2 to work, maybe i will try it again in the weekends or sth.

I did today's puzzle in Rust.

[Part 1 Solution] [Unfinished Part 2 Solution]
👍1