Skip to main content

utils

Native BasedPL primitives for strings, lines, HTML regex extraction, paths, JSON I/O, rate-limited •fetch, and answer coercion.

Error Kinds

Error kinds for •signal and :: error guards (missing tokens, locked puzzles, and invalid submissions).

%%bpl
#| export_bpl: true
AocdError ← "AocdError"
PuzzleLockedError ← "PuzzleLockedError"
DeadTokenError ← "DeadTokenError"

Tacit String, Line, & Record Combinators

Native tacit BasedPL functions built with trains, ∘, ⍥, ↣, ↢, ᵘ, ¨, and ⍚.

%%bpl
#| export_bpl: true
_nl ← ,•ucs 10ₓ
_cr ← ,•ucs 13ₓ
_ws ← " ",•ucs 9 13 10ₓ

bstrip ← •replace↢["^\s+|\s+$" ""]
brstrip_nl ← •replace↢["[\r\n]+$" ""]
_lower ← ¯1•c
%%bpl
#| export_bpl: true
bhas ← ∨/⍷
bstartswith ← ⊣≡≢↣↑
bwords ← ~∊↢_ws ↣⊆
%%bpl
#| export_bpl: true
_bjoin_ne ← (≢⊣) ↓ ∊((⊂⊣),¨⊢)
bjoin ← (0<≢ᵘ)⍚["" _bjoin_ne]
%%bpl
#| export_bpl: true
_is_nonempty_str ← (0<≢) ∧ 1=≡
_split_ne ← 1↣↓¨ ∘ ((=↢_nl)↣⊂) ∘ (_nl,) ∘ (~↢_cr)
_split_str ← ((0<≢)⍚[(•json "[]") _split_ne]) ∘ brstrip_nl
bsplit_lines ← _is_nonempty_str⍚[((0<≢)⍚[(•json "[]") ⊢]) _split_str]

String coercion, structural equality, and keyed record update.

%%bpl
#| export_bpl: true
bstr ← {
  k ← (•storage ω).kind
  k≡"character" ? (0=≢ρω)⊃[ω (,ω)] ;
  ("int" bhas k) ∧ 0=≢ρω ? •json⁻ ω ;
  (k≡"rational") ∧ (0=≢ρω) ∧ ω=⌊ω ? •json⁻ ω ;
  ⍕ω
}
%%bpl
#| export_bpl: true
beq ← ≡
%%bpl
#| export_bpl: true
rec_set ← {
  [k v] ← ω
  (⊂k) ∊ ι₀α ? r←α ◇ (k⌷r)←v ◇ r ;
  α,k:v
}
%%bpl
_assert ← { ω ? $t ; "assertion failed" •signal "ASSERT" }
_assert (bstrip "  hello ",_nl," ") ≡ "hello"
_assert (bsplit_lines "L68",_nl,"L30",_nl,_nl,"R48",_nl) ≡ ["L68" "L30" "" "R48"]
_assert (", " bjoin ["aa" "bb" "cc"]) ≡ "aa, bb, cc"

Pure Native HTML Extraction & Entity Decoding

Replaces a full DOM parser with native BasedPL •r, •replace, seeded / fold, 16⊥, and •ucs.

%%bpl
#| export_bpl: true
_HEX ← "0123456789abcdef"
_NAMED_ENTITIES ← [["&lt;" "<"] ["&gt;" ">"] ["&quot;" """"] ["&apos;" "'"] ["&#39;" "'"] ["&nbsp;" (,•ucs 160ₓ)] ["&mdash;" (,•ucs 8212ₓ)] ["&ndash;" (,•ucs 8211ₓ)] ["&hellip;" (,•ucs 8230ₓ)] ["&amp;" "&"]]
_dec_char ← ,∘•ucs∘↑∘(1⊃•vfi)
_hex_char ← ,∘•ucs∘(16⊥(_HEXι_lower))
_unescape_named ← •replace⍨/↢(⌽_NAMED_ENTITIES)
_strip_tags ← •replace↢["(?s)<[^>]+>" ""]
%%bpl
#| export_bpl: true
_decode_entities ← {
  m ← ω •r α
  0=≢m.text ? ω ;
  um ← ≠m.text
  ω •replace⍨/ ⌽((um#m.text) {[α ω]}¨ ⍶¨↑¨um#m.groups)
}
%%bpl
#| export_bpl: true
unescape_html ← {
  s ← bstr ω
  0=≢s ? s ;
  s ← "&#([0-9]+);" (_dec_char _decode_entities) s
  s ← "&#[xX]([0-9a-fA-F]+);" (_hex_char _decode_entities) s
  _unescape_named s
}
%%bpl
#| export_bpl: true
_parse_tag ← { "character"≡(•storage ω).kind ? [ω $f] ; ω }

tag_texts ← {
  [tag strip_ws] ← _parse_tag α
  gs ← ((bstr ω) •r "(?is)<",tag,"\b[^>]*>(.*?)</",tag,">").groups
  0=≢gs ? •json "[]" ;
  out ← unescape_html∘_strip_tags¨ ↑¨gs
  strip_ws ? bstrip¨out ;
  out
}
%%bpl
_assert (unescape_html "a &lt; b &amp; &#65; &#x42;") ≡ "a < b & A B"
_assert (0⊃("pre" tag_texts "<article><pre><code>L68",_nl,"&lt;x&gt;</code></pre></article>")) ≡ "L68",_nl,"<x>"

Tacit Paths, Filesystem, & JSON I/O

Tacit expand_path, file_name, is_file, and join_path with •nput/•nget.

%%bpl
#| export_bpl: true
_pick_empty ← ⊃⍠["fill":""]
_get_home ← {
  env ← •host "env"
  h ← "HOME" _pick_empty env
  0<≢h ? h ;
  "USERPROFILE" _pick_empty env
}

expand_path ← (("~"≡⊢) ∨ ("~/"bstartswith))⍚[⊢ (_get_home , 1↓)]
_parent_dir ← (⊢.parent)∘•path∘expand_path
file_name ← (⊢.name)∘•path∘expand_path
_path_kind ← ↑∘(⊢.kind)∘•metadata∘expand_path
_is_dir ← "dir"≡_path_kind
is_file ← (∊↢["file" "symlink"])∘⊂∘_path_kind
%%bpl
#| export_bpl: true
env_get ← {
  env ← •host "env"
  k ← bstr ω
  (⊂k) ∊ ι₀env ? k⊃env ;
  ⍬
} ⊘ {
  (bstr ω) (⊃⍠["fill":(bstr α)]) •host "env"
}
%%bpl
#| export_bpl: true
join_path ← (•replace↢["/+" "/"])∘("/"bjoin)∘((0<≢¨)↣#)∘(bstr¨)
%%bpl
#| export_bpl: true
read_text ← {
  ep ← expand_path ω
  ~is_file ep ? ep •signal "IO" ;
  •nget ep
}
%%bpl
#| export_bpl: true
write_text ← {
  ep ← expand_path α
  par ← _parent_dir ep
  _ ← (0<≢par) {α ? (~_is_dir ω) {α ? •mkdir ω ; 0} ω ; 0} par
  dir ← (0<≢par)⊃["." par]
  ep •rename ["path":dir "unique":1 "prefix":".nbaocd_tmp_"] •nput bstr ω
}
%%bpl
#| export_bpl: true
read_json ← •json∘read_text
write_json ← write_text↢(•json⁻)

Functional Rate-Limited HTTP (•fetch) & Answer Coercion

Rate-limited http_get/http_post via •fetch, form encoding, and answer coerce.

%%bpl
#| export_bpl: true
_BASE_HEADERS ← ["User-Agent":"github.com/AnswerDotAI/nbaocd nbaocd/0.1.0"]

_HEX_UPPER ← "0123456789ABCDEF"
_UNRESERVED_BYTES ← "UTF-8" •ucs ∊[$a (¯1•c $a) $d "-_.~"]
_hex_byte ← (⊂∘("%",)∘(_HEX_UPPERι⁻)∘(16 16⊤))⁰
_sel_byte ← (⊽∘~∘(∊↢_UNRESERVED_BYTES)) - (=↢32ₓ)
_percent_encode ← (0<≢)⍚["" (∊∘(_sel_byte⍚[(⊂•ucs)⁰ (⊂"+") _hex_byte])∘("UTF-8"•ucs))]
_pair_eq ← (⊣,"=",⊢)⍥_percent_encode

_http_max_t ← 3.0
_http_cooloff ← 0.16
_http_history ← 4ρ(•time 0) - _http_max_t
%%bpl
#| export_bpl: true
form_encode ← {
  ks ← ι₀ω
  0=≢ks ? "" ;
  "&" bjoin (bstr¨ks) _pair_eq¨ bstr¨:ω
}
%%bpl
#| export_bpl: true
_rate_limit ← {
  now ← •time 0
  (now - ↑_http_history) < _http_max_t ? •delay _http_cooloff ◇ _http_cooloff(10.0⌊⊽)← ◇ _http_history ⊢← (1↓_http_history),now ;
  _http_history ⊢← (1↓_http_history),now
}
%%bpl
#| export_bpl: true
_headers ← {
  [tok extra] ← ω
  hdrs ← (tok≡⍬) {α ? _BASE_HEADERS ; _BASE_HEADERS rec_set ["Cookie" ("session=",bstr tok)]} 0
  extra≡⍬ ? hdrs ;
  hdrs rec_set⍨/ ⌽((ι₀extra) {[α (bstr ω)]}¨ :extra)
}
%%bpl
#| export_bpl: true
http_get ← {
  α ← ⍬
  _rate_limit 0
  ["headers":(_headers [α ⍬])] •fetch bstr ω
}
%%bpl
#| export_bpl: true
http_post ← {
  [tok fields] ← α
  _rate_limit 0
  ["headers":(_headers [tok ["Content-Type":"application/x-www-form-urlencoded"]]) ; "body":(form_encode fields)] •fetch bstr ω
}
%%bpl
#| export_bpl: true
coerce ← {
  k ← (•storage ω).kind
  (k≡"character") ∧ 1<≢ρω ? "Failed to coerce value to str" •signal AocdError ;
  k≡"character" ? (0=≢ρω)⊃[ω (,ω)] ;
  k≡"bool" ? "Failed to coerce bool value to str" •signal AocdError ;
  (0<≢ρω) ∧ ~∧/1=ρω ? "Failed to coerce value to str" •signal AocdError ;
  v ← ↑,ω
  kv ← (•storage v).kind
  kv≡"bool" ? "Failed to coerce bool value to str" •signal AocdError ;
  "int" bhas kv ? •json⁻ v ;
  (kv≡"rational") ∧ v=⌊v ? •json⁻ v ;
  ("float" bhas kv) ∧ (v=v) ∧ (∞>|v) ∧ v=⌊v ? •json⁻ ⌊ₓv ;
  "complex" bhas kv ? [re im]←∨v ◇ ((im=0) ∧ (re=re) ∧ (∞>|re) ∧ re=⌊re) {α ? •json⁻ ⌊ₓω ; "Failed to coerce value to str" •signal AocdError} re ;
  "Failed to coerce value to str" •signal AocdError
}
%%bpl
_assert (form_encode ["level":"1" "answer":"hello world+1"]) ≡ "level=1&answer=hello+world%2B1"
_assert (coerce 42ₓ) ≡ "42"
_assert (coerce 42.0) ≡ "42"
_assert (coerce 42j0) ≡ "42"
_assert {AocdError :: $t ◇ _ ← coerce $t ◇ $f} 0