aboutsummaryrefslogtreecommitdiff
path: root/spec/reference.bqn
blob: 124b5dd9f823a4b6cbf7d792d1ecb8f05f3eeba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# This file gives reference implementations of BQN primitives assuming
# limited initial functionality. Implementations are designed to be
# simple and not fast.

# In some cases an operation is defined with limited functionality at
# first and later expanded. For convenience, rather than renaming these
# limited versions, every primitive use refers to the most recent
# definition in source code, as if redefinitions shadowed previous
# primitive definitions.


#⌜
# LAYER 0: Assumed functionality

# IEEE 754, except NaN results cause an error and -0 is converted to 0.
# LIMITED to the stated cases and atomic arguments.
+          #                Add
-          # Negate         Subtract
×          #                Multiply
÷          # Reciprocal     Divide
          # Exponential    Power
          # Floor
=          #                Equals
          #                Less Than or Equal to

# Other basic functionality that we need to assume
Type       # 0 if 𝕩 is an array, 1 if a number, >1 otherwise
!          # 𝕩 is 0 or 1; throw an error if it's 0
          # LIMITED to monadic case
          # LIMITED to array 𝕩 and (×´𝕨)≡≢𝕩
          # LIMITED to natural number 𝕨 and list 𝕩
_amend     # {𝕨˙⌾(𝕗⊸⊑)𝕩}
          # LIMITED to number 𝕩
Identity   # Left or right identity of function 𝕏
          # Inverse of function 𝔽
Fill       # Enclosed fill value for 𝕩


#⌜
# LAYER 1: Foundational operators and functions

# Combinators
  {𝕨((𝕨𝔽𝕩)𝕘){𝔽}𝕩}     # LIMITED to number left operand result
˙  {𝕩𝕗}
  {𝕨((1˙𝕨)-0)𝔽𝔾 𝕩}
  {𝕩}
  {𝕩}{𝕨}
˜  {𝕩𝔽𝕨𝕩}
  {𝔽𝕨𝔾𝕩}
  {(𝔾𝕨)𝔽𝔾𝕩}
  {(𝔽𝕨𝕩)𝔾𝕩}
  {(𝕨𝕩)𝔽𝔾𝕩}
  {𝕨𝔾𝔽𝕩}   # LIMITED to boolean right operand result

IsArray0=Type
Int(1=Type)0,=
Nat(1=Type)0,0≤×⌊=

  IsArray⟨⟩‿  # LIMITED to monadic case

# LIMITED to numeric arguments for arithmetic cases
  (÷2)    (÷˜)
             ×
             (+-×)
¬  1+-
|  ××       {𝕩-𝕨×⌊𝕩÷𝕨}
<  {⟨⟩𝕩}  (¬≤˜)
>             (²)
  !0       (˜)
  Length    (¬=)
=  Rank      =
×  0(<->)   ×
           {𝕨{(𝕨>𝕩)𝕨𝕩}_perv𝕩}
  --     {𝕨{(𝕨<𝕩)𝕨𝕩}_perv𝕩}

¨  _eachm   # LIMITED to monadic case and array 𝕩
´  _fold

Rank  0⊑≢
Length  (0<Rank)10⊑≢

_eachm{
  r𝕩  F𝔽
  E(r){rr𝕩_amend˜F𝕩rE𝕩+1}
  E 0  (𝕩)r
}
{ Identity  𝕨˙=Identity𝕩 }´¨ 
  ×1, ¬1
   , ¯∞
  0 , 1
  0 , =1
  >0 , 1

_fold{
  ! 1==𝕩
  lv𝕩  F𝔽
  r𝕨 (0<l){𝕩Identity f}{ll-1l𝕩} 𝕩
  {r(𝕩v)F r}¨(l-1)-¨l
  r
}


#⌜
# LAYER 2: Pervasion
# After defining _perv, we apply it to all arithmetic functions,
# making them pervasive. I'm not going to write that out.

ToArray  IsArray<

  {k𝕨k𝕨-k𝕩˜¨k+≠𝕩}  # LIMITED to two list arguments

_table{
  ma𝕨  nb𝕩  F𝔽
  rm×n
  {𝕩{rr((n×𝕨)+𝕩)_amend˜(𝕨a)F(𝕩b)}¨n}¨m
  (𝕨𝕩)r
}

_eachd{
  _e{ # 𝕨 has smaller or equal rank
    kp𝕨  q𝕩
    ! ´(p=⊑q)¨k
    l×´(q˜k+)¨q-k
    a𝕨  b𝕩
    q⥊⥊(a) (a𝔽l×+⊑b˙)_table l
  }
  (>=)𝔽_e𝔽˜_e˜
}

  {(𝔽_eachm)(𝔽_table)ToArray}
¨  {(𝔽_eachm)(𝔽_eachd)ToArray}
_perv{ # Pervasion
  (IsArray)𝔽𝔽{𝕨𝔽_perv𝕩}¨
}


#⌜
# LAYER 3: Remove other limits
# Now all implementations are full except ∾; ↕ is monadic only

DeshapeIsArray{𝕩}
Reshape{
  ! 1≥=𝕨
  sDeshape 𝕨
  sp+´p¬Nats
  ! 1sp
  ndDeshape 𝕩
  lsp(×´){
    lp×´p1¨𝕩
    ! 0<lp
    I+´×
    tI e,,,=(I p)s
    ! +´e
    a(2t){!Nat𝕩𝕩},,n÷lp
    spa¨s
    {d(Fill d)𝕩-nn𝕩}(n<)(3=t)lp×a
  } s
  s(l){!0<n𝕩¨n|𝕨}(ln)d
}

Range{
  I{!Nat𝕩𝕩}
  M{!1==𝕩(<⟨⟩)⌜´I¨𝕩}
  IsArrayIM 𝕩
}

Pick1{
  ! 1==𝕨
  ! 𝕨=s𝕩
  ! ´Int¨𝕨
  ! ´𝕨(-∧<)s
  𝕨𝕨+s×𝕨<0
  (𝕩)˜0(𝕨+⊑s×⊢)´-↕¬≠𝕨
}
Pickd(´⥊IsArray¨)Pick1{Pickd𝕩¨𝕨}
PickIsArrayPickd
First(0<≠)!0,0Deshape

match{¬(0𝕨)(1𝕨)𝕩}´
  IsArray , 0
  ¬IsArray, =
  =       , 0
  ´     , 0
  {´𝕨Match¨𝕩}


DepthIsArray0{1+0´Depth¨𝕩}

  First           Pick
  Deshape         Reshape
  Range
  {𝕨((𝕨𝔽𝕩)𝕘){𝔽}𝕩}  # Same definition, new Pick

  Depth           Match
                 (¬Match)


#⌜
# LAYER 4: Operators

>  MergeIsArray   >
  {𝕩}  {𝕨,𝕩}
  >
  _rankOp_
  _depthOp_
  _repeat_
˘  {𝔽¯1}
˝  _insert
`  _scan

DropV {𝕩¨𝕨+↕𝕨-˜𝕩}
Cell  DropV

Merge(0<≠)((≢⥊⊢)Fill){
  c≢⊑𝕩
  ! ´(c≡≢)¨𝕩
  𝕩ToArray˜⌜c
}
ValidateRanks{
  ! 1≥=𝕩
  𝕩𝕩
  ! (1≤∧≤3)𝕩
  ! ´Int¨𝕩
  𝕩
}
_ranks  {21,0 ((⊣-1+|)˜≠⊑¨<) ValidateRanks𝔽}
_depthOp_{
  neg0>n𝕨𝔾_ranks𝕩  F𝔽  B{𝕏}{𝕨˙𝕏}
  _d{
    R(𝕗+neg)_d
    𝕨(2(neg𝕗0)(0𝕗)≥⋈)(R¨R(𝕩˙)¨(𝕨 B r)¨F)𝕩
  }
  𝕨 n _d 𝕩
}
_rankOp_{
  k𝕨(= (0≤⊢)-,0⌈-¨ 𝔾_ranks)𝕩
  Enc{
    f(𝕩)¨𝕨
    c×´s𝕨Cell𝕩
    f⥊⊑(𝕩)¨((s⥊↕c)+c×⊢)¨↕×´f
  }
  Enc(>0×1+≥=)<⊢,Enc,<
  > ((k)Enc𝕨) 𝔽¨ ((1-˜)k)Enc𝕩
}
_insert{
  ! 1≤=𝕩
  𝕨 𝔽´ <˘𝕩
}
_scan{
  ! IsArray 𝕩
  ! 1≤=𝕩
  F𝔽
  cs1 Cell 𝕩
  ! (cs≡≢)𝕨
  lr𝕩
  𝕨 (0<l){
    c×´cs
    {rc(𝕩)F(r)¨l}𝕨
    {rr𝕩_amend˜𝕨F(r)𝕩}(c+)¨l-c
    (𝕩)r
  } 𝕩
}
_repeat_{
  n𝕨𝔾𝕩
  f𝕨𝔽𝕨𝔽⊢𝕩
  lu0
  {!Int𝕩ll𝕩uu𝕩}0 n
  b𝕨{𝕏⊣}˙{𝕨˙{𝔽𝕏⊣}}0
  i𝕩⟩⋄PB{𝕎`i∾↕𝕩}
  pos𝕗 P u
  neg𝕗 0<i,{𝕏}P -l
  (|⊑<0posneg˙)0 n
}


#⌜
# LAYER 5: Structural functions

  0Select        Select
  Prefixes        Take
  Suffixes        Drop
                 Windows
»  Nudge           ShiftBefore
«  NudgeBack       ShiftAfter
  Reverse         Rotate
/  Indices         Replicate

_onAxes_{
  F𝔽
  (𝔾<≡){ # One axis
    ! 1≤=𝕩
    𝕨F𝕩
  }{ # Multiple axes
    ! 1≥=𝕨
    ! 𝕨≠≢𝕩
    R{(𝕨)F(1 DropV 𝕨)R˘𝕩}{0<≠𝕨}
    𝕨R𝕩
  }ToArray
}

SelSub{
  ! IsArray 𝕨
  ! ´⥊Int¨ 𝕨
  ! ´ 𝕨 (-∧<) 𝕩
  𝕨𝕨+(𝕩)×𝕨<0
  c×´s1 Cell 𝕩
  (𝕩)¨(c×𝕨)+s⥊↕c
}
SelectToArray(SelSub _onAxes_ 1)

JoinTo{
  s𝕨𝕩
  a1´k¨s
  ! ´1a-k
  c(k¬a)+(a-1)¨s
  ! ´c
  l+´(a=k)1(⊑⊢)¨s
  (l∾⊑c)𝕨𝕩
}

Take{
  T{
    ! Int 𝕨
    l𝕩
    i(l+1)|¯1l((𝕨<0)×𝕨+l)+↕|𝕨
    i⊏JoinTo(1Cell⥊Fill)(´l=i)𝕩
  }
  𝕨 T _onAxes_ 0 (1˜0𝕨-≠⊢)𝕩
}
Drop{
  s(𝕨)(⊣↑⊢∾˜1˜0⌈-)𝕩
  ((sׯ1𝕨>0)+(-s)s𝕨)𝕩
}
Prefixes  {!1≤=𝕩  (1+≠𝕩)Take¨<𝕩}
Suffixes  {!1≤=𝕩  (1+≠𝕩)Drop¨<𝕩}

ShiftBefore  {!𝕨1=𝕩  ( 𝕩)  𝕨 JoinTo 𝕩}
ShiftAfter   {!𝕨1=𝕩  (-≠𝕩)  𝕩 JoinTo 𝕨}
Nudge      (10↑⊢)ShiftBefore
NudgeBack  (10↑⊢)ShiftAfter

Windows{
  ! 1≥=𝕨
  ! 𝕨≠≢𝕩
  ! ´Nat¨𝕨
  s(𝕨)↑≢𝕩
  ! ´𝕨1+s
  𝕨{((𝕨↓≢𝕩)≢⥊>)<¨𝕩¨s(¬+↕⊢)𝕨}(0<≠𝕨)𝕩
}

Reverse  {!1≤=𝕩  (-↕¬≠𝕩)𝕩}
Rotate  {!Int𝕨  l𝕩(l|𝕨+↕l)𝕩} _onAxes_ 0

Indices{
  ! 1==𝕩
  ! ´Nat¨𝕩
  ⟨⟩´𝕩¨↕≠𝕩
}
Rep  Indices
Replicate  {0<=𝕨}(˜≠Rep⊢){!𝕨=𝕩𝕨Rep𝕩} _onAxes_ (1-0=≠)


#⌜
# LAYER 6: Everything else

  Join            JoinTo
  GroupInds       Group
  Transpose       ReorderAxes
  MarkFirst       (IndexOf˜<≠)
  /             Find
  IndexOf       IndexOf
    Cmp _grade    (  Cmp _bins)
  -Cmp _grade    (-Cmp _bins)
               
               
  OccurrenceCount ProgressiveIndexOf

{ Identity  𝕨˙=Identity𝕩 }´¨  0 , 1 

JoinEmpty  ({!𝕨𝕩𝕨((𝕨×≠)∾↓)𝕩}≢⥊⊢)Fill

Join(0<≠)JoinEmpty, (0<=){!IsArray𝕩>𝕩}{
  ! IsArray 𝕩
  s¨𝕩
  a(𝕩){(s˜(j=𝕩)×)¨𝕨}¨jr=𝕩
  h(¬(´)¨)¨a
  ! ´´¨0h
  o+`¨h
  t(¯1o)↓⊑s
  lh{𝕎𝕩}¨˜(»o){1,𝕨¨𝕩}¨a
  ! s(<t)⌜´h¨¨l
  i(<⟨⟩)⌜´h{((¯1)-𝕩/𝕨¨»)+`𝕩}¨l
  >i<¨(0<≠)¨l/𝕩
}

Group{
  ! IsArray 𝕩
  𝕨ToArray(2>≡)𝕨
  ! 1==𝕨
  {!∧´Int¨𝕩!∧´¯1𝕩}¨𝕨
  n+´r=¨𝕨
  ! n≤=𝕩
  ld(∾≢𝕨)-n↑≢𝕩
  ! ´(0≤∧≤(r/1=r))ld
  drr(0»+`r)ld0
  sdr0,¯1¨𝕨
  𝕨dr(¯1)¨𝕨
  s1+¯1´¨𝕨
  𝕩((¨𝕨)n↓≢𝕩)𝕩
  (𝕨=/𝕩˙)¨s
}
GroupInds{
  ! 1==𝕩
  𝕩   (1<≡)(∾≢¨) 𝕩
}

# Searching
IndexOf{
  c1-˜=𝕨
  ! 0c
  ! c≤=𝕩
  𝕨 (0<≠)0˜c-↓≢, (+˝`)cc ToArray 𝕩
}
MarkFirst{
  ! 1≤=𝕩
  u0𝕩
  (0<≠)⟨⟨⟩,{𝕩u}{uu𝕩1}0˘𝕩
}
Find{
  r=𝕨
  ! r≤=𝕩
  𝕨 r ((1+r-↑≢𝕩)⌊≢𝕨)r 𝕩
}ToArray

ReorderAxes{
  𝕩<(0=≡)𝕩
  ! 1≥=𝕨
  𝕨𝕨
  ! 𝕨≠≢𝕩
  ! ´Nat¨𝕨
  r(=𝕩)-+´¬∊𝕨
  ! ´𝕨<r
  𝕨𝕨𝕨(¬˜/⊢)r
  (𝕨⊏⊑𝕩˙)¨↕⌊´¨𝕨⊔≢𝕩
}
Transpose(0<=)ToArray,(=-1˙)ReorderAxes

# Sorting
Cmp  IsArray{ # No arrays
  𝕨(>-<)𝕩 # Assume they're numbers
}{ # At least one array
  e𝕨-˜(´0=≢)𝕩
  𝕨(e=0)e{
    c𝕨×-(IsArray+=)𝕩
    s𝕨  t𝕩  r𝕨=𝕩
    ls{i+´`𝕨=𝕩m×´i𝕨{c×-´𝕩mm×⌊´𝕩}(¨𝕨𝕩)(r>)im}(r↑⌽)t
    a𝕨b𝕩
    Trav(=l){Trav(1+𝕩)(0=)a Cmp(𝕩)b}c
    Trav 0
  }𝕩
}

_grade{
  ! 1≤=𝕩
  i˜+´˘(𝔽¯1¯1˜𝕩)(0+=0×)>⌜˜i↕≠𝕩
}
_bins{
  c1-˜=𝕨
  ! 0c
  ! c≤=𝕩
  LE𝔽c0˙
  ! (0<≠)1,´·LE˝˘2↕⊢𝕨
  𝕨 (0<≠𝕨)0c,+˝LE¯1 𝕩
}

OccurrenceCount  ˜(⊢-⊏)
ProgressiveIndexOf  {𝕨(((≢∾2˙)⥊≍˘OccurrenceCount)𝕨)𝕩}