This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-07
Channels
- # beginners (71)
- # boot (25)
- # cider (204)
- # clara (18)
- # cljs-dev (10)
- # cljsrn (64)
- # clojure (50)
- # clojure-dev (2)
- # clojure-dusseldorf (1)
- # clojure-india (5)
- # clojure-italy (1)
- # clojure-nl (21)
- # clojure-poland (65)
- # clojure-spec (41)
- # clojure-uk (10)
- # clojurescript (4)
- # core-async (5)
- # cursive (2)
- # datomic (17)
- # duct (8)
- # emacs (8)
- # fulcro (1)
- # graphql (21)
- # hoplon (2)
- # javascript (3)
- # lumo (25)
- # mount (5)
- # off-topic (5)
- # onyx (3)
- # portkey (6)
- # re-frame (15)
- # reagent (5)
- # rum (1)
- # shadow-cljs (198)
- # spacemacs (19)
- # specter (2)
I’ve found it’s hard to run things through the repl with a let
binding that references previous bindings, because if I try to execute some expression that has that reference, it isn’t defined in that context. Is there some better way to handle this situation?
Specifically I’m writing a database seeding mechanism like
(let [{foo-id :id} (create-foo! { :value 10})
{another-id :id} (create-foobar! { :foo-id foo-id})])
So when I eval create-foobar!
, there is no foo-id
and I can’t use the repl effectively.
(let [a 1 b 2] (+ a b)) 3
works for me. are you sure that (create-foo! {:value 10}
is returning a map with foo-id
on it?
Another example that is close to yours @bolivier is
(let [{foo-id :id} {:id 123}
{another-id :id} {:id (dec foo-id)}]
another-id)
which evaluates to 122
. As @dpsutton indicates, the let bindings work. We are wondering what is breaking for you.But when I try to eval a subsection of a let binding, it fails if there is some variable that is referenced from a previous binding.
right, eval doesn't see locals
no, that's just not how evaluation works
there are debugging tools that show locals, but it can't be done partially like that directly
Is there a better technique for figuring out the oddities of code in that situation?
This should get you started. It shows you a technique such that you can do exactly what you want
if you're using CIDER there is a step through debugger. I'm pretty sure cursive has one as well if you want to do that
but if you have small composable functions chances are you can just inspect them in the repl
I’m using CIDER (since I’m already familiar with emacs). I’ll read up on the documentation on it as well.
super straight forward. https://cider.readthedocs.io/en/latest/debugging/
CIDER related i mean. general questions about the lang and you'll find many people here willing to help and chat
Hi everyone, so I have this functions in namespace A:
(defn get-fn
[config request]
(let [{:keys [service]} config
{:keys [uri params headers]} request]
(do (log/info request)
(-> (client/get (str service uri)
{:headers headers
:query-params params})
(select-keys [:headers :body])))))
(defn choose-method-function
[method]
(eval (symbol (str (clojure.string/lower-case method) "-fn"))))
If I were to call:
(choose-method-function "GET")
in namespace A, then I will get the get-fn
functionBut if I call that in another namespace, let's say B, then it would result an error saying that my namespace B is unable to resolve get-fn
Now that is understandable, but it got me thinking on why it does that? I mean, does choose-method-function
context changed based on the location it was called rather than where it's defined?
If so, how can I create a function in another namespace to return a function in that namespace, but it should work as well when it's called from another namespace
@hawari.rahman17 - two things about that - eval is expensive, and more power than you need, you can use symbol and resolve instead. Also multimethods are designed to do this kind of dispatch directly, you don't need to hack up this kind of thing by hand
You're right, I forgot about multimethod. Seems like it should be the way to go for me @noisesmith.
Hello, I’m trying out Clojure with quil, but this doesn’t seem to work as expected
(defn draw-state [state]
(map #(apply println %) (produce-tiles (:tile-count state))))
Produce tiles returns something like this:
([0 1 2] [0 1 3] [0 1 4])
Probably I’m doing something really stupid
In the end I want something like this:
(defn draw-state [state]
(map #(apply draw-line %) (produce-tiles (:tile-count state))))
(defn draw-line [pos-x pos-y tile-count]
(q/stroke-weight (/ (q/mouse-x) 20))
(q/line pos-x pos-y (/ (+ pos-x (q/width)) tile-count) (/ (+ pos-x (q/height)) tile-count)))
Map to call draw-line with each one of the childs of the vector produced produce-tiles
(defn produce-tiles [tile-count]
(for [y (range tile-count)
x (range tile-count)] [(calc-pos-x x tile-count) (calc-pos-y y tile-count) tile-count]))
using map (lazy) to drive something you very much don't want to be lazy (drawing stuff), is likely to be a bad idea
common ways to process sequences when you don't care about the results are doseq or run!
perfect! I totally didn’t know that map is lazy, I need to take a look at those concepts from Clojure 🙂
not sure this is the right channel or not, but please tell me to GTFO if necessary 🙂 I'm creating a process scheduler (with cljs) like an operating system would have (cooperative multitasking, no interrupts/setTimeout, multi-level queues) to explore some OS ideas in clojure with immutable data and state handling.
Processes are pure functions, their "local" memory is passed to them and then returned on every time-slice. They cooperatively "yield" control of the "cpu" whenever the feel like it.
This has worked well so far. Each processes memory is a map in the :process-table
of the kernel's own memory, itself also a map.
But now I want to enable a process to start a child process.. there is a start-process
function but as its first parameter it accepts the kernel memory map.
Passing the kernel memory to a process (and having it become the return value) is something I want to avoid 😕
What sort of pattern should I use to allow a process to start another process without making exposing the kernel memory to the process function directly?
Storing the kernel memory in an atom, and making a user-land accessible start-process!
function that mutates the atom is a possibility, but is there a better way?
@ramblurr This project sounds really cool! The way I would go about this is to have starting a process (and other system calls) be part of the return signature of your process functions. So presumably the signature right now is, (process-fn [local-memory]) => new-local-memory
?
The way I’ve modeled things like this in the past is by making the signature something like, (process-fn [local-memory]) => [new-local-memory [effects]]
> So presumably the signature right now is, (process-fn [local-memory]) => new-local-memory
?
You’d express your system calls as data structures in effects
, so it could look something like, [:kernel/start-process :some-internal-id arg1 arg2 arg3]
putting some kind of id in there might be good so that the kernel can put the outcome of the system call in the local memory datastructure somewhere so the process can see what happened
one consequence of this would be that the process wouldn't know about the return-value of the system call until the next time its assigned to the cpu
but isn’t that the case in real systems anyway? you make a system call and you’re yielding control
very cool @michael.gaare! thanks for the idea 😄
Good Day everyone, I am using java-time in clojure and I have a newbie question, how can I alias java-time in my project? I just have my require lines at the top, and then a (use 'java-time) line, but I'd like to alias java-time to j or jt so I can do (jt/plus (jt/local-date 2018) (jt/years 5))
maybe there's a nice place I can read about require vs use ? or someone can give me a concise explanation 😄?
Nevermind, I figured it out looking at clojuredocs / use. There is an easy way to add it to the (ns ...) macro, namely (:use [java-time :as jt])
Hmmm... everything seems to work great except I cannot seem to call java-time/interval
Oh, or it might be because there is an interval method in joda-time but not in java-time...
I'm hoping I'm wrong here, but as far as I can tell java-time does not actually implement (interval o) ;_;