Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

J-expressions make Javascript the acceptable Lisp

Name: Anonymous 2016-11-25 13:12

The consistent implementation of key enumeration order of objects in Javascript engines allows us to write specifications of composeable computations using generic JSON-serializable notation that we can call “J-expressions”.

{opname: main_parameter,
keyword1: value1,
keyword2: value2,
...}


J-expressions, in combination with functions that implement what the operation named opname is supposed to do, are then a systematic way to approach DSL construction within Javascript. The notation is as expressive as s-expressions.

Name: Anonymous 2016-11-26 7:20

>>1
what's the point of using dictionaries? why not just use arrays?

[opname, arg1, arg2, ...]

var named_functions = {
'add': function(a) {
return a.reduce((x, y) => x + y, 0)
},
'mul': function(a) {
return a.reduce((x, y) => x * y, 1)
},
'car': function(a) {
return a[0]
}
}
var interpret = function(jexpr) {
if (jexpr.length < 2) {
throw `Bad jexpr: ${JSON.stringify(jexpr)}`
}
var [ car, ...cdr ] = jexpr
if (Array.isArray(car)) {
car = interpret(car)
}
if (typeof car === "string" && named_functions.hasOwnProperty(car)) {
var op = named_functions[car]
} else {
var op = car
}
console.log(`op: ${op}`)
return op(interpret_params(cdr))
}
var interpret_params = function(params) {
if (params.length === 0) {
return []
}
var [ car, ...cdr ] = params
if (Array.isArray(car)) {
return [interpret(car), ...interpret_params(cdr)]
}
return [ car, ...interpret_params(cdr) ]
}


Examples:

interpret(['car', ['mul', 4, ['add', 6, 1]]])
;; => 28
interpret([(x) => Math.floor(x / 2), ['mul', 4, ['add', 6, 1]]])
;; => 14


fuck javashit tho tbh fam

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List