Posts

Showing posts from May, 2020

Sorting in Go – Don’t Reinvent This Wheel

Image
The post Sorting in Go – Don’t Reinvent This Wheel appeared first on Qvault . Sorting is a common task in programming, and for that reason most languages have a default sorting algorithm in their standard library. Go is one such language. Go has gone about providing sort functionality in one of the most elegant ways possible, via an interface. type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) } Any type that satisfies this interface can be sorted using the standard library’s sort.Sort() function. There is rarely a reason to sort any other way because the sort function is O(n log(n)) in the worst case. You can take a look at the various algorithms that are used, depending on the data to be sorted, here . Sorting a Slice The first thing

Using Concurrent Subscribers – RabbitMQ in Python (pika)

Image
The post Using Concurrent Subscribers – RabbitMQ in Python (pika) appeared first on Qvault . It’s a fairly common scenario to subscribe to a Rabbit queue and process messages before acknowledging receipt. The pika package for dealing with RabbitMQ in Python however is only single-threaded out of the box. If we want to make a network or database call before each acknowledgment our subscribers can get really slow. Waiting on i/o for each message means we likely can’t process more than a message or two per second. Let’s Get To It We will be using Python 3 for the following examples. You will also need to install pika via Pip. pip3 install pika Let’s start by specifying some configurations we will need: # some configuration variables RABBIT_URL = 'amqp://username:password@localhost' ROUTING_KEY = 'routing_key' QUEUE_NAME = 'my_queue.' + ROUTING_KEY EXCHANGE = 'exchange_name' THREADS = 5 RABBIT_URL is the connection string t

Don’t Go To Casting Hell; Use Default Native Types in Go

Image
The post Don’t Go To Casting Hell; Use Default Native Types in Go appeared first on Qvault . Go is strongly typed, and with that, we get many options for simple variable types like integers and floats. The problem arises when we have a uint16 , and the function we are trying to pass it into takes an int . We find code riddled with int(myUint16) that can become slow and annoying to read. Go’s basic types are: bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias for uint8 rune // alias for int32 // represents a Unicode code point float32 float64 complex64 complex128 There are 5 different types that can represent an integer, 5 types for an unsigned integer, 2 for a float, and 2 for a complex number. While it’s hard to defend the notion that the compiler itself has default types, the standard library certainly plays favorites. For example, the cmplx package which does math with complex numbers accepts and returns exclusively c

Leave Scrum to Rugby, I Like Getting Stuff Done

Image
The post Leave Scrum to Rugby, I Like Getting Stuff Done appeared first on Qvault . Scrum is a buzzword, the virtue signal of choice for middle-management in software organizations. If your goal as a manager is to implement a system by which you: Speed up the appearance of progress Pay for 2x the number of people you need Gather fine-grained data based on meaningless metrics Then Scrum is exactly what you are looking for! "Oh you had problems with Scrum at your last employer? Well, that's not real Scrum."- your scrum master, who is not a true Scotsman Click To Tweet Which brings us to our first problem… Problem #1 – Scrum Is Vague It’s hard to criticize Scrum. The idea of “Scrum” in my mind is likely very different from the one you are familiar with. This is by design, and by admission. In the official Scrum Guide we find Scrum’s definition: A framework within which people can address complex adaptive problems, while productively

Loops in Rust; Breaking From Nested Loops

Image
The post Loops in Rust; Breaking From Nested Loops appeared first on Qvault . Looping in Rust isn’t the same as standard C-style languages. The syntax is different and there are some powerful options that make looping easier. First, let’s go over some looping basics, then we will cover how to handle breaking and continuing in nested loops. Standard For-Loop fn main() { for x in 0..10 { println!("{}", x); } } Which prints: 0 1 2 3 4 5 6 7 8 9 0..10 is an iterator where the lower-bound is inclusive and the upper bound is exclusive. More generically: for var in iterator { // do stuff } In my opinion, all languages should move to a single syntax with for-loops based on iterators. The simplicity makes Rust’s loops easy to read, while the ability to create custom iterators makes it more powerful than even more verbose formats like Go’s: for i := 0; i < 10; i++ { fmt.Println(i) } Rust’s for-loop doesn’t specify wha

Variable Shadowing In Rust – “Let” Is Immutable But Not Constant

Image
The post Variable Shadowing In Rust – “Let” Is Immutable But Not Constant appeared first on Qvault . Let’s take a look at some of the common pitfalls with the keywords let and mut . Then, we will throw learn how immutable != constant by using variable shadowing . Getting started with Rust can be daunting. Rust is well-known for being a safe language. One of the ways in which Rust is safe is through type-safety. Rust is strongly typed and defaults to immutable values. The “let” Keyword The simplest way to create a new variable in Rust is by using the “let” keyword: fn main() { let my_num = 5; println!("{}", my_num); } let introduces a new variable into the current scope. By default new variables are immutable, which means they can’t be reassigned. For example: fn main() { let my_num = 5; my_num = 6; println!("{}", my_num); } fails to compile with the error: cannot assign twice to immutable variable In Rust t

Quantum Programming 101: Backend Monitor

Image
The post Quantum Programming 101: Backend Monitor appeared first on Qvault . Introduction In a previous tutorial we showed how you can get basic information on all quantum devices using backend_overview(). While this function is great to get information on all quantum devices at a glance it is not detailed on specific information such as qubit and gate errors. To get more detailed information on a quantum device (such as configuration and individual qubits and gates) you can use backend_monitor(). Implementation Unlike backend_overview() this is for getting information on a specific device so you have to pass the device name in to the function as an argument. For example to get real time information on the IBMQ Burlngton device you enter the following: backend_monitor(provider.backends.ibmq_burlington) and for another device like IBMQ Vigo: backend_monitor(provider.backends.ibmq_vigo) Steps Copy and paste the code below in to a python file Enter your

Concurrency In Rust; Can It Stack Up Against Go’s Goroutines?

Image
The post Concurrency In Rust; Can It Stack Up Against Go’s Goroutines? appeared first on Qvault . One of the primary goals of the Go programming language is to make concurrency simpler, faster, and more efficient. With Rust growing in popularity let’s see how its concurrency mechanisms stack up against Go’s. A Refresher On Goroutines In Go, concurrency is accomplished by spawning new goroutines : package main import ( "fmt" "time" ) func main() { go func() { for { fmt.Println("one second passed") time.Sleep(time.Second) } }() fmt.Println("waiting 10 secs for goroutine") time.Sleep(time.Second * 10) } In the example above, we use the go keyword to signify that we want to run the provided anonymous function in a goroutine. Execution at that point splits – execution continues on the main thread, but the runtime

Rust vs Go – Which Is More Popular?

Image
Go and Rust are two of the hottest compiled programming languages. I develop in Go full-time and love it, and I’m learning more about Rust recently – its an exciting language. Let’s explore some differences between the two and look at which is growing faster in the popularity polls. Popularity Stats According to the StackOverflow 2019 surveys , Go is ahead in the polls when it comes to programming and markup languages. However, compare that to the previous year: Rust wasn’t even on the chart just one year before. Go did grow by an impressive 1.6%, but it would seem Rust might be growing even faster as a percentage over time. Some more supporting evidence for the hypothesis that Rust is growing faster is another poll – the most loved languages survey : Rust is a clear leader here, but Go isn’t far behind. There is a lot of hype around Rust right now, for good reason. Let’s take a look at the most dreaded languages: No one dreads Rust. I suspect that