Be careful of shared array

In Go, when you take a slice of an existing slice, you get two slices sharing the same underlying array.

package main

import "fmt"

func main() {
    s1 := []int{1, 2, 3}
    // A slice of s1 => {2, 3}
    s2 := s1[1:3]

    // The 0th element of s2 is the 1th element of s1
    s2[0] = 42

    fmt.Println(s1)  // [1 42 3]
    fmt.Println(s2)  // [42 3]
}

And we can append items to slices with the built-in function append.

package main

import "fmt"

func main() {
    s1 := []int{1, 2, 3}
    // A slice of s1 => {2}
    s2 := s1[1:2]

    s2 = append(s2, 42)

    fmt.Println(s1)  // [1 2 42]
    fmt.Println(s2)  // [2 42]
}

Buf if there’s not available capacity for appending new items, append would create a new bigger underlying array for you, and the returned slice will point to this new array.

package main

import "fmt"

func main() {
    s1 := []int{1, 2, 3}
    // Create a slice of s1 => {2, 3}
    // The len of s2 would be 2, and the capacity would also be 2
    // Which is to say, s2 is full
    s2 := s1[1:3]

    // Not available capacity for the new item
    // `append` helps us replace the underlying array with a bigger one
    s2 = append(s2, 42)

    // Now that the underlying array for s2 is a different one
    // updating an item of s2 would have no effect on s1
    s2[0] = 11
    
    fmt.Println(s1) // [1 2 3]
    fmt.Println(s2) // [11 3 42]
}

So we need to be careful, two related slices may independently grow and change their underlying array.

what you need to know about aws cloudformation

  1. CloudFormattion is free to use;
  2. You supply a template and any required parameters to CloudFormation to create or update a stack;
  3. A template is a JSON-formatted text file (example);
  4. Parameter can be used to specify sensitive information which you don’t want to store in the template;
  5. If not all resources are created or updated successfully, CloudFormation will rollback every change;

define and run cython function with ipython

I learned this trick in the book Cython: A Guide for Python Programmer.

After having both Cython and IPython installed, we can load the cythonmagic extension with:

In [1]: %load_ext cythonmagic

Now we have 3 extra magic functions in the session:

  • %%cython will cythonize and compile contents of the code cell.
In [4]: %%cython
   ...: def sum_up(*numbers):
   ...:     cdef int s = 0
   ...:     for n in numbers:
   ...:         s += n
   ...:     return s
   ...:
In [5]: sum_up(1, 2, 3)
Out[5]: 6
  • %%cython_inline simply passes the body of the cell to Cython.inline and returns the result.
In [16]: x = 3.14

In [17]: y = 2

In [18]: %%cython_inline
   ....: return x * y
   ....:
Out[18]: 6.28
  • %%cython_pyximport is similar to %%cython, except that the contents of the code shell are written to a .pyx file in the current working directory and imported using pyximport, and a module name is required.
In [21]: %%cython_pyximport double
....: def f(x):
....:     return 2.0 * x
....:
In [22]: !ls *.pyx
double.pyx
In [23]: f(4)
Out[23]: 8.0