@fraczak/k
Advanced tools
Comparing version 1.3.2 to 1.3.3
57
INTRO.md
# k - the way of building and manipulating JSON-like data | ||
Technically: notation for defining __first-order partial functions__. | ||
Technically, `k` is a notation for defining __first-order partial functions__. | ||
An example of a _partial function_ is "`.toto`", which maps an object | ||
to its property named `toto`, or it is not defined if the property | ||
doesn't exist. E.g., | ||
An example of a _partial function_ is the _projection_, e.g., "`.toto`", | ||
which maps an object to its property named `toto`, or it is not defined if | ||
the property doesn't exist. E.g., | ||
@@ -54,2 +54,10 @@ .toto : | ||
#### Syntactic sugar: | ||
- parenthesis can be omitted, except for the empty composition `()`, | ||
- dot (`.`) in "projection" acts as a separator so the space around it can be omitted. | ||
For example, `[(.toto .titi (.0 .1))]` can be written as `[.toto.titi.0.1]`. | ||
## Basic extensions | ||
@@ -81,15 +89,21 @@ | ||
- `GT` : | ||
- `GT` : -- identity for lists of decreasing elements; undefined otherwise | ||
[4,3] --> [4,3] | ||
[3,4] ... undefined | ||
[] --> [] | ||
[4,3,0] --> [4,3,0] | ||
[5,8] - undefined | ||
- `EQ` : | ||
- `EQ` : -- identity for lists of equal elements; undefined otherwise | ||
[4,4] --> [4,4] | ||
[4,5] ... undefined | ||
[4,4,4] --> [4,4,4] | ||
[5,8] - undefined | ||
[] --> [] | ||
- `[PLUS, TIMES, MINUS]` : | ||
- `{PLUS plus, TIMES times}` : | ||
[2,2,2] --> [6,8,-2] | ||
[1,2] --> {"plus":3,"times":2} | ||
[2,2,2] --> {"plus":6,"times":8} | ||
[] --> {"plus":0,"times":1} | ||
@@ -102,9 +116,10 @@ - `CONCAT` : | ||
{a: 12} --> "{\"a\":12}" | ||
{"a": 12} --> "{\"a\":12}" | ||
- `fromJSON` : | ||
"{\"a\":12}" --> {a: 12} | ||
"{\"a\":12}" --> {"a":12} | ||
- `DIV`, `FDIV`, `CONS`, `SNOC`, `_log!` | ||
- other predefined parial functions are: `DIV`, `FDIV`, `CONS`, `SNOC`, `toDateMsec`, | ||
`toDateStr`, and `_log!`. | ||
@@ -116,7 +131,7 @@ --- | ||
-- factorial.k | ||
dec = ([(),-1] PLUS); | ||
zero? = ([(),0] EQ 0); | ||
dec = [(),-1] PLUS; | ||
zero? = [(),0] EQ 0; | ||
factorial = < | ||
(zero? 1), | ||
([(dec factorial), ()] TIMES) | ||
zero? 1, | ||
[(dec factorial), ()] TIMES | ||
>; | ||
@@ -129,8 +144,8 @@ { () x, factorial "x!" } | ||
$nat = <nat s, {} z>; -- e.g., {{{{} z} s} s} | ||
$pair = {nat x, nat y}; -- e.g., {{{} z} x, {{{{} z} s} s} y} | ||
$nat = <nat 1, {} 0>; -- e.g., {{{{} 0} 1} 1} | ||
$pair = {nat x, nat y}; -- e.g., {{{} 0} x, {{{{} 0} 1} 1} y} | ||
s = ($nat {() s} $nat); | ||
add = ($pair <{(.x .s) x, (.y s) y} add, .y> $nat); | ||
suc = $nat {() 1} $nat; | ||
add = $pair <{.x.1 x, .y suc y} add, .y> $nat; | ||
{ | ||
"name": "@fraczak/k", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "k-language for JSON-like data transformation", | ||
@@ -24,4 +24,4 @@ "main": "index.js", | ||
"coffeescript": "^2.7.0", | ||
"jiwson": "^0.0.1" | ||
"jiwson": "^0.1.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
/* parser generated by jison 0.0.1 */ | ||
/* parser generated by jison 0.1.1 */ | ||
/* | ||
@@ -3,0 +3,0 @@ Returns a Parser object of the following structure: |
@@ -54,5 +54,5 @@ # k-language | ||
6. There is a number of built-in functions: `GT`, `EQ`, `PLUS`, | ||
`TIMES`, `MINUS`, `DIV`, `CONCAT`, `toJSON`, `fromJSON`, `CONS`, | ||
`SNOC`, `_log!`, ... For example: | ||
6. The other "built-in" functions: `GT`, `EQ`, `PLUS`, | ||
`TIMES`, `DIV`, `CONCAT`, `toJSON`, `fromJSON`, `CONS`, | ||
`SNOC`, `toDateMsec`, `toDateStr`, and `_log!`. For example: | ||
@@ -62,3 +62,3 @@ [1, 2, 3] PLUS --> integer constant function 6 | ||
[3, 2] GT --> vector constant function [3, 2] | ||
[3, 4] GT --> is not defined! | ||
[3, 4] GT ... is not defined! | ||
@@ -65,0 +65,0 @@ A more interesting example could be: |
13
run.js
@@ -52,15 +52,2 @@ // Generated by CoffeeScript 2.7.0 | ||
}, | ||
"MINUS": function(args) { | ||
if ('number' === typeof args) { | ||
return valid(-args); | ||
} | ||
return (function([res, ...args]) { | ||
if (args.length === 0) { | ||
return valid(-res); | ||
} | ||
return valid(args.reduce((function(res, x) { | ||
return res - x; | ||
}), res)); | ||
})(args); | ||
}, | ||
"DIV": function([x, y]) { | ||
@@ -67,0 +54,0 @@ var div, rem; |
// Generated by CoffeeScript 2.7.0 | ||
(function() { | ||
var add_code, add_rel, as_ref, codes, comp, identity, is_empty_rel, is_full_rel, is_identity_rel, rels, union; | ||
var add_code, add_rel, as_ref, codes, comp, comp_first, identity, is_constant_rel, is_empty_rel, is_full_rel, is_identity_rel, rels, union; | ||
@@ -22,2 +22,18 @@ rels = Object.create(null); | ||
is_constant_rel = function(rel) { | ||
switch (rel != null ? rel.op : void 0) { | ||
case "int": | ||
case "str": | ||
return true; | ||
case "vector": | ||
return Object.values(rel[rel.op]).every(is_constant_rel); | ||
case "product": | ||
return Object.values(rel[rel.op]).every(function({exp}) { | ||
return is_constant_rel(exp); | ||
}); | ||
default: | ||
return false; | ||
} | ||
}; | ||
is_empty_rel = function(rel) { | ||
@@ -28,2 +44,5 @@ return rel.op === "union" && rel.union.length === 0; | ||
is_full_rel = function(rel) { | ||
if (is_constant_rel(rel)) { | ||
return true; | ||
} | ||
switch (rel.op) { | ||
@@ -36,5 +55,8 @@ case "int": | ||
return rel.comp.every(is_full_rel); | ||
case "product": | ||
case "vector": | ||
return Object.values(rel[rel.op]).every(is_full_rel); | ||
case "product": | ||
return Object.values(rel[rel.op]).every(function({exp}) { | ||
return is_full_rel(exp); | ||
}); | ||
default: | ||
@@ -45,3 +67,3 @@ return false; | ||
comp = function(e1, e2) { | ||
comp_first = function(e1, e2) { | ||
if (is_identity_rel(e1)) { | ||
@@ -83,2 +105,17 @@ return e2; | ||
comp = function(e1, e2) { | ||
var result; | ||
result = comp_first(e1, e2); | ||
if (result.op !== "comp") { | ||
return result; | ||
} | ||
result.comp = result.comp.reduceRight(function(c, e) { | ||
if (is_constant_rel(c[0]) && is_full_rel(e)) { | ||
return c; | ||
} | ||
return [e, ...c]; | ||
}, []); | ||
return result; | ||
}; | ||
union = function(rels) { | ||
@@ -85,0 +122,0 @@ var list; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
96266
24
1682