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
|
# SPDX-FileCopyrightText: Copyright © 2025-2026 sterni
# SPDX-License-Identifier: GPL-3.0-only
# datetime.bqn
date ⇐ •Import "datetime.bqn"
AssertMatches ← {((•Repr 𝕨)∾"≢"∾•Repr 𝕩)!𝕨≡𝕩}
!∘date.IsLeap¨ ⟨400, 2000, 2016, 2024, 2032⟩
!∘¬∘date.IsLeap¨ ⟨100, 300, 1000, 1999, 2007⟩
! date.epochDelta=62167219200
! 146097=date.YearDays 400
⟨R⟩ ← •Import "run.bqn"
⟨FmtInt⟩ ← •Import "integer.bqn"
⟨_join⟩ ← •Import "string.bqn"
static ← ⟨¯1-date.epochDelta,0-date.epochDelta,1-date.epochDelta,¯1,0,1⟩
{𝕊 t:
•Out "date.FromUnix "∾•Fmt t
d ← date.FromUnix t
t AssertMatches date.ToUnix d
# TODO(sterni): ToRfc3339 uses 4 digit years, GNU date(1) more as needed
exp ← R "date"‿"--utc"‿"+%-Y,%-m,%-d,%-H,%-M,%-S"‿("--date=@"∾FmtInt t)
exp AssertMatches ',' _join FmtInt¨ d.year‿d.month‿d.day‿d.hour‿d.minute‿d.second
}¨ static∾⥊date.epochDelta‿0+¯1‿1×⌜1000 •rand.Range ¯1+2⋆53
# integer.bqn
⟨ParseInt,QuotRem,Tr,ToMixedRadix⟩ ⇐ •Import "integer.bqn"
! ((ParseInt •Fmt)¨ ≡ ⊢) (-∾⊢) 1000 •rand.Range 4294967296
! ((ParseInt FmtInt)¨ ≡ ⊢) (-∾⊢) 1000 •rand.Range 4294967296
! 493‿420≡8⊸ParseInt¨ "0755"‿"644"
! "755"‿"644"≡8⊸FmtInt¨ 493‿420
! ∧´"10"⊸≡¨ FmtInt˜¨ 2‿5‿8‿10
! 1=Tr 1.8
! ¯1=Tr ¯1.6
! 8‿1≡25 QuotRem 3
! 3‿0≡21 QuotRem 7
! ¯1‿¯2≡¯5 QuotRem 3
! 2‿5‿12‿23≡∞‿24‿60‿60 ToMixedRadix 191543
! (-2‿5‿12‿23)≡∞‿24‿60‿60 ToMixedRadix ¯191543
# string.bqn
⟨DropPrefix⟩ ← •Import "string.bqn"
! "o"≡"fo" DropPrefix "foo"
! ∧´"lol"‿""‿"bogus"‿"foobar"‿"b"‿"o"‿"oo"‿"Foo"‿"fOo" (⊢≡DropPrefix)¨ <"foo"
# posix-path.bqn
⟨ParseCanonicalPath, IsRelative, SplitExt⟩ ← •Import "posix-path.bqn"
# from the test suite of Haskell's filepath
"/"‿"directory"‿"file.ext" AssertMatches ParseCanonicalPath "/directory/file.ext"
"test"‿"file" AssertMatches ParseCanonicalPath "test/file"
"/"‿"test"‿"file" AssertMatches ParseCanonicalPath "/test/file"
⟨⟩ AssertMatches ParseCanonicalPath ""
"/"‿"test"‿"file" AssertMatches ParseCanonicalPath "/test///file"
! IsRelative "foo/bar"
! IsRelative "./foo/bar"
! IsRelative " /foo"
! IsRelative ⟨"foo"⟩
! ¬IsRelative "/foo"
! ¬IsRelative "/"
! ¬IsRelative "/"‿"foo"
! "foo"‿⟨⟩ AssertMatches SplitExt "foo"
! "links"‿".org" AssertMatches SplitExt "links.org"
! "pub/links"‿".org" AssertMatches SplitExt "pub/links.org"
! ".git/exclude"‿"" AssertMatches SplitExt ".git/exclude"
! "/depot/"‿".gitignore" AssertMatches SplitExt "/depot/.gitignore"
! "pandoc.nix"‿".golden" AssertMatches SplitExt "pandoc.nix.golden"
! "/home/alice/.local/share/data.secret/foo.json"‿".br" AssertMatches SplitExt "/home/alice/.local/share/data.secret/foo.json.br"
# kv.bqn
kv ← •Import "kv.bqn"
simpleObj ← "foo"‿"bar"‿"baz"≍1‿2‿3
nestedObj ← "hello"‿"world"≍⟨4,5,6⟩‿simpleObj
! 3=simpleObj kv.Get "baz"
! 4=simpleObj 4 kv._GetDef "f0o"
! 2=nestedObj kv.GetPath "world"‿"bar"
simpleObj AssertMatches nestedObj kv.Get "world"
! 1=kv.Get⟜"foo" simpleObj kv.Set "bar"‿0
! 0=kv.Get⟜"bar" simpleObj kv.Set "bar"‿0
! 3=kv.Get⟜"baz" simpleObj kv.Set "bar"‿0
! 3=≠⊏ simpleObj kv.Set "bar"‿0
! 1=kv.Get⟜"foo" simpleObj kv.Set "nof"‿5
! 2=kv.Get⟜"bar" simpleObj kv.Set "nof"‿5
! 3=kv.Get⟜"baz" simpleObj kv.Set "nof"‿5
! 5=kv.Get⟜"nof" simpleObj kv.Set "nof"‿5
! 4=≠⊏ simpleObj kv.Set "nof"‿5
|