tkolleh
tkolleh

Reputation: 423

Clojurescript using nodejs to read stdin

I am unable to read stdin from NodeJs using ClojureScript. Ideally the final commands would be java -cp cljs.jar:src clojure.main node.clj followed by node main.js. The Javascript version works fine but i'm unable to get it to work using ClojureScript.

Clojurescript

(ns hello-world.core
  (:require [clojure.string :as string] [cljs.nodejs :as nodejs]))

(nodejs/enable-util-print!)

(defn -main [& args]
  (let [readline (js/require "readline")
    rl (.createInterface readline js/process.stdin js/process.stdout)]
    (.on rl "line" 
     (fn [line]
          (def lineVector (clojure.string/split line #" "))
          (if  (and (nil? lineVector)
                    (< (get lineVector 0) 10) 
                    (< (get lineVector 1) 10))
            (+ (get lineVector 0) (get lineVector 0)))))))

(set! *main-cli-fn* -main)

Javascript

var readline = require('readline');

process.stdin.setEncoding('utf8');
var rl = readline.createInterface({
  input: process.stdin,
  terminal: false
});  

rl.on('line', readLine);

function readLine (line) {...}

I get the following error message:

TypeError: a.ac is not a function at Function.be.w (/Users/tinatuh/Workspace/AlgorithmicToolbox-Coursera/main.js:212:47) at be (/Users/tinatuh/Workspace/AlgorithmicToolbox-Coursera/main.js:211:409) at Gc (/Users/tinatuh/Workspace/AlgorithmicToolbox-Coursera/main.js:99:59) at Jc (/Users/tinatuh/Workspace/AlgorithmicToolbox-Coursera/main.js:108:60) at Object. (/Users/tinatuh/Workspace/AlgorithmicToolbox-Coursera/main.js:212:506) at Module._compile (module.js:641:30) at Object.Module._extensions..js (module.js:652:10) at Module.load (module.js:560:32) at tryModuleLoad (module.js:503:12) at Function.Module._load (module.js:495:3)

Upvotes: 1

Views: 407

Answers (1)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

It looks you are using advanced optimisations in ClojureScript/Closure compilation which aggressively renames functions and variables to make the output JavaScript code smaller.

You might try to use simple (:simple) optimisations instead or provide externs definitions that will help Closure compiler to correctly process your and libraries code (referenced web page includes a link to a sample project using externs).

There is also another issue in your code in how you call readline.createInterface: it expects the options object and you are passing it bare options values instead. You need to call it like below:

(.createInterface readline
                  #js {:input js/process.stdin
                       :output js/process.stdout})

Upvotes: 2

Related Questions