 bondscell_results$88e4de2b-a04d-486c-8377-dd9646eaaf90queued¤logsrunning¦outputbody<div class="markdown"><h2>do-block syntax</h2>
<p>If a function accepts another function as its <strong>first</strong> argument we can use the <code>do-block</code> syntax, it is particularly useful </p>
</div>mimetext/htmlrootassigneelast_run_timestampAjE persist_js_state·has_pluto_hook_features§cell_id$88e4de2b-a04d-486c-8377-dd9646eaaf90depends_on_disabled_cells§runtime ޵published_object_keysdepends_on_skipped_cells§errored$6e46f5ce-c320-467d-843d-0003880bddeaqueued¤logslinemsg9A * B = [6.0 6.0; 14.0 14.0]
A .* B = [2.0 4.0; 6.0 8.0]
text/plaincell_id$6e46f5ce-c320-467d-843d-0003880bddeakwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/5ete1/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbody)2×2 Matrix{Float64}:
 2.0  4.0
 6.0  8.0mimetext/plainrootassigneelast_run_timestampAjJmpersist_js_state·has_pluto_hook_features§cell_id$6e46f5ce-c320-467d-843d-0003880bddeadepends_on_disabled_cells§runtime[oܓpublished_object_keysdepends_on_skipped_cells§errored$201356a6-ce32-4b4a-becd-4509fc79c35aqueued¤logsrunning¦outputbody<div class="markdown"><h1>Passing functions as arguments</h1>
<p>It is not rare to pass a function as argument for another function, e.g. having to perform a fit. Julia gives the user 2 methods so that it can bypass the definition of the function.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAjEpersist_js_state·has_pluto_hook_features§cell_id$201356a6-ce32-4b4a-becd-4509fc79c35adepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$9d13960a-3d65-48d7-990c-1961fa001141queued¤logsrunning¦outputbodyprefixFloat64elements2.64434e-15text/plain2.0text/plaintypeArrayprefix_shortobjectidf0582d6f1e0a4825mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAjK7persist_js_state·has_pluto_hook_features§cell_id$9d13960a-3d65-48d7-990c-1961fa001141depends_on_disabled_cells§runtimeΞ.published_object_keysdepends_on_skipped_cells§errored$891849f0-0d84-11f0-1994-33a1449ad649queued¤logsrunning¦outputbodyh<div class="markdown"><h1>Broadcasting</h1>
<p>Suppose you have a function like the following</p>
</div>mimetext/htmlrootassigneelast_run_timestampAjElpersist_js_state·has_pluto_hook_features§cell_id$891849f0-0d84-11f0-1994-33a1449ad649depends_on_disabled_cells§runtime zpublished_object_keysdepends_on_skipped_cells§errored$16c77061-2611-4e9f-96b8-1c0c77db9467queued¤logsrunning¦outputbody<div class="markdown"><p>Unlike python, where the operator <code>*</code> applied to matrices performs an element wise multiplication, in Julia performs the row-column multiplication, we can still do the elementwise multiplication using broadcasting.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAjEװpersist_js_state·has_pluto_hook_features§cell_id$16c77061-2611-4e9f-96b8-1c0c77db9467depends_on_disabled_cells§runtime <'published_object_keysdepends_on_skipped_cells§errored$0d65f47e-4dae-4876-9b36-de62d6694011queued¤logsrunning¦outputbody%add2 (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAjI+аpersist_js_state·has_pluto_hook_features§cell_id$0d65f47e-4dae-4876-9b36-de62d6694011depends_on_disabled_cells§runtime fpublished_object_keysdepends_on_skipped_cells§errored$9ee84029-b80d-4253-8ae6-8c8affb37cf9queued¤logsrunning¦outputbody5.0mimetext/plainrootassigneelast_run_timestampAjIUpersist_js_state·has_pluto_hook_features§cell_id$9ee84029-b80d-4253-8ae6-8c8affb37cf9depends_on_disabled_cells§runtime)published_object_keysdepends_on_skipped_cells§errored$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fqueued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAjIKpersist_js_state·has_pluto_hook_features§cell_id$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fdepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8dqueued¤logsrunning¦outputbody٥<div class="markdown"><h2>Anonymous functions</h2>
<p>These are similar to python&#39;s lambda function, they are particularly useful for small functions.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAjEpersist_js_state·has_pluto_hook_features§cell_id$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8ddepends_on_disabled_cells§runtime $published_object_keysdepends_on_skipped_cells§errored$bc532db4-ab56-498e-8f8c-7c7d44c08a29queued¤logsrunning¦outputbody<div class="markdown"><p>Now imagine that you want to apply the function to all the element of an array. To pass the array as input would give an error</p>
<pre><code class="language-julia-repl">julia&gt; add2&#40;&#91;1, 2, 3&#93;&#41;
ERROR: MethodError: no method matching add2&#40;::Vector&#123;Int64&#125;&#41;
The function &#96;add2&#96; exists, but no method is defined for this combination of argument types.

Closest candidates are:
  add2&#40;::Number&#41;
   @ Main REPL&#91;4&#93;:1</code></pre>
<p>We can then use the broadcasting operator <code>.</code> which applies the function to each element of the collection</p>
</div>mimetext/htmlrootassigneelast_run_timestampAjEpersist_js_state·has_pluto_hook_features§cell_id$bc532db4-ab56-498e-8f8c-7c7d44c08a29depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$04b0c385-62dd-423c-bd12-1bb53132e4e7queued¤logsrunning¦outputbodyprefixInt64elements3text/plain4text/plain5text/plaintypeArrayprefix_shortobjectidd7f518c567b9ecd0mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAjI(persist_js_state·has_pluto_hook_features§cell_id$04b0c385-62dd-423c-bd12-1bb53132e4e7depends_on_disabled_cells§runtime꺵published_object_keysdepends_on_skipped_cells§errored$9d24d3bb-eb96-4432-943e-92cb45212c98queued¤logsrunning¦outputbodyٵ<div class="markdown"><p>if the functiona accepts only 1 argument, the brackets can be omitted, for functions with no arguments the syntax is <code>&#40;&#41;-&gt;</code></p>
</div>mimetext/htmlrootassigneelast_run_timestampAjEYpersist_js_state·has_pluto_hook_features§cell_id$9d24d3bb-eb96-4432-943e-92cb45212c98depends_on_disabled_cells§runtime P:published_object_keysdepends_on_skipped_cells§errored±cell_dependencies$88e4de2b-a04d-486c-8377-dd9646eaaf90precedence_heuristic	cell_id$88e4de2b-a04d-486c-8377-dd9646eaaf90downstream_cells_mapupstream_cells_map@md_strgetindex$6e46f5ce-c320-467d-843d-0003880bddeaprecedence_heuristic	cell_id$6e46f5ce-c320-467d-843d-0003880bddeadownstream_cells_mapupstream_cells_mapBase.reprBaseBase.printlnFloat64*@show$201356a6-ce32-4b4a-becd-4509fc79c35aprecedence_heuristic	cell_id$201356a6-ce32-4b4a-becd-4509fc79c35adownstream_cells_mapupstream_cells_map@md_strgetindex$9d13960a-3d65-48d7-990c-1961fa001141precedence_heuristic	cell_id$9d13960a-3d65-48d7-990c-1961fa001141downstream_cells_mapupstream_cells_map+coef*curve_fit$891849f0-0d84-11f0-1994-33a1449ad649precedence_heuristic	cell_id$891849f0-0d84-11f0-1994-33a1449ad649downstream_cells_mapupstream_cells_map@md_strgetindex$16c77061-2611-4e9f-96b8-1c0c77db9467precedence_heuristic	cell_id$16c77061-2611-4e9f-96b8-1c0c77db9467downstream_cells_mapupstream_cells_map@md_strgetindex$0d65f47e-4dae-4876-9b36-de62d6694011precedence_heuristic	cell_id$0d65f47e-4dae-4876-9b36-de62d6694011downstream_cells_mapadd2$9ee84029-b80d-4253-8ae6-8c8affb37cf9$04b0c385-62dd-423c-bd12-1bb53132e4e7upstream_cells_map+Number$9ee84029-b80d-4253-8ae6-8c8affb37cf9precedence_heuristic	cell_id$9ee84029-b80d-4253-8ae6-8c8affb37cf9downstream_cells_mapupstream_cells_mapadd2$0d65f47e-4dae-4876-9b36-de62d6694011$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fprecedence_heuristiccell_id$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fdownstream_cells_mapLsqFitupstream_cells_map$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8dprecedence_heuristic	cell_id$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8ddownstream_cells_mapupstream_cells_map@md_strgetindex$bc532db4-ab56-498e-8f8c-7c7d44c08a29precedence_heuristic	cell_id$bc532db4-ab56-498e-8f8c-7c7d44c08a29downstream_cells_mapupstream_cells_map@md_strgetindex$04b0c385-62dd-423c-bd12-1bb53132e4e7precedence_heuristic	cell_id$04b0c385-62dd-423c-bd12-1bb53132e4e7downstream_cells_mapupstream_cells_mapadd2$0d65f47e-4dae-4876-9b36-de62d6694011$9d24d3bb-eb96-4432-943e-92cb45212c98precedence_heuristic	cell_id$9d24d3bb-eb96-4432-943e-92cb45212c98downstream_cells_mapupstream_cells_map@md_strgetindexcell_execution_order$b32f61d8-cda5-4f79-b8b7-c0b6b28d917f$891849f0-0d84-11f0-1994-33a1449ad649$0d65f47e-4dae-4876-9b36-de62d6694011$9ee84029-b80d-4253-8ae6-8c8affb37cf9$bc532db4-ab56-498e-8f8c-7c7d44c08a29$04b0c385-62dd-423c-bd12-1bb53132e4e7$16c77061-2611-4e9f-96b8-1c0c77db9467$6e46f5ce-c320-467d-843d-0003880bddea$201356a6-ce32-4b4a-becd-4509fc79c35a$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8d$9d13960a-3d65-48d7-990c-1961fa001141$9d24d3bb-eb96-4432-943e-92cb45212c98$88e4de2b-a04d-486c-8377-dd9646eaaf90last_hot_reload_time        shortpathmore_functions.jlprocess_statusreadypathO/home/runner/work/JuliaWorkshopAISF/JuliaWorkshopAISF/Lesson1/more_functions.jlpluto_versionv0.20.8last_save_timeAjEΪcell_order$891849f0-0d84-11f0-1994-33a1449ad649$0d65f47e-4dae-4876-9b36-de62d6694011$9ee84029-b80d-4253-8ae6-8c8affb37cf9$bc532db4-ab56-498e-8f8c-7c7d44c08a29$04b0c385-62dd-423c-bd12-1bb53132e4e7$16c77061-2611-4e9f-96b8-1c0c77db9467$6e46f5ce-c320-467d-843d-0003880bddea$201356a6-ce32-4b4a-becd-4509fc79c35a$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8d$b32f61d8-cda5-4f79-b8b7-c0b6b28d917f$9d13960a-3d65-48d7-990c-1961fa001141$9d24d3bb-eb96-4432-943e-92cb45212c98$88e4de2b-a04d-486c-8377-dd9646eaaf90published_objectsnbpkginstall_time_ns   o4instantiatedòinstalled_versionsLsqFit0.15.0terminal_outputsnbpkg_syncV
[0m[1mResolving...[22m
[90m===[39m
[32m[1m   Installed[22m[39m StatsFuns ─ v1.3.2
           No Changes           to `/tmp/jl_78MgzU/Project.toml`              [32m[1m[22m[39m
[32m[1m    Updating[22m[39m `/tmp/jl_78MgzU/Manifest.toml`
  [90m[05823500] [39m[93m↑ OpenLibm_jll v0.8.1+4 ⇒ v0.8.5+0[39m

[0m[1mInstantiating...[22m
[90m===[39m

[0m[1mPrecompiling...[22m
[90m===[39m
[32m[1m  Activating[22m[39m project at `/tmp/jl_78MgzU`
[92m[1mPrecompiling[22m[39m project...
  3 dependencies successfully precompiled in 8 seconds. 71 already precompiled.LsqFitV
[0m[1mResolving...[22m
[90m===[39m
[32m[1m   Installed[22m[39m StatsFuns ─ v1.3.2
           No Changes           to `/tmp/jl_78MgzU/Project.toml`              [32m[1m[22m[39m
[32m[1m    Updating[22m[39m `/tmp/jl_78MgzU/Manifest.toml`
  [90m[05823500] [39m[93m↑ OpenLibm_jll v0.8.1+4 ⇒ v0.8.5+0[39m

[0m[1mInstantiating...[22m
[90m===[39m

[0m[1mPrecompiling...[22m
[90m===[39m
[32m[1m  Activating[22m[39m project at `/tmp/jl_78MgzU`
[92m[1mPrecompiling[22m[39m project...
  3 dependencies successfully precompiled in 8 seconds. 71 already precompiled.enabled÷restart_recommended_msgrestart_required_msgbusy_packageswaiting_for_permission,waiting_for_permission_but_probably_disabled«cell_inputs$88e4de2b-a04d-486c-8377-dd9646eaaf90cell_id$88e4de2b-a04d-486c-8377-dd9646eaaf90codeٚmd"""
## do-block syntax
If a function accepts another function as its **first** argument we can use the `do-block` syntax, it is particularly useful 
"""metadatashow_logsèdisabled®skip_as_script«code_folded$6e46f5ce-c320-467d-843d-0003880bddeacell_id$6e46f5ce-c320-467d-843d-0003880bddeacodePlet
	A = Float64[1 2; 3 4]
	B = Float64[2 2; 2 2]
	@show A * B
	@show A .* B
endmetadatashow_logsèdisabled®skip_as_script«code_folded$201356a6-ce32-4b4a-becd-4509fc79c35acell_id$201356a6-ce32-4b4a-becd-4509fc79c35acodemd"""
# Passing functions as arguments
It is not rare to pass a function as argument for another function, e.g. having to perform a fit. Julia gives the user 2 methods so that it can bypass the definition of the function.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$9d13960a-3d65-48d7-990c-1961fa001141cell_id$9d13960a-3d65-48d7-990c-1961fa001141code^let
	fit = curve_fit((x,p)->p[1].+p[2].*x,[1., 2., 3.], [2., 4., 6.], [1., 1.])
	coef(fit)
endmetadatashow_logsèdisabled®skip_as_script«code_folded$891849f0-0d84-11f0-1994-33a1449ad649cell_id$891849f0-0d84-11f0-1994-33a1449ad649codeGmd"""
# Broadcasting
Suppose you have a function like the following
"""metadatashow_logsèdisabled®skip_as_script«code_folded$16c77061-2611-4e9f-96b8-1c0c77db9467cell_id$16c77061-2611-4e9f-96b8-1c0c77db9467codemd"""
Unlike python, where the operator `*` applied to matrices performs an element wise multiplication, in Julia performs the row-column multiplication, we can still do the elementwise multiplication using broadcasting.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$0d65f47e-4dae-4876-9b36-de62d6694011cell_id$0d65f47e-4dae-4876-9b36-de62d6694011codeadd2(x::Number) = x + 2metadatashow_logsèdisabled®skip_as_script«code_folded$9ee84029-b80d-4253-8ae6-8c8affb37cf9cell_id$9ee84029-b80d-4253-8ae6-8c8affb37cf9codeadd2(3.0)metadatashow_logsèdisabled®skip_as_script«code_folded$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fcell_id$b32f61d8-cda5-4f79-b8b7-c0b6b28d917fcodeusing LsqFitmetadatashow_logsèdisabled®skip_as_script«code_folded$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8dcell_id$c9bd0437-e1ac-4f92-9a1c-1eedfb63ed8dcodeفmd"""
## Anonymous functions
These are similar to python's lambda function, they are particularly useful for small functions.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$bc532db4-ab56-498e-8f8c-7c7d44c08a29cell_id$bc532db4-ab56-498e-8f8c-7c7d44c08a29codemd"""
Now imagine that you want to apply the function to all the element of an array. To pass the array as input would give an error
```julia-repl
julia> add2([1, 2, 3])
ERROR: MethodError: no method matching add2(::Vector{Int64})
The function `add2` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  add2(::Number)
   @ Main REPL[4]:1
```
We can then use the broadcasting operator `.` which applies the function to each element of the collection
"""metadatashow_logsèdisabled®skip_as_script«code_folded$04b0c385-62dd-423c-bd12-1bb53132e4e7cell_id$04b0c385-62dd-423c-bd12-1bb53132e4e7codeadd2.([1, 2, 3])metadatashow_logsèdisabled®skip_as_script«code_folded$9d24d3bb-eb96-4432-943e-92cb45212c98cell_id$9d24d3bb-eb96-4432-943e-92cb45212c98codeمmd"""
if the functiona accepts only 1 argument, the brackets can be omitted, for functions with no arguments the syntax is `()->`
"""metadatashow_logsèdisabled®skip_as_script«code_foldedënotebook_id$4389c1f4-2f06-11f0-2ba9-25c5d0514886in_temp_dir¨metadata