From d6abe6879f53763bb1283a7721676be9e7f6c27c Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 9 Apr 2021 15:53:49 -0400 Subject: Always give an identity element for folds in the runtime if they can be empty --- src/r.bqn | 64 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/r.bqn b/src/r.bqn index 6f5b3859..c6df1d1b 100644 --- a/src/r.bqn +++ b/src/r.bqn @@ -437,13 +437,13 @@ ReshapeT ← "∘⌊⌽↑"_glyphLookup_(↕5) Reshape←{ "𝕨⥊𝕩: 𝕨 must have rank at most 1" ! 1≥=𝕨 s←Deshape 𝕨 - sp←+´p←¬Nat⌜s + sp←0+´p←¬Nat⌜s "𝕨⥊𝕩: 𝕨 must consist of natural numbers" ! 1≥sp n←≠d←Deshape 𝕩 - l←sp◶(×´)‿{ - lp←×´p⊣◶⊢‿1¨𝕩 + l←sp◶(1×´⊢)‿{ + lp←1×´p⊣◶⊢‿1¨𝕩 "𝕨⥊𝕩: Can't compute axis length when rest of shape is empty" ! 0⟜0×1+≥⟜=)◶⟨<⊢,Enc,<⌜⊢⟩ > ((0⊑k)Enc𝕨) 𝔽¨ ((1-˜≠)⊸⊑k)Enc𝕩 @@ -505,10 +505,10 @@ _insert←{ JoinTo←∨○(1<=)◶(∾○⥊)‿{ s←𝕨Pair○≢𝕩 a←1⌈´k←≠⌜s - "𝕨∾𝕩: Rank of 𝕨 and 𝕩 must differ by at most 1" ! ∧´1≥a-k + "𝕨∾𝕩: Rank of 𝕨 and 𝕩 must differ by at most 1" ! 1∧´1≥a-k c←(k¬a)+⟜(↕a-1)⊸⊏¨s "𝕨∾𝕩: Cell shapes of 𝕨 and 𝕩 must match" ! ≡´c - l←+´(a=k)⊣◶1‿(0⊑⊢)¨s + l←0+´(a=k)⊣◶1‿(0⊑⊢)¨s (⟨l⟩∾0⊑c)⥊𝕨∾○⥊𝕩 } _fillBy_ IF @@ -532,8 +532,8 @@ Replicate ← (0<=∘⊣)◶{ « ← FC⊸ShiftAfter ⊘ ShiftAfter _fillBy_ (⊢⊘IF) _group←{ - "⊔: Grouping argument must consist of integers" ! ∧´Int⌜𝕩 - "⊔: Grouping argument values cannot be less than ¯1" ! ∧´¯1≤𝕩 + "⊔: Grouping argument must consist of integers" ! 1∧´Int⌜𝕩 + "⊔: Grouping argument values cannot be less than ¯1" ! 1∧´¯1≤𝕩 GL←GroupLen⋄𝕩↩𝕨(-˜⟜≠{GL↩(𝕨⊑𝕩)GL⊢⋄𝕨↑𝕩}⊢)⍟(0⊘⊣)𝕩 d←(l←GL𝕩)GroupOrd𝕩 i←0⋄(𝔽{𝕩⋄(i↩i+1)⊢i⊑d}⌜∘↕)⌜l @@ -550,36 +550,36 @@ GroupGen←{ "𝕨⊔𝕩: 𝕩 must be an array" ! IsArray 𝕩 𝕨↩Pair∘ToArray⍟(2>≡)𝕨 "𝕨⊔𝕩: Compound 𝕨 must be a list" ! 1==𝕨 - n←+´r←=⌜𝕨 + n←0+´r←=⌜𝕨 "𝕨⊔𝕩: Total rank of 𝕨 must be at most rank of 𝕩" ! n≤=𝕩 ld←(Join≢⌜𝕨)-n↑≢𝕩 - "𝕨⊔𝕩: Lengths of 𝕨 must equal to 𝕩, or one more only in a rank-1 component" ! ∧´(0⊸≤∧≤⟜(r/1=r))ld + "𝕨⊔𝕩: Lengths of 𝕨 must equal to 𝕩, or one more only in a rank-1 component" ! 1∧´(0⊸≤∧≤⟜(r/1=r))ld dr←r⌊(0»+`r)⊏ld∾⟨0⟩ l←≠⌜𝕨↩⥊⌜𝕨 ⋄ LS←∾⟜(n Cell 𝕩) Reshape 𝕩˙ - S←⊏⟜(LS⟨×´l⟩) + S←⊏⟜(LS⟨1×´l⟩) (LS 0⌜𝕨) Fill dr (1≠≠∘⊢)◶⟨S _group○(0⊸⊑), S⌜ ·+⌜⌜´ (⌽×`1»⌽l) × ⊢_group¨⟩ 𝕨 } Pick1←{ "𝕨⊑𝕩: Indices in compound 𝕨 must be lists" ! 1==𝕨 "𝕨⊑𝕩: Index length in 𝕨 must match rank of 𝕩" ! 𝕨=○≠s←≢𝕩 - "𝕨⊑𝕩: Indices in 𝕨 must consist of integers" ! ∧´Int⌜𝕨 - "𝕨⊑𝕩: Index out of range" ! ∧´𝕨(≥⟜-∧<)s + "𝕨⊑𝕩: Indices in 𝕨 must consist of integers" ! 1∧´Int⌜𝕨 + "𝕨⊑𝕩: Index out of range" ! 1∧´𝕨(≥⟜-∧<)s 𝕨↩𝕨+s×𝕨<0 (⥊𝕩)⊑˜0(⊑⟜𝕨+⊑⟜s×⊢)´-↕⊸¬≠𝕨 } -Pickd←(∨´IsArray⌜∘⥊∘⊣)◶Pick1‿{Pickd⟜𝕩⌜𝕨} +Pickd←(0∨´IsArray⌜∘⥊∘⊣)◶Pick1‿{Pickd⟜𝕩⌜𝕨} Pick←IsArray◶⥊‿⊢⊸Pickd # Sorting CmpLen ← { - e←𝕨-˜○(∨´0⊸=)𝕩 + e←𝕨-˜○(0∨´0⊸=)𝕩 𝕨(e=0)◶⟨0,e⟩‿{ c←×𝕨-○≠𝕩 r←𝕨⌊○≠𝕩 l←𝕨{ - i←+´∧`𝕨=𝕩 - m←×´⊑⟜𝕨⌜↕i + i←0+´∧`𝕨=𝕩 + m←1×´⊑⟜𝕨⌜↕i {c↩×-´𝕩⋄m↩m×⌊´𝕩}∘(⊑¨⟜𝕨‿𝕩)⍟(r⊸>)i m }○(((-1+↕r)+≠)⊸{⊑⟜𝕩⌜𝕨})𝕩 @@ -607,14 +607,14 @@ _bins←{ c←1-˜=𝕨 "⍋ or ⍒: Rank of 𝕨 must be at least 1" ! 0≤c "⍋ or ⍒: Rank of 𝕩 must be at least cell rank of 𝕨" ! c≤=𝕩 - lw←×´sw←1 Cell 𝕨 + lw←1×´sw←1 Cell 𝕨 cw←lw 𝔽○(⊑⟜(⥊𝕨)) _getCellCmp 0 - "⍋ or ⍒: 𝕨 must be sorted" ! 0⊸<◶⟨1,∧´0≤˜·cw⟜(lw⊸+)⌜lw×↕∘-⟜1⟩≠𝕨 + "⍋ or ⍒: 𝕨 must be sorted" ! 0⊸<◶⟨1,1∧´0≤˜·cw⟜(lw⊸+)⌜lw×↕∘-⟜1⟩≠𝕨 cx←c-˜=𝕩 sx←cx Cell ToArray 𝕩 ⋄ lc←sw CmpLen sx cc ← (⊑⟜(⥊𝕨))⊸𝔽⟜(⊑⟜(⥊𝕩)) _getCellCmp´ lc - B←(×´sw)⊸×⊸Cc≤0˜ - 0 Fill (≠𝕨)⊸{B⟜𝕩 _binSearch 𝕨}⌜ (×´sx) × ⥊⟜(↕×´)⊑⟜(≢𝕩)⌜↕cx + B←(1×´sw)⊸×⊸Cc≤0˜ + 0 Fill (≠𝕨)⊸{B⟜𝕩 _binSearch 𝕨}⌜ (1×´sx) × ⥊⟜(↕1×´⊢)⊑⟜(≢𝕩)⌜↕cx } ⚇ ← _depthOp_ @@ -658,7 +658,7 @@ Find←{ ReorderChk←{ "𝕨⍉𝕩: 𝕨 must have rank at most 1" ! 1≥=𝕨 "𝕨⍉𝕩: Length of 𝕨 must not exceed rank of 𝕩" ! 𝕨≤○≠≢𝕩 - "𝕨⍉𝕩: 𝕨 must consist of natural numbers" ! ∧´Nat⌜⥊𝕨 + "𝕨⍉𝕩: 𝕨 must consist of natural numbers" ! 1∧´Nat⌜⥊𝕨 } ReorderAxesSub←{ (𝕨⊸⊏Pick𝕩˙)⌜↕⌊´⌜𝕨⊔≢𝕩 @@ -666,15 +666,15 @@ ReorderAxesSub←{ ReorderAxes←{ 𝕨 ReorderChk 𝕩 𝕨↩⥊𝕨 - r←(=𝕩)-+´¬∊𝕨 - "𝕨⍉𝕩: Skipped result axis" ! ∧´𝕨