Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
    I'm using Arcadia right now
    It's clojureclr for unity which uses dotnet
    Yes, it does
    What is the best way to learn clojure very well. I like to solve adventforcode challenges and making websites.
    dead room?
    David Zhu
    i'm stepping through book called "Web Development with Clojure 2nd Ed"

    I'm a bit confused with the following:

    ​ (ns ring-app.core
    ​ (:require [ring.adapter.jetty :as jetty]
    ​ [ring.util.response :as response]
    ​ [ring.middleware.reload :refer [wrap-reload]]))

    ​ (​defn​ handler [request]
    ​ (response/response
    ​ (str ​"<html><body> your IP is: "​
    ​ (:remote-addr request)
    ​ ​"</body></html>"​)))

    ​ (​defn​ wrap-nocache [handler]
    ​ (​fn​ [request]
    ​ (-> request
    ​ handler
    ​ (assoc-in [:headers ​"Pragma"​] ​"no-cache"​))))

    ​ (​defn​ -main []
    ​ (jetty/run-jetty
    ​ (-> handler var wrap-nocache wrap-reload)
    ​ {:port 3000
    ​ :join? false}))

    in -main, threading -> handler var wrap-nocache wrap-reload
    why is var needed here?
    The book says "Note that we have to create a var from the handler in order for this middleware to work. This is necessary to ensure that the var object containing the current handler function is returned. If we used the handler instead, then the app would only see the original value of the function and changes would not be reflected. A more common way to create a var would be to use the #’ prefix as follows: (-> #’handler wrap-nocache wrap-reload)."
    i'm clueless as to what that means. Please help thank you
    Eugene Pakhomov
    @noblecraft I think it talks about the ring.middleware.reload/wrap-reload that reloads the code that you've changed upon a request. I don't know for sure but I think not using var would prevent such code from being reloaded. You can experiment a bit and confirm it yourself.
    James Sully
    (map rand-nth (replicate 5 (range 5)))
    How do I do this more efficiently, without writing the loop myself? (Using library functions) repeatedly was not quite what I wanted
    Eugene Pakhomov
    @sullyj3 Using repeat instead of replicate will be a bit faster (replicate is deprecated in any case). Apart from that - don't know really. I was surprised that using repeatedly results in a slower code.
    Hello. Sorry for noob questions, but I can't understand what I'm doing wrong.
    Here's code:
    censor-word func works fine, and play is updated in play-with-word, but play-with-word only outputs all-censored word and game never ends.
    Eugene Pakhomov
    @GoldsteinE After a superficial look at your code, it seems that \* needs to be in the set of known letters by default.
    Hey all, Not sure of this is the right place to ask such a noob question.
    But in the docs for the reduce function says "...If val is not supplied,
    returns the result of applying f to the first 2 items in coll, then
    applying f to that result and the 3rd item, etc. ..."
    what is val?
    Eugene Pakhomov
    @cyberscientist The second signature of reduce is [f val coll].
    hello guys, im new in clojure and need to take some value defined on my filaA in my fileB, but when i use (println filaA/myDef) i got this: #object[clojure.lang.Atom 0x1edfe27 {:status :ready, :val {:parent {:childOne true, :childTwo true}, :errors []}}]
    how i can convert this #object to json ?
    Eugene Pakhomov

    @YuriMostardeiro Hey. You're asking two questions here:

    1. How to get a value from an atom (given what you provided, fileA/myDef is an atom and not a var)
    2. How to convert a value to JSON


    1. Just use (deref fileA/myDef) or @fileA/myDef. Or maybe don't use an atom at all, if you don't need to change its value
    2. Use this or something similar if you really need a JSON string:
    ah ok cool.
    i used Atom becouse i need to read some file text and change my object values every line readed and put it on memory, then i need to convert this changes (after some business logic) to an output json
    ill try your suggests thx :)
    Eugene Pakhomov
    It still sounds like something that could be done in a immutable way that doesn't require any atoms. But since you're learning, that's OK.
    Benjamin McFerren
    how to reduce in reverse so to iterate from the last index and on through until the 0 index?
    Eugene Pakhomov
    @mcferren Just wrap the collection in reverse before you reduce it:
    Or maybe since it's lazy.
    Beeno Tung

    Hi everyone, I'm new to clojure, appreciate if you can point out how shall I implement this format-size function.

    I want to format byte size into human-friendly format.
    But I feel reduce isn't the right approach if the list is huge (I call format-size on a large list), is there a way to early return (without using exception) ?

    One approach I can think of is to use recursive function with, so I can stop iteration, but then I'll have to urgly decompose in each loop, like
    (first c) and (first (rest c))

    Beeno Tung
    This is how I would do it in 'erlang-style', not sure if there is better way in clojure.
    In the 'erlang-version' it doesn't need to construct and destruct the size and unit, instead they're passed as argument
    Eugene Pakhomov

    @beenotung There's no need for iteration at all. If you prepend an empty string to the vector of units, the index of that vector becomes a corresponding power of 1024 that corresponds to that unit (1024^0 - no unit, 1024^1 - KB, and so on).
    So, you need to just get the logarithm of the input number to the base of 1024, round it down, and get the unit from the units vector.
    Of course, you can still use iteration to actually find the logarithm. Or you could use the built-in math function.

    On a side note - you can stop a reduction early by using reduced.

    Guillermo Martín
    hi everybody
    i'm getting this clj-kondo warning: inline def
    i'd like to do it more idomatic can someone point me in a direction?
    i have a buch of function that share a parameter so i have a def
    but the def depends on the project so i have a (defn set-param [p] (def my-param p))
    which seems a bad idea just don't know how to do it right
    Eugene Pakhomov
    @willemsh_gitlab You can either set! the var created by a def at the NS scope or you can use a dynamic variable if that makes sense. More details:
    Jorge Tovar
    Hello everybody ... Im exploring Clojure and I really love it
    but whats the best practice for error Handling in clojure???
    try catch?
    Eugene Pakhomov
    That's an incorrect question. Some good insights here:
    Viet Vu
    Eugene Pakhomov
    It's used within the metabase.util/prog1 macro which defines that symbol:
    Viet Vu
    Thanks Eugene, I got it!