lantiga / ki Goto Github PK
View Code? Open in Web Editor NEWlisp + mori, sweet.js
Home Page: ki-lang.org
License: MIT License
lisp + mori, sweet.js
Home Page: ki-lang.org
License: MIT License
ki require core
ki (:a {:a :b})
Results in
TypeError: keyword(...) is not a function
Example:
http://ki-lang.org/editor/editor.html#ki%20require%20core%0Aki%20(:a%20%7B:a%20:b%7D)
Any way to work on ki type code with REPL integration? To eval one block at a time similar to clojure REPL/Cider in Emacs?
We could consider relying on the AST for catching the most frequent macroexpand-time errors and emit proper errors with line numbers etc (as in source maps).
This is useful e.g. for a live ki demo on ki-lang.org
See here:
http://sweetjs.org/doc/main/sweet.html
https://github.com/mozilla/sweet.js/blob/3062bde9d3464adee868c98a2ced44d2316a6763/browser/editor.html
https://github.com/mozilla/sweet.js/blob/3062bde9d3464adee868c98a2ced44d2316a6763/browser/scripts/editor.js
I think that the instructions should add
npm install mori
otherwise, npm will resolve mori
as a dependency of ki
, but it won't be found by the require()
either that, or the example is changed to use
var mori = require('ki/node_modules/mori')
(or you make each ki statement generate a new require of mori, thus removing the burden of ki-users to deal with it, this shouldn't be a problem performance-wise)
That way you show up in https://npmjs.org/browse/keyword/sweet-macros which is linked to on the sweet.js github readme file.
The exit code of the ki
command should be 1
(or non-0
) when there are errors. This way one could use tools such as gulp-notify or nanybar to get notified about compilation status.
This may be a sweet.js issue but since I've only encoutered it with the ki compiler so far, I'd like to track it here if it's okay with you @lantiga.
Here's a simple test case: https://gist.github.com/fasterthanlime/4c348adeca79370bf58c
And here's the stack trace Chrome gives:
The file/line number is correct for problem4, problem3, problem2, but for problem and the top-level scope, they're completely wrong (863, 530).
It might not be a huge problem, it just means top-level code should always be wrapped in a function to get correct stack traces, but I was wondering if it's something the ki compiler do that throws off sweet.js source map generation.
Make reading keywords and object property notation more robust by relying on ranges (as in react.hiccup).
Ki's implementation of apply differs from Clojure. According to ClojureDocs apply
"applies fn f to the argument list formed by prepending intervening arguments to args". Only prepending. I'm interested in the second signature (apply f x args)
. Here is an example:
(def pick (js require("lodash").pick))
(defn pick1 [what key]
(pick what key))
(defn pickm [what keys]
(apply pick what keys))
(.log console (pick1 {$ "a" 1 "b" 2} "a")) // { a: 1 }
(.log console (pickm {$ "a" 1 "b" 2 "c" 3 } [$ "a" "b"])) // {}
(.log console (pick {$ "a" 1 "b" 2 "c" 3 } "a" "b")) // { a: 1, b: 2 }
The pick of lodash is same as underscore's and I don't think mori has an equivalent fn. If apply with three arguments worked like Clojure's apply, I believe the second console.log
would have matched the third. I'm not very skilled with lispy tricks, so I wrote my own pick like so:
(defn pick [from keys]
(let [res {$}]
(loop [key (.pop keys)]
(if (js key == undefined)
res
(do
(js res[key] = from[key])
(recur (.pop keys)))))))
It would be cool if ki functions / special forms named same as Clojure / ClojureScript worked the same.
I'm working on datomiki, a datomic rest client written in ki, and I just realized I need mori in order to use it - the compiled code can't run on its own. Or I figured it's better to depend on ki, so that a newer mori does not break things. Here is the error from [email protected]
:
/Users/om/Dev/apf/node_modules/datomiki/datomiki.js:115
return mori;
^
ReferenceError: mori is not defined
at _ki.modules.mori (/Users/om/Dev/apf/node_modules/datomiki/datomiki.js:115:28)
at Object.<anonymous> (/Users/om/Dev/apf/node_modules/datomiki/datomiki.js:118:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/om/Dev/apf/context/routes/api/sources/index.js:1:71)
at Module._compile (module.js:456:26)
Is this intentional? Maybe ki could --include
mori in the output? Perhaps with a new convenient option e.g. -a, --autonomous
?
Negative numeric literals are not parsed correctly.
E.g. [-1 -2] gets parsed as vector(-1,2), (def a -1) produces def(1).
Workaround for now is enclose negative numeric literals in a unary minus (-1) form.
Does ki handles promises?
I've tried to play around with Fetch API, fetch
function does return promise, but I can't figure out what is the proper syntax for .then().catch()
in ki.
From the docs, you can chain dot notation in the following way:
ki require core
var r1 = ki (threadf "lol r1" (.toUpperCase) (.trim));
console.log(r1); // "LOL R1"
However, sometimes you want to move this behavior into a function for reuse. Honestly I feel this should work, but it doesn't compile:
var r2 = ki (comp (.toUpperCase) (.trim));
// Unexpected token .
(Neither does (comp .toUpperCase .trim)
, but that fails with a runtime error)
If you want the same behavior composed, or partially applied, you have to do the following:
var toUpper = function(x){ return x.toUpperCase(); };
var toTrim = function(x){ return x.trim(); };
var r3 = ki (curry pipeline toUpper toTrim);
console.log(r3("lol r3")); // "LOL R3"
var r4 = ki (comp toUpper toTrim);
console.log(r4("lol r4")); // "LOL R4"
On a related note, threadf
also doesn't work with curry. The following compiles but results in a runtime error:
var r5 = ki (curry threadf toUpper toTrim);
console.log(r5("lol r5"));
// ReferenceError: threadf is not defined
It kind of makes sense if you think of threadf
as a special form, but it doesn't appear dot notation should be. Dot notation works as an argument to threadf
, but not in any other situations?
Explore alternatives to calling bind(this) in IIFN, e.g. in letv. Native bind has potential performance issues compared to using closures.
Sometimes it's useful to have the arguments
var available, just like js functions offer. Some cases can't quite be covered with variadic arity / multimethod functions. For example variable args with last arg a callback, or whatever other edge-case scenario. In any case, people could write their functions in ki without missing javascript's arguments
or falling back to javascript for that reason. Otherwise useful just for the sake of matching-up the host language.
With this input file:
var mori = require('mori');
var foo = ki (vector 1 2 3)
console.log(foo);
I obtain this:
var mori$610 = require('mori');
var foo$620 = mori$499.vector(1, 2, 3);
console.log(foo$620);
which is obviously incorrect
I never used sweet.js before, so I'm not sure why is it expanding the whole file as a macro, and not only the ki statement
I haven't looked into how exactly you're relying on mori but just a thought, if you're only after the immutability features then it might be worth considering ancient-oak?
https://github.com/brainshave/ancient-oak
Cheers
I was trying to write this:
ki require core
var f = ki (fn self ([] (self "World")) ([who] (str "Hello " who "!")))
console.log('calling ki', f())
compiler says the following:
[SyntaxError: [macro] Macro `_fnmap` could not be matched with `self , () , ()...`
219: ^]
I don't even know if this should be possible considering that this is not a Clojure clone, but in Clojure you could write:
user=> (def f (fn self ([] (self "World")) ([who] (str "Hello " who "!"))))
#'user/f
user=> (f)
"Hello World!"
user=> (f "Daniel")
"Hello Daniel!"
with javascript, is also posible:
> var x = function self(who) { if (!who) return self.call(this, "World!"); return "Hello " + who + "!"; }
undefined
> x()
'Hello World!!'
> x("Daniel")
'Hello Daniel!'
> x
[Function: self]
> self
ReferenceError: self is not defined
at repl:1:2
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
One of the things I'd love to work out how to do is load ki
as a library in Node, specifically to write a Webpack loader for easier development workflows. Any ideas where to start with that, as lib/ki_run.js
seems to have a lot of code involved
The following:
(defn welcome [name]
[:p "Welcome to " name])
Is valid Clojure/ClojureScript as far as I can tell, but it fails with:
[SyntaxError: [macro] Macro `_return_sexprs` could not be matched with `()...`
207:
^]
A workaround is to surround the vector literal with a (do )
.
Is it possible to make Ki work with CoffeeScript? Judging by this article https://coffeescript-cookbook.github.io/chapters/syntax/embedding_javascript it should be as easy as putting a pair of backticks around the generated JS (supposing that the first pass is for the Ki compiler and the next pass is for the CS compiler to get the resulting JS). Is this truly so? Can this somehow be implemented?
It gives me a SyntaxError: Unexpected token ;
r("mori.mutable.disj.fn",ge.K);;return this.mori;}.call({});});
^
Consider introducing destructuring.
Mori 0.3.0 has several improvements over previous versions and a few backwards incompatible changes (e.g. idiomatic function names). We should update ki and docs to Mori 0.3.0.
ki (+ 2 3)
returns 3
This may lead to subtle bugs. So the use of the symbol should be prohibited.
Like *
, +
should be treated as an unexpected token.
ki require core
return ki(+ 2 3);
returns 3.
See also:
http://ki-lang.org/editor/editor.html#ki%20require%20core%0A%0Areturn%20ki%28+%202%203%29;
See if we can get away with AST manipulation (it's now used for :keywords and js.object.property.access) by using custom pattern classes as recently introduced in sweet.js:
http://sweetjs.org/doc/main/sweet.html#custom-pattern-classes-experimental
The --watch
option does not work for ki macro
. And the --include
d code doesn't get watched at all. I ended up writing a little gulpfile.js, so I'd be able to play with macros. I imagine someone may write a gulp-ki plugin some day... Though this is just a few lines good-enough copy/paste fix that does the job.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.