Fork me on GitHub
#beginners
<
2021-11-09
>
JohnJ00:11:24

@hiredman yep, that seems to be the most overhead (returns of Object)., the price of dynamism

hiredman00:11:21

if you haven't done as @alexmiller suggested yet, you can turn on boxed math warnings and go through an eliminate it, likely to the same degree java would be able to (numbers in an arraylist will box there as well)

JohnJ00:11:39

eliminate it with type hints?

JohnJ00:11:39

coercing the subtraction operands with long eliminated the warnings and seemed to help a bit

ns01:11:03

What would be the best way to handle url parameters, for example /widgets?color=blue&sort=newest on frontend/ClojureSript? I'm writing my app with re-frame and using "bidi" + "pushy" which allow me to handle routes just fine but I don't see a way to parse url params nor assign them back in url? What is your prefered way of doing so?

Ngoc Khuat02:11:29

I use this lib : https://github.com/lambdaisland/uri . curious what others ppl use

Benjamin09:11:49

(and (f1 data)
     (f2 data)
     (f3 data))
what is a nice way to compose predicates. It's a bit like I'd like to say apply and ..

Benjamin09:11:18

maybe

((every?
  true?
  (apply juxt fns)) d)

Benjamin09:11:01

ah sounds good

Lennart Buit09:11:28

your own suggestion has different short circuit behaviour: If f3 was like #(fire-missles!) but f1 was (constantly false), with and and every-pred? no missles would be fired, but with your juxt I think they would.

👍 1
Lennart Buit09:11:15

(why is it always fire-missles!, who would write a function like that lol)

Felipe12:11:29

fire-missiles is good because it can’t be safely retried 😁

sova-soars-the-sora18:11:57

...but i am le tired

Sebastian11:11:30

I am currently documenting the tests i have in my project. Is there a way i can generate an API documentation of all my tests? I've tried https://github.com/weavejester/codox, but it seems to only generate for everything in src.

Benjamin12:11:31

what is a simple way to implement a timeout (the only thing I want to do is throttle some behaviour)? Storing a date in an atom?

Shawn12:11:30

Simplest way is probably to create a timeout channel and block the thread on it https://clojuredocs.org/clojure.core.async/timeout

Benjamin13:11:31

is there a way to instantly check if a channel has items? aka <!! returns

bnstvn13:11:35

with future:

(deref 
  (future (Thread/sleep 0) :ok)
  1000
  :timed-out)
=> :ok
(deref 
  (future (Thread/sleep 1500) :ok)
  1000
  :timed-out)
=> :timed-out

Stuart14:11:09

Why cant I do this?

(map (comp Integer/parseInt str/trim) ["1 " " 2" "3" " 4 "])
? It doesn't like Integer/parseInt, but this works
(map (comp #(Integer/parseInt %) str/trim) ["1 " " 2" "3" " 4 "])
??

Benjamin14:11:03

interop calls are different from normal functions

Stuart14:11:49

oh. Is their a "normal" function for parsing ints from strings?

Benjamin14:11:10

I think your second code is fine

alexmiller14:11:12

Not yet, but it's coming in 1.11!

clojure-spin 7
3
😍 4
alexmiller14:11:31

One of the things I'm working through right now…

dabrazhe14:11:14

It will be really cool. Could you consider built-in trimming and parsing doubles as well : ) ?

alexmiller14:11:54

doubles will be covered. trimming is a clojure.string/trim away

JohnJ16:11:17

I'm bechmarking a function, what could cause isolated runs (with either criterium or time with dotimes) to report a 30ms mean in some runs and 90ms in others?

alexmiller16:11:32

garbage collection

alexmiller16:11:45

or lots of other things that you haven't told us enough to guess at :)

JohnJ16:11:27

this is all the code

(ns unrolled-benchmark-2
  (:require [criterium.core :as cc]))

(def times-v (into [] (take 1e6) (iterate #(+ % (rand-int 1000)) 0)))

;(set! *unchecked-math* true)
(defn unrolled [v]
  (loop [idx 0 agg ()]
    (if (< idx (- (count v) 7))
      (recur (inc idx)
             (if (> 1000 (- (long (nth v (+ idx 7)))
                            (long (nth v idx))))
               (cons idx agg)
               agg))
      agg)))
;(set! *unchecked-math* false)

;(let [v times-v]
;  (dotimes [_ 30] (time (unrolled v))))

(let [v times-v]
  (cc/quick-bench (unrolled v)))

alexmiller16:11:34

I find quick-bench is often unreliable for "fast" operations

alexmiller16:11:10

not long enough for jit effects to sort themselves out, same for shorter time operations

JohnJ16:11:17

(dotimes... report the same

alexmiller16:11:19

I often use something like (dotimes [_ 20] (time (dotimes [_ 10000] (op ...))))

alexmiller16:11:10

with whatever number for 10000 make something take something in the seconds

JohnJ16:11:20

so the JIT not kicking in some runs? let me try that

alexmiller16:11:22

JIT can take 10000 evals before you get to the final stage of compilation

alexmiller16:11:45

and Java clock is maybe only microsecond level resolution (depends on your OS and java version)

alexmiller16:11:16

quick-bench is ok on chunkier tests but not enough runs for fast tests

JohnJ17:11:11

Ok, the weird part is sometimes optimizations kick in and somteimes not, with your method optimizations kicked in the first result, it's taking its time 😉

JohnJ17:11:57

it's a linux vm with a dedicated CPU (supposedly), no idea if that affects the Java clock

JohnJ17:11:58

same results, if it takes 30ms each call would take 30ms for the whole run, same if each call takes ~90ms. Would try again later with real hardware, thanks.

jumar04:11:17

Virtualization and other tenants may well affect this too

Dmitrii Pisarenko16:11:33

Hello! I am reading the fascinating book "On LISP" by Paul Graham (available for download at http://www.paulgraham.com/onlisp.html ). One of the key ideas is that with LISP you can build a language for your domain, and then build you application using that language. All examples in the book are in Common LISP. Is there a book like this with a "translation" from Common LISP to Clojure?

Bob B16:11:38

I'm not aware of a whole book that's 'translated' to Clojure, but Stuart Halloway did a few blog articles where he translates some specific bits from OL: <https://cognitect.com/blog/2008/12/12/on-lisp-clojure>

adityaathalye19:11:24

> you can build a language for your domain, and then build you application using that language @U02A6THCMBK The book Beautiful Racket is all about such "language-oriented programming". The book's author explains it this way: https://beautifulracket.com/appendix/why-lop-why-racket.html > ... language-oriented programming has risen to become my favorite part of Racket. Along the way, I converted my enthusiasm into action. In addition to making languages, I wrote this online book—https://beautifulracket.com/—that teaches LOP as a technique, and Racket as a tool.

hiredman17:11:47

the reframe thing wraps cljs-ajax which says it doesn't support files https://github.com/JulianBirch/cljs-ajax/blob/830b4de47908536ab66c233931d2ca4a4256f576/README.md#formdata-support not sure how that manifests, etc

hiredman17:11:06

although this https://github.com/JulianBirch/cljs-ajax/issues/122 shows someone using it to upload files

zetashift18:11:04

Hey y'all, I'm a Clojure(and Lisp) newbie, and I'm trying out Clojure using https://github.com/PEZ/rich4clojure trying to solve: problem_001.clj and I'm getting the following error trying to evaluate the following form:

(tests
  solution := true)
; Syntax error compiling at (src/rich4clojure/elementary/problem_001.clj:25:1).
; Unable to resolve symbol: tests in this context
I'm using the Calva Jack-In option with the Rich 4Clojure project type, anyone have an idea what might be going wrong?

pez18:11:37

With Calva, always start with loading the file. Evaluating the ns form should also work, but “load file” is a bit more complete, I think.

zetashift18:11:53

How do I "load file"? Do load-file in the repl?

zetashift18:11:10

Ah the INSTRUCTIONS.md also mention it and I've seen it mentioned a few times too, but I'm not sure what command runs the load-file or reloads a file

zetashift18:11:11

Ah Load current file and depencies !

👍 1
zetashift20:11:16

I see a nil returned too, I'm not sure what that's from tho?

pez20:11:34

It’s the return value of (tests …)

zetashift21:11:20

should that be nil or also a green checkmark?

pez21:11:30

I’m not sure what you mean?

pez21:11:53

Thanks for the feedback about load file. I’ve updated INSTRUCTIONS.md now. Will have to think about where else this should be made clearer.

zetashift16:11:40

> I’m not sure what you mean? So loading the file or evaluating the form gives me a green checkmark and a nil, but from the INSTRUCTIONS, there is no nil, so I don't know if seeing a nil returned is a good 😛:

zetashift17:11:10

Also thank you for making an awesome getting started experience, if it wasn't for the lowest entry to trying out clojure ever I probably wouldn't be playing around with it!

pez17:11:35

You are welcome!

pez17:11:04

I'll add the nil to the instructions. 🙏

❤️ 1
hiredman18:11:35

did you evaluate the ns form at the top of the file?

zetashift18:11:01

I did not 😛

Ryan18:11:11

I am trying to write a fn to do some introspection to build a map using metadata, and I can't seem to get the symbol passed in correctly :

(defn test1 [v] (ns-publics 'v))
yields no results for :
(test1 'namespace.with.public.fns) yields {}
but in repl
(ns-publics 'namespace.with.public.fns) yields {fn1 #'namespace.with.public.fns, fn2 #'namespace.with.public.fns}
Any clues what I'm doing wrong?

Ryan18:11:46

I've tried (quote v) and a few other bits with no avail

seancorfield18:11:03

When you say (ns-publics 'v) you are passing a literal symbol v so you are asking for public vars in a namespace called v.

seancorfield18:11:34

You want v evaluated there so whatever symbol you actually pass in to test1 is used.

seancorfield18:11:14

dev=> (defn test1 [v] (ns-publics v))
#'dev/test1
dev=> (test1 'honey.sql)
{format-entity #'honey.sql/format-entity, format-expr #'honey.sql/format-expr, ...}

seancorfield18:11:51

And at this point, you don't need test1 since it is simply calling another function.

Ryan18:11:13

I know, I reduced my actual example to the part I was having trouble with 🙂

Ryan18:11:15

thanks so much

Ryan18:11:09

hmm, my cljs repl is giving me grief about this:

(defn test1 [v] (ns-publics v))
Encountered error when macroexpanding cljs.core/ns-publics.
AssertionError: Assert failed: Argument to ns-publics must be a quoted symbol

seancorfield18:11:10

Ah, I didn't realize you're using ClojureScript. I've no idea about that. Namespaces behave differently in cljs.

seancorfield18:11:54

(even with cljs, macros essentially run as Clojure during the compiler -- I've no idea how cljs REPLs handle macros)

👍 1
Ryan19:11:53

Thanks, I should have been more specific 🙂 I've tried asking in #clojurescript as well, and am making some progress with help there!

1