Created
January 16, 2015 05:38
-
-
Save pmuellr/bd1633070b4f373dbcf2 to your computer and use it in GitHub Desktop.
es6 template strings for APL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// es6 template strings for APL | |
// npm install apl | |
"use strict" | |
// from: https://github.com/ngn/apl/blob/master/examples/1-mult.apl | |
// note: http://s.mlkshk-cdn.com/r/97VP | |
apl` | |
⍝ Multiplication table | |
⍝ a × b scalar multiplication, "a times b" | |
⍝ ∘. is the "outer product" operator | |
⍝ A ∘.× B every item in A times every item in B | |
⎕ ← (⍳ 10) ∘.× ⍳ 10 | |
` | |
// prints: | |
// 0 0 0 0 0 0 0 0 0 0 | |
// 0 1 2 3 4 5 6 7 8 9 | |
// 0 2 4 6 8 10 12 14 16 18 | |
// 0 3 6 9 12 15 18 21 24 27 | |
// 0 4 8 12 16 20 24 28 32 36 | |
// 0 5 10 15 20 25 30 35 40 45 | |
// 0 6 12 18 24 30 36 42 48 54 | |
// 0 7 14 21 28 35 42 49 56 63 | |
// 0 8 16 24 32 40 48 56 64 72 | |
// 0 9 18 27 36 45 54 63 72 81 | |
//------------------------------------------------------------------------------ | |
function apl(lits) { | |
let APL = require("apl") | |
let subs = [].slice.call(arguments, 1) | |
let source = [] | |
for (let i=0; i<subs.length; i++) { | |
source.push(lits[i]) | |
source.push(subs[i]) | |
} | |
source.push(lits[lits.length-1]) | |
source = source.join("") | |
let result | |
let ePrefix = `error compiling '${JSON.stringify(source)}'` | |
try { | |
result = APL(source) | |
} | |
catch (e) { | |
throw new Error(`${ePrefix}: error compiling APL: ${e.message}`) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment