aboutsummaryrefslogtreecommitdiff
path: root/spec/reference.bqn
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-20 22:08:00 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-20 22:08:00 -0400
commit1c2acf0cec24023260174541972d663f07aa923c (patch)
treea36f6e1513b9342debd9fdeb12cc8ad91750cafa /spec/reference.bqn
parent0f47b5d1eeeba8315e5428181197d5de77195a64 (diff)
Layer 4 and 5 comments
Diffstat (limited to 'spec/reference.bqn')
-rw-r--r--spec/reference.bqn97
1 files changed, 57 insertions, 40 deletions
diff --git a/spec/reference.bqn b/spec/reference.bqn
index d8c66f8a..4c597258 100644
--- a/spec/reference.bqn
+++ b/spec/reference.bqn
@@ -88,7 +88,7 @@ _fold←{
! 1==𝕩
l←≠v←𝕩 β‹„ F←𝔽
# If 𝕨 isn't given, start with the last cell of 𝕩
- r←𝕨 (0<l)β—Ά{𝕩⋄Identity f}β€Ώ{l↩l-1β‹„lβŠ‘π•©}⊘⊣ 𝕩
+ r←𝕨 (0<l)β—Ά{𝕩⋄Identity f}β€Ώ{l-↩1β‹„lβŠ‘π•©}⊘⊣ 𝕩
# Apply r↩(π•¨βŠ‘v)F r for 𝕨 from l to 0
l {𝕨 -⟜1⊸(βŠ£π•ŠβŠ‘βŸœv⊸F)⍟(>⟜0) 𝕩} r
}
@@ -220,40 +220,52 @@ Depth ← IsArrayβ—Ά0β€Ώ{1+0⌈´DepthΒ¨β₯Šπ•©}
˝ ← _insert
` ← _scan
-DropV← {βŠ‘βŸœπ•©Β¨π•¨+↕𝕨-Λœβ‰ π•©}
-Cell ← DropVβŸœβ‰’
+Drop1 ← {βŠ‘βŸœπ•©Β¨π•¨+↕𝕨-Λœβ‰ π•©} # Drop from list
+Cell ← Drop1βŸœβ‰’ # (-𝕨)-cell shape of array 𝕩
+# Merge: if empty, append fill shape to array shape to reshape fill
+# Otherwise, run all element ravels together
Merge←(0<β‰ βˆ˜β₯Š)β—Ά((βˆΎβ—‹β‰’β₯ŠβŠ’)⟜Fill⍟HasFill)β€Ώ{
cβ†β‰’βŠ‘π•©
- ! ∧´β₯Š(c≑≒)¨𝕩
- π•©βŠ‘βŸœToArrayΛœβŒœβ†•c
+ ! ∧´β₯Š(c≑≒)¨𝕩 # Shapes must match
+ π•©βŠ‘βŸœToArrayΛœβŒœβ†•c # Shape is (≒𝕩)∾c, with corresponding axis structure
}
+
+# Check if ranks/depths are valid: list of one to three integers
+# Return β₯Šπ•©
ValidateRanks←{
! 1β‰₯=𝕩
- 𝕩↩β₯Šπ•©
+ 𝕩β₯Šβ†©
! (1βŠΈβ‰€βˆ§β‰€βŸœ3)≠𝕩
! ∧´Int¨𝕩
𝕩
}
+# Extract the appropriate ranks/depths for a call
+# Use negative indexing and wrap with |
_ranks ← {⟨2⟩⊘⟨1,0⟩ ((⊣-1+|)ΛœβŸœβ‰ βŠ‘Β¨<∘⊒) ValidateRanksβˆ˜π”½}
_depthOp_←{
- neg←0>n←𝕨𝔾_ranks𝕩 β‹„ F←𝔽 β‹„ B←{𝕏;π•¨Λ™βŠΈπ•}
+ n ← 𝕨 𝔾_ranks 𝕩
+ neg←0>n β‹„ F←𝔽
_d←{
- R←(𝕗+neg)_d
- 𝕨(2β₯Š(negβˆ§π•—β‰₯0)∨(0βŒˆπ•—)β‰₯β‹ˆβ—‹β‰‘)β—Ά(⟨RΒ¨,R⟜(𝕩˙)Β¨βˆ˜βŠ£βŸ©β‰βŸ¨(𝕨 B r)¨∘⊒,F⟩)𝕩
+ R←(𝕗+neg)_d # Increment
+ rec ← 2β₯Š(negβˆ§π•—β‰₯0)∨(0βŒˆπ•—)β‰₯π•¨β‹ˆβ—‹β‰‘π•© # Whether to recurse into π•¨βŠ£π•© and 𝕩
+ 𝕨 recβ—Ά(⟨RΒ¨,R⟜(𝕩˙)Β¨βˆ˜βŠ£βŸ©β‰βŸ¨(𝕨{𝕏;π•¨Λ™βŠΈπ•}r)¨∘⊒,F⟩) 𝕩
}
𝕨 n _d 𝕩
}
_rankOp_←{
- k←𝕨(β‹ˆβ—‹= (0β‰€βŠ’)β—ΆβŸ¨βŒŠβŸœ-,0⌈-⟩¨ 𝔾_ranks)𝕩
- Enc←{
- fβ†βŠ‘βŸœ(≒𝕩)¨↕𝕨
+ k←𝕨(β‹ˆβ—‹= 0βŠΈβ‰€β—ΆβŸ¨βŒŠβŸœ-,0⌈-⟩¨ 𝔾_ranks)𝕩 # Effective frame length
+ # Enclose (-𝕨)-cells of 𝕩, that is, <βŽ‰((=𝕩)-𝕨) 𝕩
+ EncK←{
+ fβ†βŠ‘βŸœ(≒𝕩)¨↕𝕨 # 𝕨↑≒𝕩
c←×´s←𝕨Cell𝕩
fβ₯ŠβŠ‘βŸœ(β₯Šπ•©)¨∘((sβ₯Šβ†•c)+cΓ—βŠ’)¨↕×´f
}
- Enc↩(>⟜0Γ—1+β‰₯⟜=)β—ΆβŸ¨<⊒,Enc,<⌜⊒⟩
+ # Use <⊒ if k=0 (required to handle atoms correctly), and <⌜⊒ if k==𝕩
+ Enc←(>⟜0Γ—1+β‰₯⟜=)β—ΆβŸ¨<⊒,EncK,<⌜⊒⟩
> ((βŠ‘k)Enc𝕨) 𝔽¨ ((1-Λœβ‰ )βŠΈβŠ‘k)Enc𝕩
}
+
_insert←{
! 1≀=𝕩
𝕨 𝔽´ <Λ˜π•©
@@ -261,25 +273,25 @@ _insert←{
_scan←{
! IsArray 𝕩
! 1≀=𝕩
- F←𝔽
- cs←1 Cell 𝕩
+ c←×´cs←1 Cell 𝕩
! (cs≑≒)𝕨
- l←≠r←β₯Šπ•©
+ l←≠r←β₯Šπ•© β‹„ F←𝔽
𝕨 (0<l)β—ΆβŠ’β€Ώ{
- c←×´cs
+ # Initial cell: csβ₯Šπ•¨FΒ¨c↑𝕩 if 𝕨 is given
{r↩β‰₯⟜cβ—ΆβŸ¨βŠ‘βŸœ(β₯Šπ•©)⊸F,⊒⟩⟜(βŠ‘βŸœr)¨↕l}𝕨
+ # For non-initial elements, apply F to index i-c and i
+ # Must be ordered to compute value i-c first
(≒𝕩) β₯Š r {((𝕨-c)Fβ—‹(βŠ‘βŸœπ•©)𝕨)𝕨_amend𝕩}Β΄ (l-1)-↕l-c
} 𝕩
}
_repeat_←{
- n←𝕨𝔾𝕩
- fβ†βŠ‘π•¨βŸ¨π”½βŸ©βŠ˜βŸ¨π•¨π”½βŠ’βŸ©π•©
- l←u←0
- {!Int𝕩⋄!∞>|𝕩⋄l↩lβŒŠπ•©β‹„u↩uβŒˆπ•©}βš‡0 n
- b←𝕨{π•βŠ£}Λ™βŠ˜{𝕨˙{π”½π•βŠ£}}0
- iβ†βŸ¨π•©βŸ©β‹„P←B⊸{π•Ž`iβˆΎβ†•π•©}
- pos←𝕗 P u
- neg←𝕗 0⊸<β—ΆβŸ¨i,{𝕏⁼}⊸P⟩ -l
+ n←𝕨𝔾𝕩 # Repetition numbers
+ l←u←0 # Min and max repetitions
+ {!Int𝕩⋄!∞>|𝕩⋄lβŒŠβ†©π•©β‹„uβŒˆβ†©π•©}βš‡0 n
+ b←𝕨{⊒;𝕨{π•—Λ™βŠΈπ•}}0 # Bind 𝕨 to 𝕏 if necessary
+ iβ†βŸ¨π•©βŸ©β‹„P←{π•βŠ£}∘B⊸{π•Ž`iβˆΎβ†•π•©} # P makes a list of repetition 0, 1, …
+ pos←𝕗 P u # Positive repetitions
+ neg←𝕗 0⊸<β—ΆβŸ¨i,{𝕏⁼}⊸P⟩ -l # Negative repetitions
(|βŠ‘<⟜0βŠ‘posβ€ΏnegΛ™)βš‡0 n
}
@@ -296,6 +308,8 @@ _repeat_←{
⌽ ← Reverse ⊘ Rotate
/ ← Indices ⊘ Replicate
+# Multi-axis primitive: 𝔾 is applied to 𝕨 and determines the maximum
+# depth of the one-axis case
_onAxes_←{
F←𝔽
(𝔾<≑)βˆ˜βŠ£β—Ά{ # One axis
@@ -303,29 +317,29 @@ _onAxes_←{
𝕨F𝕩
}β€Ώ{ # Multiple axes
! 1β‰₯=𝕨
- ! 𝕨≀○≠≒𝕩
- R←{(βŠ‘π•¨)F(1 DropV 𝕨)⊸RΛ˜π•©}⍟{0<≠𝕨}
+ ! (≠𝕨)≀=𝕩
+ R←{(βŠ‘π•¨)F(1 Drop1 𝕨)⊸RΛ˜π•©}⍟{0<≠𝕨} # Recurse, then handle one axis
𝕨R𝕩
}⟜ToArray
}
SelSub←{
! IsArray 𝕨
- ! ∧´β₯ŠIntΒ¨ 𝕨
- ! ∧´β₯Š 𝕨 (β‰₯⟜-∧<) ≠𝕩
- 𝕨↩𝕨+(≠𝕩)×𝕨<0
+ ! ∧´β₯ŠIntΒ¨ 𝕨 # All integers
+ ! ∧´β₯Š 𝕨 (β‰₯⟜-∧<) ≠𝕩 # In range
+ 𝕨 +↩ (≠𝕩)×𝕨<0 # Wrap negatives
c←×´s←1 Cell 𝕩
- βŠ‘βŸœ(β₯Šπ•©)Β¨(c×𝕨)+⌜sβ₯Šβ†•c
+ βŠ‘βŸœ(β₯Šπ•©)Β¨(c×𝕨)+⌜sβ₯Šβ†•c # Extend each index to a whole cell
}
Select←ToArray⊸(SelSub _onAxes_ 1)
JoinTo←{
sβ†π•¨β‹ˆβ—‹β‰’π•©
- a←1⌈´k←≠¨s
- ! ∧´1β‰₯a-k
- c←(kΒ¬a)+⟜(↕a-1)⊸⊏¨s
+ a←1⌈´k←≠¨s # Argument ranks k and result rank a
+ ! ∧´1β‰₯a-k # Can add at most one axis
+ c←(kΒ¬a)+⟜(↕a-1)⊸⊏¨s # Cell shapes
! ≑´c
- l←+Β΄(a=k)βŠ£β—Ά1β€Ώ(βŠ‘βŠ’)Β¨s
+ l←+Β΄(a=k)βŠ£β—Ά1β€Ώ(βŠ‘βŠ’)Β¨s # Total length
(⟨lβŸ©βˆΎβŠ‘c)β₯Šπ•¨βˆΎβ—‹β₯Šπ•©
}
@@ -333,14 +347,17 @@ Take←{
T←{
! Int 𝕨
l←≠𝕩
+ # Indices, with clamp and modulus so that fills are all l
i←(l+1)|Β―1⌈l⌊((𝕨<0)×𝕨+l)+↕|𝕨
+ # Don't get the fill if not needed, as it can error
i⊏JoinTo⟜(1⊸Cellβ₯ŠFill)⍟(∨´l=i)𝕩
}
+ # Add leading 1s to shape of 𝕩 first if necessary
𝕨 T _onAxes_ 0 (⟨1⟩β₯ŠΛœ0βŒˆπ•¨-β—‹β‰ βŠ’)βŠΈβˆΎβˆ˜β‰’βŠΈβ₯Šπ•©
}
Drop←{
- s←(≠𝕨)(βŠ£β†‘βŠ’βˆΎΛœ1β₯ŠΛœ0⌈-βŸœβ‰ )≒𝕩
- ((sΓ—Β―1⋆𝕨>0)+(-s)⌈sβŒŠπ•¨)↑𝕩
+ s←(≠𝕨)(βŠ£β†‘βŠ’βˆΎΛœ1β₯ŠΛœ0⌈-βŸœβ‰ )≒𝕩 # Padded shape
+ ((sΓ—Β¬βŠΈ-𝕨>0)+(-s)⌈sβŒŠπ•¨)↑𝕩 # Clamp and complement, then use Take
}
Prefixes ← {!1≀=𝕩 β‹„ (↕1+≠𝕩)TakeΒ¨<𝕩}
Suffixes ← {!1≀=𝕩 β‹„ (↕1+≠𝕩)DropΒ¨<𝕩}
@@ -352,9 +369,9 @@ NudgeBack ← (1↑0β†‘βŠ’)⊸ShiftAfter
Windows←{
! 1β‰₯=𝕨
- ! 𝕨≀○≠≒𝕩
+ ! (≠𝕨)≀=𝕩
! ∧´NatΒ¨β₯Šπ•¨
- s←(≠𝕨)↑≒𝕩
+ sβ†π•¨β‰ βŠΈβ†‘β‰’π•©
! βˆ§Β΄π•¨β‰€1+s
𝕨{(∾⟜(π•¨β‰ βŠΈβ†“β‰’π•©)βˆ˜β‰’β₯Š>)<Β¨βŠΈβŠβŸœπ•©Β¨s(Β¬+βŒœβ—‹β†•βŠ’)β₯Šπ•¨}⍟(0<≠𝕨)𝕩
}
@@ -447,7 +464,7 @@ ReorderAxes←{
𝕩↩<⍟(0=≑)𝕩
! 1β‰₯=𝕨
𝕨↩β₯Šπ•¨
- ! 𝕨≀○≠≒𝕩
+ ! (≠𝕨)≀=𝕩
! ∧´NatΒ¨β₯Šπ•¨
r←(=𝕩)-+Β΄Β¬βˆŠπ•¨
! βˆ§Β΄π•¨<r