Pair/tuple data type in Go

You can do this. It looks more wordy than a tuple, but it’s a big improvement because you get type checking. Edit: Replaced snippet with complete working example, following Nick’s suggestion. Playground link: http://play.golang.org/p/RNx_otTFpk package main import “fmt” func main() { queue := make(chan struct {string; int}) go sendPair(queue) pair := <-queue fmt.Println(pair.string, pair.int) } … Read more

Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?

Tuples are great if you control both creating and using them – you can maintain context, which is essential to understanding them. On a public API, however, they are less effective. The consumer (not you) has to either guess or look up documentation, especially for things like Tuple<int, int>. I would use them for private/internal … Read more

What’s the difference between System.ValueTuple and System.Tuple?

What are ValueTuples and why not Tuple instead? A ValueTuple is a struct which reflects a tuple, same as the original System.Tuple class. The main difference between Tuple and ValueTuple are: System.ValueTuple is a value type (struct), while System.Tuple is a reference type (class). This is meaningful when talking about allocations and GC pressure. System.ValueTuple … Read more

How does std::tie work?

In order to clarify the core concept, let’s reduce it to a more basic example. Although std::tie is useful for functions returning (a tuple of) more values, we can understand it just fine with just one value: int a; std::tie(a) = std::make_tuple(24); return a; // 24 Things we need to know in order to go … Read more

Getting one value from a tuple

You can write i = 5 + tup()[0] Tuples can be indexed just like lists. The main difference between tuples and lists is that tuples are immutable – you can’t set the elements of a tuple to different values, or add or remove elements like you can from a list. But other than that, in … Read more

Convert a namedtuple into a dictionary

TL;DR: there’s a method _asdict provided for this. Here is a demonstration of the usage: >>> fields = [‘name’, ‘population’, ‘coordinates’, ‘capital’, ‘state_bird’] >>> Town = collections.namedtuple(‘Town’, fields) >>> funkytown = Town(‘funky’, 300, ‘somewhere’, ‘lipps’, ‘chicken’) >>> funkytown._asdict() OrderedDict([(‘name’, ‘funky’), (‘population’, 300), (‘coordinates’, ‘somewhere’), (‘capital’, ‘lipps’), (‘state_bird’, ‘chicken’)]) This is a documented method of namedtuples, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)