aboutsummaryrefslogtreecommitdiff
path: root/examples/fifty.bqn
blob: cc452ce4f3c6ec2b61a88cf537fd910f4514828b (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
# A history of APL in 50 functions
# But they're BQN functions
# Still about APL history though
# Also not all the functions are here yet
# https://www.jsoftware.com/papers/50/

# Utilities
Words  (⊢-˜¬×+`)=' '  # Split string on spaces

# 0
ArrayLogic  (>-<)0
! 1¯101  ArrayLogic 5¯2.706

# 1
Average  +´÷≠
! 3  Average 216

# 2
# x⌹x=x is an alternate way to get the average of vector x.
# There's no reason to do it this way in BQN (or APL, really).

# 3
IndexOfSelfie  ˜
! 012341  IndexOfSelfie "Selfie"

# 4
BarChart  ".⎕"˜>(↕⌈´)
! {
  bc  BarChart 314159
  bc  69"⎕⎕⎕......⎕........⎕⎕⎕⎕.....⎕........⎕⎕⎕⎕⎕....⎕⎕⎕⎕⎕⎕⎕⎕⎕"
}

# 5
ParenthesesNesting  +`1¯10˜"()"⊐⊢
! {
  pn  ParenthesesNesting "⍵((∇<S),=S,(∇>S))⍵⌷⍨?≢⍵"
  pn  01222211111222210000000
}

# 6
PerfectShuffle  ⊢⊏˜·⍒≠⥊01˜
! "IAJBKCLDMENFOGPHQ"  PerfectShuffle "ABCDEFGHIJKLMNOPQ"

# 7
_quicksort  {Cmp𝔽  S{p𝕩Cmp˜𝕩˜•Rand≠𝕩  101𝕩{S𝕨𝕗/˜0𝕏p}¨<=>}(1<≠)}
! 1223579101010111416  -_quicksort 2271010113101459116
! {
  Cmp  0(-´⍋∾<)¨
  srt  Words "Anna Fi JD Jay Jd John Morten Roger Scott Zeus"
  srt  Cmp _quicksort Words "Fi Jay John Morten Roger JD Jd Anna Scott Zeus"
}

# 8
PascalsTriangle  {>𝕩¨0(∾+∾˜)(𝕩)1}
! (>1000110012101331)  PascalsTriangle 4
! 11235813213455  { 𝕩  +´¨ (+⌜˜𝕩)  PascalsTriangle 𝕩 } 10

# 9
GoldenRatio  +÷´¨(1↓↑) 1
! 1e¯5 ´> | (GoldenRatio 16) - 121.51.666671.61.6251.615381.619051.617651.618181.617981.618061.618031.618041.618031.61803

# 10
NewtonsMethod  (2 ÷˜  + ÷)´¨(1↓↑) 
! 1e¯5 ´> | (7 NewtonsMethod 2) - 21.51.416671.414221.414211.414211.41421

# 11
InnerProduct  +˝×12
! (>2341)  (>10¯11) InnerProduct >2364

# 12
CayleysTheorem  {R⥊⊐⊢  (R𝕩)  R ˜⌜˜ <˘R𝕩}
! {
  t  22¨ (2|⌊÷2(4))¨ 967111314
  g  ˝12⌜˜t
  CayleysTheorem g
}

# 14
IntervalIndex  1-˜(⊣↓⊢⍋⊏+`>)  # Or ⍋, of course
! 02324102¯1  ¯12378.5 IntervalIndex 047611213¯5
! 010¯144  "Fi Jay John Morten Roger" IntervalIndexWords "JD Jd Geoff Anna Scott Zeus"

# 15
CentralLimit  { 41  / (5×↕40)  +˝ •Rand 𝕩21 }
# 5‿8 ⥊ CentralLimit 10 1e3

# 17
SeventeenTwentyNine  {F:
  c  3˜1+↕200
  t  (<⌜˜200) × +⌜˜c
  d   t
  ´ ¨ (2=≠¨)/ d
}
# ! 1729 ≡ SeventeenTwentyNine⟨⟩  # slow

# 24
SymmetricArray    1
! SymmetricArray +⌜´ 3⥊<23711

# 34
_pow  {𝔽´𝔽˜(/2|⌊÷2(·2)𝕩)𝕨}
! 847288609443  3 ×_pow 25

# 38
Hanoi  {¯1(⥊⊢≍0˜≠⥊⊑⊑152034˜)𝕩1}
! 0130450  Hanoi 3