Fork me on GitHub
#clojurescript
<
2020-08-18
>
Daniel Craig00:08:10

This is rad, I want to try it out @sritchie09

Sam Ritchie01:08:03

I’m planning on putting up a version of Maria.cloud with this preloaded, and porting this Quil layer I built over so it can animate too in the browser. I’ll drop a link when that’s set!

Sam Ritchie01:08:38

Let me know if you’re interested in the textbook exercises- those are all possible immediately, and I’ve put a good amount of work into making those accessible too

Daniel Craig02:08:36

Yeah I’m definitely interested in those!

Daniel Craig02:08:48

My dad is a physics professor and he’s been wanting me to make replacements for his Java applets and flash simulations

Sam Ritchie02:08:06

Amazing! I’m trying to gather a little crew together here so I’d be happy to send you a note when I get the ability to animate

Sam Ritchie02:08:28

This repo has a system I built to do the exercises in the original scheme, using the SICM textbook

Daniel Craig02:08:55

There are a lot of educational physics simulations that are no longer usable so I’m interested in this

Sam Ritchie02:08:31

Yes, the goal here is to generate interactive in browser simulations, that you can fiddle with and easily make by writing down a lagrangian

Sam Ritchie02:08:47

Here’s a first attempt at using that library to generate animations with Quil

Sam Ritchie02:08:13

It worked really well! But I wanted to be able to embed it in the browser and let folks fiddle with the code... which led me to the big CLJS conversion

Sam Ritchie02:08:43

If you send me your email I can let you know when I have something up that’s in a good shape for others to start banging on and writing simulations with

Sam Ritchie02:08:51

Also, I’d be really interested to see your wish list

Sam Ritchie02:08:21

Of what simulations and models should exist. My bigger goal here is to make it possible to write “executable textbooks”

Daniel Craig02:08:33

Sure! I’ll dm my email

Daniel Craig02:08:12

I’ll try get a list of wish list items for you in the next few days

👍 3
bones02:08:16

Is there a way I can grab items from a list, one at time - like generators/yield in js?

noisesmith03:08:11

the common way to do that is wrap the code in a context that traverses (like map or reduce), but you can also do this with core.async or interop

noisesmith03:08:15

though I don't know off hand what the raw interop looks like without the js syntax

bones03:08:34

That’s cool, thanks :thumbsup:

noisesmith03:08:44

if this isn't out of date, it looks like generators are intentionally not available in cljs https://observablehq.com/@shaunlebron/proposal-generators-and-async-functions-in-clojurescript so you'd need to write at least a stub of js

noisesmith03:08:55

if I'm reading it correctly

noisesmith03:08:04

quoting (likely from this very same channel)

await/async is just sugar over functions that return Promise. You can always use the Promise API on the result of async function. If you need to provide async function in your cljs public API, just return Promise. Generators are sugar for generating stateful Iterators and way to add some kind of laziness to JS. Cljs structures are lazy and implement Iterator for long time. Cljs is ahead in this. 

bones03:08:13

Hrm I see Iterator, that might be the way

noisesmith03:08:11

iterator, or even just a lazy seq, sure

bones05:08:56

I think iterator might be the way to go. It specifically has next - giving me the next value, as opposed to lazy seq which gives me the entire seq up to a point?

noisesmith05:08:23

a lazy-seq is more idiomatic, but requires a rearrangement of the code, so that instead of a stateful object providing new values, a function inside a comprehension consumes those values

bones11:08:05

I think I understand. It's all about changing mindset from that for-loop mentality, to data isn't it

💡 3
noisesmith12:08:08

yes, precisely

noisesmith05:08:14

but for every solution that stores a state and reuses it, there's another that reifies that state into position on a lazy-seq and runs inside a map or reduce

noisesmith05:08:43

and I think it's provable that (given some sort of async driver) both can do what the other does

noisesmith05:08:14

iterator might be most direct for your usage, but code that's reshaped to use laziness instead tends to be better clojure code

timrichardt08:08:11

When doing JS interop, is there an idiomatic way to propagate down nested obejcts without null-checking? In ES, there is this ? operator. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

thheller08:08:06

@timrichardt you mean with null-checking? (some-> obj .-foo .-bar) would work

👍 12
Akash08:08:12

Can someone please suggest a simple data visualisation library in Clojurescript. The use case is to plot simple graphs like simple line plots/bar chart with smoothing option.

polymeris17:08:51

(js but easily usable from e.g. reagent)

Mackram12:08:44

can someone point me in the direction of an auto documentation tool or library that has support for clojure.spec

Mackram12:08:06

and obviously supports clojure and clojurescript

👀 3
Mackram12:08:47

something ala codox (but with support for clojure.spec)

dnolen13:08:57

while this isn't exactly what you're talking about, you can generators work with core.async same as Promises

mokr16:08:06

Just to have asked: Is it possible to get the contents of an edn-file into clojurescript? The file contains some generated lookup data that I need in my frontend code. Currently I copy the contents into a var in a cljs-file, but it would be nice to instead just consume the edn-file directly (e.g. using (:require [“lookup.edn” :as lookup])) as that would pick up any updates to it. Is this possible without e.g. an AJAX request or having the backend inject it into the HTML page, … Thinking about it I might as well just generate a cljs instead of edn, but I would still be interested in an EDN solution.

phronmophobic16:08:51

you could create a macro that does this at compile time which be similar to your require example:

(defedn lookup "lookup.edn")
in clj file:
(defmacro defedn [name fname]
  `(def ~name (quote ~(read-string (slurp fname)))))

mokr16:08:59

Thanks, @U7RJTCH6J, this is the kind of solution I was looking for.

👍 3
Sam Ritchie20:08:16

Hey all - does anyone have tips on how to get a list of all vars declared in a namespace, from cljs?

dpsutton20:08:58

(keys (ns-publics 'clojure.set))

Sam Ritchie20:08:00

I'm looking to write a function or macro that can do the equivalent of the evil :refer :all, just for a repl session

Alex Miller (Clojure team)20:08:24

or is that not a thing in cljs

Sam Ritchie21:08:17

super strange... I 'm seeing this error:

Sam Ritchie21:08:19

Invalid :refer, macro sicmutils.env/literal-function does not exist in file
   <cljs repl>

Sam Ritchie21:08:37

the macros seem to be getting treated as functions

Sam Ritchie21:08:51

(ns sicmutils.examples.pendulum
  (:refer-clojure :exclude [+ - * / zero? partial ref])
  (:require [sicmutils.env :refer-macros [literal-function]]))

Sam Ritchie21:08:08

(sending code link)

Sam Ritchie21:08:36

all of these macros seem affected

Sam Ritchie21:08:22

oh, I might have an idea... I may have left a .clj file around, doing the importing

Sam Ritchie21:08:37

nope, not that. has anyone seen this, where macros simply aren't seen?

lilactown21:08:11

I don’t have enough information to diagnose the error

Alex Miller (Clojure team)21:08:24

well if you're in cljs, there are no macros right?

Sam Ritchie21:08:40

sorry, my bad, let me summarize - I've got sicmutils.env, which declares three macros

Sam Ritchie21:08:03

(and I've done this in a few spots around the library)

Sam Ritchie21:08:14

@alexmiller it's a cljc file, so we should be good here

Sam Ritchie21:08:41

here's the env file with macros:

Sam Ritchie21:08:19

then, when I try to evaluate this new namespace form that pulls in that namespace and refers macros: https://github.com/littleredcomputer/sicmutils/blob/sritchie/convert_env/src/sicmutils/examples/pendulum.cljc#L23

Sam Ritchie21:08:05

I see

Unexpected error (ExceptionInfo) compiling at (REPL:1).
Invalid :refer, macro sicmutils.env/with-literal-functions does not exist in file <cljs repl>

Sam Ritchie21:08:16

(ns sicmutils.examples.pendulum
  (:refer-clojure :exclude [+ - * / zero? partial ref])
  (:require [sicmutils.env :as e :include-macros true])
  (:require [sicmutils.function :as f :include-macros true]))

Sam Ritchie21:08:53

(f/with-literal-functions [x y z]) works great at the repl, referencing this macro

Sam Ritchie21:08:04

(e/with-literal-functions [x y z]) fails

Sam Ritchie21:08:21

meaning, it doesn't act as a macro, so I see

WARNING: Use of undeclared Var sicmutils.examples.pendulum/x at line 1 <cljs repl>
WARNING: Use of undeclared Var sicmutils.examples.pendulum/y at line 1 <cljs repl>
WARNING: Use of undeclared Var sicmutils.examples.pendulum/z at line 1 <cljs repl>

Sam Ritchie21:08:47

I'll try to slim the ns way down and get a small repro

Sam Ritchie21:08:57

but thought someone might be able to pattern match on that strange error /behavior

lilactown21:08:27

try adding to sicmutils.env ’s ns form:

(:require-macros [sicmutils.env])

lilactown21:08:42

ah but you have :include-macros in the consuming ns

lilactown21:08:00

are there any errors at macro time? e.g. a parse or compiler error in your Clojure REPL

Sam Ritchie21:08:35

let me try adding that line

Sam Ritchie21:08:48

no errors that I can see

Sam Ritchie21:08:01

@lilactown the macros seem to be acting as functions

Sam Ritchie21:08:33

that return their form?

phronmophobic21:08:41

this looks like the output you get when using bootstrapped clojurescript

Sam Ritchie21:08:12

this is cider, with the node repl

Sam Ritchie21:08:54

okay! we may have a winner

Sam Ritchie21:08:02

wrapping the macro definitions in a reader conditional worked, here

🎉 3
Sam Ritchie21:08:52

boooom thanks all!