4,493
edits
No edit summary |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
: ''"dfns" redirects here. For the workspace by Scholes, see [[dfns workspace]].'' | |||
A '''dfn''' (contraction of '''direct function''' or '''dynamic function''', pronounced "dee fun") is an alternative way to define a [[function]] and [[operator]], invented by [[John Scholes]]. A dfn operator can also be called a '''dop''' (pronounced "dee op"). | A '''dfn''' (contraction of '''direct function''' or '''dynamic function''', pronounced "dee fun") is an alternative way to define a [[function]] and [[operator]], invented by [[John Scholes]]. A dfn operator can also be called a '''dop''' (pronounced "dee op"). | ||
Line 12: | Line 14: | ||
4 | 4 | ||
3 {⍵*÷⍺} 27 ⍝ ⍺th root | 3 {⍵*÷⍺} 27 ⍝ ⍺th root | ||
3 | 3</source> | ||
=== Default left arguments === | |||
Assignment to <source lang=apl inline>⍺</source> is unusual in that the entire statement is only executed if the dfn is called monadically:<ref>[https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Default%20Left%20Argument.htm Default Left Argument] – Dyalog APL.</ref> | |||
<source lang=apl> | |||
root←{ | root←{ | ||
⍺←2 ⍝ square root by default | ⍺←2 ⍝ square root by default | ||
⍵*÷⍺ ⍝ result | |||
}</source> | |||
=== Guards === | |||
Guards provide dfns with support for basic flow control.<ref>[https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Guards.htm Guards] – Dyalog APL.</ref> This is a multiline dfn with a conditional result: | |||
<source lang=apl> | |||
root←{ | |||
⍺=0:0 ⍝ return zero if zeroth root | ⍺=0:0 ⍝ return zero if zeroth root | ||
⍵*÷⍺ ⍝ result | ⍵*÷⍺ ⍝ result | ||
Line 40: | Line 50: | ||
N/A | N/A | ||
</source> | </source> | ||
=== Shy results === | |||
[[Roger Hui]]'s <source lang=apl inline>assert</source> is a dfn that has become the de facto standard when it comes to test suites.<ref>Stefan Kruger. [https://www.dyalog.com/blog/2021/04/2020-problem-solving-competition-phase-ii-highlights/ 2020 Problem Solving Competition – Phase II highlights]. [[Dyalog Ltd.|Dyalog]] blog. April 30, 2021.</ref>. In it, Hui uses both a [[default left argument]] and a final assignment to make the dfn shy: | |||
<source lang=apl> | |||
assert ← {⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0}</source> | |||
== External links == | == External links == |