@fraczak/k
Advanced tools
Comparing version 1.1.1 to 1.1.2
21
codes.js
@@ -6,3 +6,3 @@ // Generated by CoffeeScript 2.7.0 | ||
are_different = function(classes, representatives, name1, name2, codes) { | ||
var code1, code2, field, fields1, fields2, rep; | ||
var arg1, arg2, code1, code2, field, fields1, fields2, rep; | ||
if (name1 === name2) { | ||
@@ -21,5 +21,8 @@ return false; | ||
[fields1, fields2] = [code1, code2].map(function(code) { | ||
return Object.keys(code[code.code]).reduce(function(fields, label) { | ||
return fields[label] = representatives[code[code.code][label]]; | ||
}, {}); | ||
return (function(arg) { | ||
return Object.keys(arg).reduce(function(fields, label) { | ||
var ref; | ||
return fields[label] = (ref = representatives[arg[label]]) != null ? ref : arg[label]; | ||
}, {}); | ||
})(code[code.code]); | ||
}); | ||
@@ -35,5 +38,11 @@ if (Object.keys(fields1).length !== Object.keys(fields2).length) { | ||
} | ||
return false; | ||
break; | ||
case "vector": | ||
return representatives[code1.vector] !== representatives[code2.vector]; | ||
[arg1, arg2] = [code1, code2].map(function({vector}) { | ||
var ref; | ||
return (ref = representatives[vector]) != null ? ref : vector; | ||
}); | ||
if (arg1 !== arg2) { | ||
return true; | ||
} | ||
} | ||
@@ -40,0 +49,0 @@ return false; |
{ | ||
"name": "@fraczak/k", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "k-language for JSON-like data transformation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
112
README.md
# k-language | ||
npm install @fraczak/k | ||
Another JSON transformation notation. A `k`-expression | ||
@@ -58,7 +60,7 @@ (script) defines a __partial function__, i.e., a function which, in | ||
< [.0, .1] GT, [.1, .0] GT > .0 | ||
< GT .0, .1> | ||
which selects the bigger of two first integers of a vector argument, i.e., | ||
which selects the maximum element in two element vector, i.e., | ||
[3,8] < [.0, .1] GT, [.1, .0] GT > .0 --> 8 | ||
[3,8] < GT .0, .1 > --> 8 | ||
@@ -76,4 +78,15 @@ ## User defined functions | ||
## Value encodings (also called _types_ or _codes_) | ||
Another example could be finding the biggest (max) value in a vector: | ||
max = < | ||
SNOC -- [x0, x1, x2, ...] -> [x0, [x1, x2, ...]] | ||
[.0, .1 max] -- [x0, [x1, x2, ...]] -> [x0, max(x1,x2,...)], i.e., recursive call | ||
<GT.0,.1>, -- if x0 > max(x1,x2,...) then x0 else max(x1,x2,...) | ||
-- when SNOC is not defined, e.g. the argument is a singleton vector [x0] | ||
.0 -- [x0] -> x0, | ||
>; | ||
max | ||
## Value encodings (also called _codes_) | ||
There are three predefined value encodings: `int`, `string`, and `bool`. The language | ||
@@ -100,7 +113,8 @@ supports `code`-expressions: | ||
$tree = <string leaf, {tree left, tree right} tree>; | ||
$ tree = <string leaf, {tree left, tree right} tree>; | ||
inc = [(),1] PLUS; | ||
max = <GT .0, .1>; | ||
height = $ tree < | ||
.leaf 0, | ||
.tree [.left height, .right height] <GT .0, .1> inc | ||
.tree [.left height, .right height] max inc | ||
> $ int; | ||
@@ -130,3 +144,3 @@ height | ||
$ ./k.coffee '<["x=",.x," & y=",.y],["only x=",.x],["only y=",.y],["no x nor y"]>{CONCAT "x&y"}' | ||
$ ./node_modules/.bin/k '<["x=",.x," & y=",.y],["only x=",.x],["only y=",.y],["no x nor y"]>{CONCAT "x&y"}' | ||
@@ -157,7 +171,7 @@ {"y": 123, "x": 432,"others": "..."} --> {"x&y":"x=432 & y=123"} | ||
$ ./k.coffee -k test.k | ||
$ ./node_modules/.bin/k -k test.k | ||
If we want to read `json` objects from a file, e.g., `my-objects.json`, we do | ||
$ ./k.coffee -k test.k my-objects.json | ||
$ ./node_modules/.bin/k -k test.k my-objects.json | ||
{"x&y":"x=432 & y=123"} | ||
@@ -178,50 +192,60 @@ {"x&y":"onlyx=987"} | ||
# Using from `javascript` | ||
var k = require("k"); | ||
var k = require("k"); | ||
var k_expression; | ||
k_expression = '()'; | ||
k.run(k_expression,"ANYTHING..."); | ||
// RETURNS: "ANYTHING..." | ||
k_expression = '()'; | ||
k.run(t_expression,"ANYTHING..."); | ||
// RETURNS: "ANYTHING..." | ||
k_expression = '{"ala" name, 23 age}'; | ||
k.run(k_expression,"ANYTHING..."); | ||
// RETURNS: {"name":"ala","age":23} | ||
k_expression = '{"ala" name, 23 age}'; | ||
k.run(k_expression,"ANYTHING..."); | ||
// RETURNS: {"name":"ala","age":23} | ||
k_expression = '[.year, .age]'; | ||
k.run(k_expression,{"year":2002,"age":19}); | ||
// RETURNS: [2002,19] | ||
k_expression = '[.year, .age]'; | ||
k.run(k_expression,{"year":2002,"age":19}); | ||
// RETURNS: [2002,19] | ||
k_expression = '[(), ()]'; | ||
k.run(k_expression,"duplicate me"); | ||
// RETURNS: ["duplicate me","duplicate me"] | ||
k_expression = '[(), ()]'; | ||
k.run(k_expression,"duplicate me"); | ||
// RETURNS: ["duplicate me","duplicate me"] | ||
k_expression = '[[[()]]]'; | ||
k.run(k_expression,"nesting"); | ||
// RETURNS: [[["nesting"]]] | ||
k_expression = '[[[()]]]'; | ||
k.run(k_expression,"nesting"); | ||
// RETURNS: [[["nesting"]]] | ||
k_expression = '[[()]] {() nested, .0.0 val}'; | ||
k.run(k_expression,"nesting and accessing"); | ||
// RETURNS: {"nested":[["nesting and accessing"]],"val":"nesting and accessing"} | ||
k_expression = '[[()]] {() nested, .0.0 val}'; | ||
k.run(k_expression,"nesting and accessing") | ||
// RETURNS: {"nested":[["nesting and accessing"]],"val":"nesting and accessing"} | ||
k_expression = '0000'; | ||
k.run(k_expression,{"test":"parse integer"}); | ||
// RETURNS: 0 | ||
k_expression = '0000'; | ||
k.run(k_expression,{"test":"parse integer"}); | ||
// RETURNS: 0 | ||
k_expression = '[.y,.x] PLUS'; | ||
k.run(k_expression,{"x":3,"y":4}); | ||
// RETURNS: 7 | ||
k_expression = '[.y,.x] PLUS'; | ||
k.run(k_expression,{"x":3,"y":4}); | ||
// RETURNS: 7 | ||
var k_fn = k.compile('{.name nom, <[.age, 18] GT .0, [.age, 12] GT "ado", "enfant"> age}'); | ||
k_expression = '{.name nom, <[.age, 18] GT .0, [.age, 12] GT "ado", "enfant"> age}'; | ||
k_fn({"age":23,"name":"Emily"}); | ||
// RETURNS: {"nom":"Emily","age":23} | ||
var k_fn = k.compile(k_expression); | ||
k_fn({"age":16,"name":"Katrina"}); | ||
// RETURNS: {"nom":"Katrina","age":"ado"} | ||
k_fn({"age":23,"name":"Emily"}); | ||
// RETURNS: {"nom":"Emily","age":23} | ||
k_fn({"age":2,"name":"Mark"}); | ||
// RETURNS: {"nom":"Mark","age":"enfant"} | ||
k_fn({"age":16,"name":"Katrina"}); | ||
// RETURNS: {"nom":"Katrina","age":"ado"} | ||
var k_fn = k.compile('$t = < i: int, t: [ t ] > ; <$t, $int>'); | ||
k_fn({"age":2,"name":"Mark"}) | ||
// RETURNS: {"nom":"Mark","age":"enfant"} | ||
k_fn(1); | ||
// RETURNS: 1 | ||
k_fn({"i":1}); | ||
// RETURNS: {"i":1} | ||
k_fn([{"i":2},{"i":3},{"t":[]}]); | ||
// RETURNS: undefined | ||
k_fn({"t":[{"i":2},{"i":3},{"t":[]}]}); | ||
// RETURNS: {"t":[{"i":2},{"i":3},{"t":[]}]} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81815
25
1467
246