This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-01-28
Channels
- # babashka (28)
- # beginners (252)
- # bristol-clojurians (2)
- # calva (28)
- # cider (11)
- # clj-kondo (15)
- # cljs-dev (7)
- # clojure (378)
- # clojure-europe (4)
- # clojure-italy (4)
- # clojure-nl (3)
- # clojure-norway (4)
- # clojure-uk (32)
- # clojurescript (128)
- # cursive (39)
- # data-science (18)
- # docker (37)
- # figwheel-main (10)
- # fulcro (45)
- # ghostwheel (7)
- # graalvm (2)
- # hugsql (1)
- # jobs (2)
- # joker (5)
- # kaocha (5)
- # luminus (12)
- # off-topic (37)
- # onyx (4)
- # pathom (22)
- # pedestal (70)
- # re-frame (7)
- # reagent (30)
- # ring (4)
- # shadow-cljs (12)
- # spacemacs (1)
- # sql (26)
- # tools-deps (7)
- # vrac (2)
- # vscode (7)
- # xtdb (27)
I'm having some issues to use Sci with Nodejs in an asynchronous context. It seems (fn [] ..)
doesn't return a javascript function that I can use outside of the evalString context. E.g. the following fails
const { evalString } = require('@borkdude/sci');
var myFN = evalString("(fn [a] (+ a 10))")
console.log("v", myFN(4))
This gives
console.log("v", myFN(4))
^
TypeError: myFN is not a function
Not sure if there is a better option, but a "solution" I found is by having a nested evaluate:
'use strict';
const { evalString } = require('@borkdude/sci');
var evaluate = function(code, args) {
evalString(code, {
bindings: {
args: args,
println: function(...args) { console.log(args.join(' ')) },
request: require('request')
evalLater: function(s) { return function(...args) {
evaluate(s, args) } } },
namespaces: {
https: require('https'),
http: require('http')
}}) };
exports.handler = function(event, context, callback) {
evaluate({{clojure-code}},
[event,
context,
function(response) { callback(null, response) }]);
};
In the CLJ sci this does work:
(let [f (sci/eval-string "(fn [a b] (+ a b))")]
(f 1 2))
I'll create a Github issue
const { evalString, toJS } = require('@borkdude/sci');
and then apply the function toJS
to the function.Multimethods landed on bb master:
bb "
(derive ::rect ::shape)
(defmulti bar (fn [x y] [x y]))
(defmethod bar [::rect ::shape] [x y] :rect-shape)
(defmethod bar [::shape ::rect] [x y] :shape-rect)
(prefer-method bar [::rect ::shape] [::shape ::rect])
(bar ::rect ::rect)"
:rect-shape
@jeroenvandijk The above approach works with toJS
but I've now also pushed 0.0.13-alpha.2
to npm which don't add metadata to evaluated functions.
I published a new version of sci but somehow the js doesn't end up there...
$ ls node_modules/@borkdude/sci/
CHANGES.md LICENSE README.md package.json
although it does mention on the npm website that the package is 480 kB
and didn't do anything else than previous times... I already tried it 4 times.. (now it's alpha.4)
npm seems to be back to normal.. @jeroenvandijk :
> const { evalString, toJS } = require('@borkdude/sci');
undefined
> f = evalString("(fn [] \"hello\")")
[Function: d] { i: 0, l: [Function (anonymous)], f: [Function: g] }
> f()
'hello'
@U04V15CAJ First test seems to work with npm i @borkdude/[email protected]
, but not with the latest npm i @borkdude/sci
Because the package doesn't have the sci.js file
latest is 0.0.13-alpha.5 according to npm
Yeah, I've had this problem several times before... that's why it got all the way up to alpha 5... 😞
Ok annoying 😞
I'll test with 4.2 I assume it is the same package than?
If you go to the npm page you do see that the package is around 480kb which indicates that the source should be in there somehow
This is my release script:
#!/usr/bin/env bash
set -Eeo pipefail
rm -rf /tmp/release-js
mkdir -p /tmp/release-js
cp out/min/sci.min.js /tmp/release-js/sci.js
cp README.md /tmp/release-js
cp package.json /tmp/release-js
cd /tmp/release-js
npm publish --access public
I do the same every time. It's a mystery to me why the sci.js sometimes is not there
I've now released alpha.6:
$ script/release-js
npm notice
npm notice :package: @borkdude/[email protected]
npm notice === Tarball Contents ===
npm notice 468.3kB sci.js
npm notice 686B package.json
npm notice 10.7kB README.md
npm notice === Tarball Details ===
npm notice name: @borkdude/sci
npm notice version: 0.0.13-alpha.6
npm notice package size: 106.9 kB
npm notice unpacked size: 479.7 kB
npm notice shasum: 427cca209aaa65b726053c7cc175e8ff36ec2df7
npm notice integrity: sha512-kwqOlJl4znqZW[...]D4Anf92xallVw==
npm notice total files: 3
npm notice
+ @borkdude/[email protected]
here too
I'm a bit confused by how npm install ... works, but with the explicity call npm install @borkdude/[email protected]
it works
Thanks!