Ch 19 - 20 - 21 The Goodies Flashcards

1
Q

Think Julia

How do you create a named tuple?

A

You can name the components of a tuple, creating a named tuple:

julia > x = (a = 1, b = 1 + 1)
(a = 1, b = 2)
julia > x.a
1

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7189-7196). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

What is the compact syntax for creating a function?

A

Functions A function in Julia can be defined by a compact syntax:

julia > f( x, y) = x + y
f (generic function with 1 method)

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7200-7205). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

How is an anonymous function created?

A

Anonymous Functions

We can define a function without specifying a name:

julia > x -> x^2 + 2x - 1
#1 (generic function with 1 method)

Which is the same as:

julia > function (x)
….x^2 + 2x - 1
end

#3 (generic function with 1 method)

These are examples of anonymous functions. Anonymous functions are often used as arguments to another function:

julia > using Plots
julia > plot( x -> x^2 + 2x - 1, 0, 10, xlabel =” x”, ylabel =” y”)

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7206-7226). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

How are keyword arguments created?

A

Keyword Arguments Function arguments can also be named:

julia > function myplot( x, y; style =” solid”, width = 1, color =” black”)
…..###
end
myplot (generic function with 1 method)
julia > myplot( 0: 10, 0: 10, style =” dotted”, color =” blue”)

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7230-7242). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

How are keyword arguments seperated from place arguments?

A

Keyword Arguments Function arguments can also be named:

julia > function myplot( x, y; style =” solid”, width = 1, color =” black”)
…..###
end
myplot (generic function with 1 method)

Keyword arguments in a function are specified after a semicolon in the signature but can also be called with a comma.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Location 7243). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

What is a block?

A

Blocks

A block is a way to group a number of statements. A block starts with the keyword begin and ends with end.

In Chapter 4, the @svg macro was introduced:

🐢 Turtle()
@svg begin
….forward( 🐢, 100)
….turn( 🐢, -90)
….forward( 🐢, 100)
end

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7256-7268). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

What is a do block?

A

do Blocks

In “Reading and Writing” I showed you how to close a file when you’re done writing. This can be done automatically using a do block:

julia > data = “This here’s the wattle,\ nthe emblem of our land.\ n”
“This here’s the wattle,\ nthe emblem of our land.\ n”
julia > open(“ output.txt”, “w”) do fout
….write( fout, data)
end
48

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7290-7299). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Think Julia

How does the ternary operator work?

A

The ternary operator, ?:, is an alternative to an if-elseif statement used when you need to make a choice between single expression values:

julia > a = 150
150
julia > a % 2 == 0 ? println(“ even”) : println(“ odd”)
even

The expression before the ? is a conditional expression. If the condition is true, the expression before the : is evaluated; otherwise, the expression after the : is evaluated.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7325-7337). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Think Julia

How is a primitive type created?

A

Primitive Types

A concrete type consisting of plain old bits is called a primitive type. Unlike most languages, Julia allows you to declare your own primitive types. The standard primitive types are defined in the same way:

primitive type Float64 <: AbstractFloat 64 end
primitive type Bool <: Integer 8 end
primitive type Char <: AbstractChar 32 end
primitive type Int64 <: Signed 64 end

The number in the statement specifies how many bits are required.

The following example creates a primitive type Byte and a constructor:

julia > primitive type Byte 8 end
julia > Byte( val:: UInt8) = reinterpret( Byte, val)
Byte
julia > b = Byte( 0x01)
Byte( 0x01)

The function reinterpret is used to store the bits of an unsigned integer with 8 bits (UInt8) into the Byte.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7414-7415). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Think Julia

How can the performance of a function be measured?

A

Measuring Performance

We have seen that some algorithms perform better than others. The fibonnaci implementation from “Memos” is a lot faster than the fib implementation from “One More Example”. The @time macro allows us to quantify the difference:

julia > fib( 1)
1
julia > fibonacci( 1)
1
julia > @time fib( 40)
0.567546 seconds (5 allocations: 176 bytes)
1.102334155
julia > @time fibonacci( 40)
0.000012 seconds (8 allocations: 1.547 KiB)
1.102334155

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 7743-7746). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly