Avi Drucker / Jun 24 2021 / Published
Remix of Python by Nextjournal
Three Language Comparison
First Steps
For fun, I'd like to create a code demo that compares code from three languages I know in the order I learned them: Python, JavaScript, and Clojure.
In each cell, I will attempt to:
1 print a string ✔
2 print a number ✔
3 ask the user for their name via some sort of input mechanism ✖
4 then print a greeting in response ✖
5 define a custom function ✔
6 declare a variable, constant, or otherwise saved value ✔
7 use that custom function ✔
8 what's next? I'd love to hear your ideas!
# print the string "Hello World"
print("Hello World")
# print the number 5
print(5)
# ask the user for their name
# name = input("What's your name? ") # error: currently unsupported: StdinNotImplementedError raw_input was called, but this frontend does not support input requests.
# print("Hi " + name + "!")
0.1s
Python
# define the function "make-full-name-map" which takes in a first, middle, and last name, and returns a hashmap collection ("dictionary" in Python)
def makeFullNameMap (first, middle, last):
return {"first": first, "middle": middle, "last": last}
# define the function "full-name-map-to-full-name" which takes in a hashmap ("dictionary" in Python) of first, middle, and last names and returns the name as string joined by spaces
def fullNameMapToFullName (nameMap):
return nameMap.get("first") + " " + nameMap['middle'] + " " + nameMap['last']
# " ".join([nameMap.get("first"), nameMap.get("middle"), nameMap.get("last")])
bjb = makeFullNameMap("Billy", "Joe", "Bob")
full = fullNameMapToFullName(bjb)
print(bjb)
print(full)
0.3s
Python
;; print the string "Hello World"
(println "Hello World")
;; print the number 5
(println 5)
;; note: this doesn't stop for the user to input their text
;;(do (print "What's your name? ")
;; (flush)
;; (read-line))
;; note: this also doesn't stop for the user to input their text
;; (println "Enter something> ")
;; (def x (read-line))
;; (println (str "You typed \"" x "\""))
0.9s
Clojure
;; define the function "make-full-name-map" which takes in a first, middle, and last name, and returns a hashmap collection
(defn makeFullNameMap [f,m,l]
{:first f :middle m :last l})
;; define the function "full-name-map-to-full-name" which takes in a hashmap of first, middle, and last names and returns the name as string joined by spaces
(defn fullNameMapToFullName [m]
(clojure.string/join " " [(:first m) (:middle m) (:last m)]))
(def bjb (makeFullNameMap "Brenda" "Jane" "Brant"))
(println bjb)
(println (fullNameMapToFullName bjb))
0.5s
Clojure
// source: https://stackoverflow.com/questions/19846078/how-to-read-from-chromes-console-in-javascript/19846113
if (console.everything === undefined)
{
console.everything = [];
console.defaultLog = console.log.bind(console);
console.log = function(){
console.everything.push({"type":"log", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultLog.apply(console, arguments);
}
console.defaultError = console.error.bind(console);
console.error = function(){
console.everything.push({"type":"error", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultError.apply(console, arguments);
}
console.defaultWarn = console.warn.bind(console);
console.warn = function(){
console.everything.push({"type":"warn", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultWarn.apply(console, arguments);
}
console.defaultDebug = console.debug.bind(console);
console.debug = function(){
console.everything.push({"type":"debug", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultDebug.apply(console, arguments);
}
}
Javascript
// note: It appears that this function cannot defined in another cell... More testing is required
// const unpackage = function(x) {
// return x.map(y => y.value[0])
// }
// note: I found a workaround to the above issue by attaching the function to the DOM window object: https://stackoverflow.com/questions/10324501/attach-function-to-domwindow-object
if (!window.unpackage) {
window.unpackage = function(x) {
return x.map(y => y.value[0])
};
};
Javascript
// question: How may I explicitly print from a JavaScript cell?
// answer: See the two hidden cells above, also related research:
// https://stackoverflow.com/questions/13013535/google-chrome-extension-is-it-possible-to-get-console-output-js-errors-conso
console.log("Hello World")
console.log(5)
// console.everything // UNCOMMENT to see full log
unpackage(console.everything)
Javascript
// This cell can be used to clear the JavaScript console log array
// console.everything.length = 0; // UNCOMMENT to reset
// console.everything.length // UNCOMMENT to verify # of JavaScript logs made
Javascript
// define the function "make-full-name-map" which takes in a first, middle, and last name, and returns a hashmap collection (object in JavaScript)
function makeFullNameMap(f,m,l) {
return {first: f, middle: m, last: l};
}
// define the function "full-name-map-to-full-name" which takes in a hashmap (object in JavaScript) of first, middle, and last names and returns the name as string joined by spaces
function fullNameMapToFullName (o) {
return o.first + " " + o.middle + " " + o.last;
}
console.everything.length = 0; // clear "console log"
const bjb = makeFullNameMap("Boba", "Jett", "Bright");
console.log(bjb);
console.log(fullNameMapToFullName (bjb));
unpackage(console.everything)
Javascript
In order to get the JavaScript items printed below their respective cells, I leveraged a workaround which allows side effects of console.log to be saved to a resulting object, and then unpackaged said object to show its array contents.