Skip to content

Instantly share code, notes, and snippets.

@rodrigogiraoserrao
Created August 6, 2020 15:51
Show Gist options
  • Save rodrigogiraoserrao/ddc89dbebab4fd7a812ea83f517c6042 to your computer and use it in GitHub Desktop.
Save rodrigogiraoserrao/ddc89dbebab4fd7a812ea83f517c6042 to your computer and use it in GitHub Desktop.
RGS's solution for Problem 6, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
PROBLEM 6.1
Merge {
(⎕IO ⎕ML ⎕WX) 0 1 3
Merges JSON data from a file named ⍵ into a template file named ⍺.
Dyadic function expecting a character vector on the left (template path) and a character vector on the right (data path).
Returns a character vector.
t ⎕NGET
G (⎕JSON⎕NGET )_SafeGet
R {
Convert matched regex patterns into the appropriate replacements.
.PatternNum=0: '???' G 1¯1.Match drop surrounding @
.PatternNum=1: '@'
}
('@\w+?@' '@@' ⎕R R)t
}
_SafeGet ← {
(⎕IO ⎕ML ⎕WX) ← 0 1 3
6::⍺ ⍝ Catch ValueErrors, cf. http://help.dyalog.com/17.1/index.htm#Language/Errors/APL%20Errors.htm#APLErrors
⍺⍺⍎⍵
}
_SafeGet {
(⎕IO ⎕ML ⎕WX) 0 1 3
Utility function to safely retrieve ⍵ from the namespace ⍺⍺.
Monadic operator deriving a dyadic function, expects a namespace left operand, a value on the left and a character vector on the right.
Returns ⍺⍺.⍵ unless ⍵ doesn't exist in ⍺⍺, in which case returns ⍺.
~()⍺⍺.⎕nl ¯2:
⍺⍺
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment