What does range or map return? - dictionary

Go has very neat multiple return values paradigm. But It looks like v, ok := map[key] and v, k := range m use different mechanism with same notation. Here is a simple example:
func f2() (k, v string) {
return "Hello", "World"
}
func main(){
k := f2() // Doesn't work : multiple-value f2() in single-value context
m := map[string]int{"One": 1}
// It works
v, ok := m["One"]
// How it all work?
v := m["One"]
for k := range m {}
}
In above example, k := f2() gives error as f2 returns two values, whereas v, ok := m["One"] and v := m["One"] - both expressions work without any error.
Why is that different behavior?

A fetch from the built in map, using range on a map, array or slice, and also type assertions allows for one or two variables. This is not the case for user defined functions and methods. If a function declares two return values, you must tell what to do with both of them, or ignore both:
k, _ := f2() // Specify what to do with each returned value
f2() // Ignoring both
Why? Because the specification says it is so:
Map (indexed expressions):
An index expression on a map a of type map[K]V may be used in an assignment or initialization of the special form
v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]
where the result of the index expression is a pair of values with types (V, bool). In this form, the value of ok is true if the key x is present in the map, and false otherwise. The value of v is the value a[x] as in the single-result form.
Range (for statement):
For each iteration, iteration values are produced as follows:
Range expression: m map[K]V
1st value: key k K
2nd value (if 2nd variable is present): m[k] V
Type assertion:
For an expression x of interface type and a type T, the primary expression
x.(T)
asserts that x is not nil and that the value stored in x is of type T.
and
If a type assertion is used in an assignment or initialization of the form
v, ok = x.(T)
v, ok := x.(T)
var v, ok = x.(T)
the result of the assertion is a pair of values with types (T, bool)

Related

SML Create function receives list of tuples and return list with sum each pair

I'm studying Standard ML and one of the exercices I have to do is to write a function called opPairs that receives a list of tuples of type int, and returns a list with the sum of each pair.
Example:
input: opPairs [(1, 2), (3, 4)]
output: val it = [3, 7]
These were my attempts, which are not compiling:
ATTEMPT 1
type T0 = int * int;
fun opPairs ((h:TO)::t) = let val aux =(#1 h + #2 h) in
aux::(opPairs(t))
end;
The error message is:
Error: unbound type constructor: TO
Error: operator and operand don't agree [type mismatch]
operator domain: {1:'Y; 'Z}
operand: [E]
in expression:
(fn {1=1,...} => 1) h
ATTEMPT 2
fun opPairs2 l = map (fn x => #1 x + #2 x ) l;
The error message is: Error: unresolved flex record (need to know the names of ALL the fields
in this context)
type: {1:[+ ty], 2:[+ ty]; 'Z}
The first attempt has a typo: type T0 is defined, where 0 is zero, but then type TO is referenced in the pattern, where O is the letter O. This gets rid of the "operand and operator do not agree" error, but there is a further problem. The pattern ((h:T0)::t) does not match an empty list, so there is a "match nonexhaustive" warning with the corrected type identifier. This manifests as an exception when the function is used, because the code needs to match an empty list when it reaches the end of the input.
The second attempt needs to use a type for the tuples. This is because the tuple accessor #n needs to know the type of the tuple it accesses. To fix this problem, provide the type of the tuple argument to the anonymous function:
fun opPairs2 l = map (fn x:T0 => #1 x + #2 x) l;
But, really it is bad practice to use #1, #2, etc. to access tuple fields; use pattern matching instead. Here is a cleaner approach, more like the first attempt, but taking full advantage of pattern matching:
fun opPairs nil = nil
| opPairs ((a, b)::cs) = (a + b)::(opPairs cs);
Here, opPairs returns an empty list when the input is an empty list, otherwise pattern matching provides the field values a and b to be added and consed recursively onto the output. When the last tuple is reached, cs is the empty list, and opPairs cs is then also the empty list: the individual tuple sums are then consed onto this empty list to create the output list.
To extend on exnihilo's answer, once you have achieved familiarity with the type of solution that uses explicit recursion and pattern matching (opPairs ((a, b)::cs) = ...), you can begin to generalise the solution using list combinators:
val opPairs = map op+

How to convert a map to a slice of entries?

I'm trying to convert key-value map to slice of pairs, for example given a map like:
m := make(map[int64]int64)
m[521] = 4
m[528] = 8
How do I convert that into a slice of its entries, like: [[521, 4], [528, 8]]
I'm thinking about ranging over all those key-values then create slice for that, but is there any simple code to do that?
package main
import "fmt"
func main() {
//create a map
m := map[int64]int64{512: 8, 513: 9, 234: 9, 392: 0}
//create a slice to hold required values
s := make([][]int64, 0)
//range over map `m` to append to slice `s`
for k, v := range m {
// append each element, with a new slice []int64{k, v}
s = append(s, []int64{k, v})
}
fmt.Println(s)
}
Go 1.18
It is now possible to write a generic function to extract all key-value pairs, i.e. the map entries, with any key and value types.
Notes:
the map iterations are still unordered — using generics doesn't change that.
the constraint for the map key must be comparable
type Pair[K, V any] struct {
First K
Second V
}
func Entries[M ~map[K]V, K comparable, V any](m M) []Pair[K, V] {
entries := make([]Pair[K, V], 0)
for k, v := range m {
entries = append(entries, Pair[K, V]{k, v})
}
return entries
}
The type Pair here is used to preserve type safety in the return value. If you really must return a slice of slices, then it can only be [][]any (or [][2]any) in order to hold different types.
If the map key and value have the same type, of course you can still use Pair but you can also use a type-safe variation of the above:
func Entries[T comparable](m map[T]T) [][2]T {
entries := make([][2]T, 0)
for k, v := range m {
entries = append(entries, [2]T{k, v})
}
return entries
}
Again, T must be comparable or stricter in order to work as a map key.
Playground: https://go.dev/play/p/RwCGmp7MHKW

Difference between `go print(v)` and `go func() { print(v) }()`?

Here is the code:
type field struct {
name string
}
func print(p *field) {
fmt.Println(p.name)
}
func fix1() {
data := []*field{{name: "one"}, {name: "two"}, {name: "three"}}
for _, v := range data {
go print(v)
}
time.Sleep(time.Millisecond * 200)
}
func wrong1() {
data := []*field{{name: "one"}, {name: "two"}, {name: "three"}}
for _, v := range data {
go func() {
print(v)
}()
}
time.Sleep(time.Millisecond * 200)
}
func main() {
wrong1()
}
As far as I understand, all goroutines in function wrong1 share the same local variable v. At the moment of a goroutine execution, the value of v may be equal to any value in data, therefore the function prints random data three times.
However, I am failing to understand why function fix1 behaves differently (it prints each value in data exactly once).
wrong1(): go func() { print(v) }()
Go: Frequently Asked Questions (FAQ)
What happens with closures running as goroutines?
Some confusion may arise when using closures with concurrency.
Consider the following program:
func main() {
done := make(chan bool)
values := []string{"a", "b", "c"}
for _, v := range values {
go func() {
fmt.Println(v)
done <- true
}()
}
// wait for all goroutines to complete before exiting
for _ = range values {
<-done
}
}
One might mistakenly expect to see a, b, c as the output. What you'll
probably see instead is c, c, c. This is because each iteration of the
loop uses the same instance of the variable v, so each closure shares
that single variable. When the closure runs, it prints the value of v
at the time fmt.Println is executed, but v may have been modified
since the goroutine was launched.
To bind the current value of v to each closure as it is launched, one
must modify the inner loop to create a new variable each iteration.
One way is to pass the variable as an argument to the closure:
for _, v := range values {
go func(u string) {
fmt.Println(u)
done <- true
}(v)
}
In this example, the value of v is passed as an argument to the
anonymous function. That value is then accessible inside the function
as the variable u.
Even easier is just to create a new variable, using a declaration
style that may seem odd but works fine in Go:
for _, v := range values {
v := v // create a new 'v'.
go func() {
fmt.Println(v)
done <- true
}()
}
Your wrong1 example,
for _, v := range data {
go func() {
print(v)
}()
}
Playground: https://play.golang.org/p/0w86nvVMt1g
Output:
three
three
three
Your wrong1 example, creating a new variable,
for _, v := range data {
v := v
go func() {
print(v)
}()
}
Playground: https://play.golang.org/p/z5RCI0ZZU8Z
Output:
one
two
three
Your wrong1 example, passing the variable as an argument,
for _, v := range data {
go func(v *field) {
print(v)
}(v)
}
Playground: https://play.golang.org/p/1JVI7XYSqvv
Output:
one
two
three
fix1(): go print(v)
The Go Programming Language Specification
Calls
Given an expression f of function type F,
f(a1, a2, … an)
calls f with arguments a1, a2, … an. Except for one special case,
arguments must be single-valued expressions assignable to the
parameter types of F and are evaluated before the function is called.
Go statements
The function value and parameters are evaluated as usual in the
calling goroutine.
Your fix1 example, evaluating the value of v before the function is called,
for _, v := range data {
go print(v)
}
Playground: https://play.golang.org/p/rN3UNaGi-ge
Output:
one
two
three

Assign result of reflect.AppendSlice to pointer

I have troubles translating this piece of code, which is effectively a left rotate on a slice, into a more generic version which accepts interface{} as an input parameter.
func rotate(a *[]int, i int) {
x, b := (*a)[:i], (*a)[i:]
*a = append(b, x...)
}
I have troubles with the final assignment:
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a)
x, b := v.Elem().Slice(0, i), v.Elem().Slice(i, v.Elem().Len())
*a = reflect.AppendSlice(b, x)
}
The error message is invalid indirect of a (type {}). The value of a is interface{}, hence *a = would be to assign the right-hand value to the space where the pointer is pointing to. My call to AppendSlice returns Value though. I am not sure where the type assertion needs to happen, I suppose on the left-hand side?
a is an interface{} not a pointer, so you can't dereference it. Even if you have a pointer to a slice, you can't assigned the result ofreflect.AppendSlice, because it returns the type reflect.Value. You need to set the value via Value.Set.
https://play.golang.org/p/JCF8jsRJ_O
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a).Elem()
x, b := v.Slice(0, i), v.Slice(i, v.Len())
v.Set(reflect.AppendSlice(b, x))
}
Eliminate the use of the reflect package in Go 1.18 and later by using generics:
func rotateSlice[T any](a []T, i int) []T {
x, b := a[:i], a[i:]
return append(b, x...)
}
Call it like this: x = rotateSlice(x, 2)
Here's now to implement rotate using the reflect package.
Use Value.Set to set the value in the slice.
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a).Elem()
x, b := v.Slice(0, i), v.Slice(i, v.Len())
v.Set(reflect.AppendSlice(b, x))
}
Call the function with a pointer to a slice:
a := []string{"a", "b", "c", "d"}
roateSlice(&a, 2)
Run all of the examples on the playground.

what is the purpose of using ":" in OCaml function declaration

What is actually meant by the OCaml statement?
let func (v: A.a) : unit =
#rest of the function
does it mean it takes v of type A.a and return unit
or it takes two parameters, v and A.a and return a unit?
or it takes a function v with parameter A.a and returns a unit?
or something else?
let func (v: A.a) : unit =
The first : means v is a parameter and its type is expected to be A.a.
The second : means func is expected to return a type of unit
1 and maybe 3. In OCaml functions are values and if A.a is function-type your 1st argument of func is function.
It means 1.
That is, if v has type A.a, then func v has type unit.
Or, equivalently, the type of func is A.a -> unit.

Resources