Skip to content

Commit

Permalink
update functions chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastyBlacksmith committed Aug 30, 2023
1 parent f574a6e commit e271b40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
56 changes: 28 additions & 28 deletions 05_Write_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ Trebuchets.shoot(5, 0.25pi, 500)[2]

function shoot_distance(windspeed, angle, weight)
Trebuchets.shoot(windspeed, angle, weight)[2]
#md end ;
#nb end
end

# !!! note "Implicit return"
# Note that Melissa didn't have to use the `return` keyword, since in Julia the
Expand Down Expand Up @@ -184,39 +183,40 @@ end
# These are _anonymous functions_.
# They can be defined with either the so-called stabby lambda notation,

(windspeed, angle, weight) -> Trebuchets.shoot(windspeed, angle, weight)[2] ;
(windspeed, angle, weight) -> Trebuchets.shoot(windspeed, angle, weight)[2]

# or in long form, by omitting the name:

function (windspeed, angle, weight)
Trebuchets.shoot(windspeed, angle, weight)[2]
end

# ### Errors and macros
# ### Calling methods
#
# Now, that she defined all these methods she tests calling a few

# Melissa would like to set the fields of a `Trebuchet` using an index.
# She writes
shoot_distance(5, 0.25pi, 500)

#md # ```julia
#md # Trebuchets[1] = 2
#md # ```
#

#nb Trebuchets[1] = 2
shoot_distance([5, 0.25pi, 500])

#md # ```error
#md # ERROR: MethodError: no method matching setindex!(::Trebuchet, ::Int64, ::Int64)
#md # Stacktrace:
#md # [1] top-level scope
#md # @ REPL[4]:1
#md # ```
# For the other method she needs to construct `Trebuchet` and `Environment` objects first

env = Environment(5, 100)

# The error tells her two things:
#

# 1. a function named `setindex!` was called
trebuchet = Trebuchet(500, 0.25pi)

# ### Errors and macros

# This error tells her two things:

# 1. a function named `size` was called
# 2. it didn't have a method for `Trebuchet`

# Melissa wants to add the missing method to `setindex!` but she doesn't know
# Melissa wants to add the missing method to `size` but she doesn't know
# where it is defined.
# There is a handy _macro_ named `@which` that obtains the module where the
# function is defined.
Expand All @@ -228,22 +228,22 @@ end
# They can be expanded by prepending `@macroexpand` to the macro call of
# interest.

#md # ```julia
#md # @which setindex!
#md # ```
@which size

#nb @which setindex!
# Now Melissa knows she needs to add a method to `Base.size` with the
# signature `(::Trebuchet)`.
# She can also lookup the docstring using the `@doc` macro

#md # ```output
#md # Base
#md # ```
@doc size

# With that information she can now implement this method:

# Now Melissa knows she needs to add a method to `Base.setindex!` with the
# signature `(::Trebuchet, ::Int64, ::Int64)`.
Base.size(::Trebuchet) = tuple(2)


# !!! keypoints
# - "You can think of functions being a collection of methods"
# - "Methods are defined by their signature"
# - "The signature is defined by the number of arguments, their order and their type"
# - "Keep the number of positional arguments low"
# - "Macros transform Julia expressions"
1 change: 0 additions & 1 deletion generate.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cd(@__DIR__)
using Distributed
while nprocs() < min(4, length(Sys.cpu_info()))
addprocs(1)
Expand Down

0 comments on commit e271b40

Please sign in to comment.