This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-06
Channels
- # bangalore-clj (16)
- # beginners (120)
- # boot (21)
- # cider (24)
- # clara (9)
- # cljs-experience (1)
- # cljsrn (1)
- # clojure (218)
- # clojure-dev (3)
- # clojure-italy (12)
- # clojure-losangeles (4)
- # clojure-norway (2)
- # clojure-russia (2)
- # clojure-spec (19)
- # clojure-uk (178)
- # clojurescript (52)
- # cursive (7)
- # data-science (55)
- # datomic (25)
- # defnpodcast (11)
- # emacs (5)
- # fulcro (27)
- # hoplon (2)
- # leiningen (14)
- # midje (9)
- # off-topic (132)
- # onyx (19)
- # other-languages (23)
- # portkey (2)
- # re-frame (31)
- # reagent (1)
- # ring-swagger (15)
- # shadow-cljs (58)
- # slack-help (13)
- # spacemacs (22)
- # sql (7)
- # test-check (13)
Hello! :spock-hand: I'm a lisp enthusiast learning Clojure to find real life solutions. While learning I'm migrating a small Nodejs codebase to Clj. Any help you guys can give me will be highly appreciated
Hi @U7V9HE682, welcome! I've never done much with Node, but as far as I know, you'll want ClojureScript for that rather than straight-up Clojure. You might already be aware of that, just wanted to make sure you weren't going down a blind alley.
You may find Lumo interesting; it's a standalone Node-based REPL: https://github.com/anmonteiro/lumo
Also you should be aware that ClojureScript has recently acquired integration with NPM modules: https://clojurescript.org/news/2017-07-12-clojurescript-is-not-an-island-integrating-node-modules
I've been strugling deciding which reducing function to use. Be vanilla reduce, r/reduce or r/fold
uff lots of tasks really. The software I'm writing is a simulator for a queue management system. It should get a collection of "tickets" and handle all of them, updating the state for each.
simplified it should look like this:
(defn handle
[state ticket]
(do-something-with-state-and-return-it))
My suggestion would be to start with regular old clojure.core/reduce
, and only move to one of the others if there's good reason to later (eg performance). There are exceptions, I'm sure, but I think for most people it's rare to reach for reducers right away.
k. After I get this project done I will measure the performance with each reducer function
What would be the point of putting a require form inside a defn here? https://github.com/boot-clj/boot-new/blob/04823d2f1a8d38a2f112dea04bd4cfef2add2bda/src/boot/new/templates.clj#L102-L109
The author says the function is an alternative to pulling in stencil but won't (require '[stencil.core]) do just that?
it means if you don't call that function, then you don't need stencil in your deps
so you can decide, as a user of that code, get a stencil dep and call that fn, or ignore it and not - otherwise you'd get an error from loading the namespace, if the require was in the usual place
I am trying to use vase. 1) Here is what I want to do: I need to call a 3rd party api (I should be able to do that using clj-http i think) and save data in my db (say Datomic here). Now, I am not able to figure out where should I make this 3rd party api call (in a function definition) - should it be in service.clj? If yes, then how can I use that function inside the edn file? And, question 2) Are routes defined i in the service.clj file being overridden by the ones defined in the edn file?
Is there any way in Clojure to write function with lazy arguments? Something like defnlazy
In Clojure that would more likely be a macro
Or a lazy seq
depends on what you’re after when you say “lazy”
Ok, that’s more like a macro then. Can you give an example of where you’d want to use one?
@alexbay in Clojure function arguments are always strictly evaluated. it is macros that have the ability to defer/avoid evaluation (e.g. when
)
Yep. Is there any library that has this kind of macros defnlazy
, so the function could be written like (defnlazy f [a b] a)
?
It’s not a library, you’d just use defmacro
(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
{:added "1.0"}
[test & body]
(list 'if test (cons 'do body)))
The body
arg(s) will not be evaluated unless the test
arg evaluates to true.
That’s probably not a great intro to macros, though, since that’s from the source code for clojure.core
.
Ok, I see. Why I’m asking is because this ability is sort of standard for FP languages like Haskell, Scala.
Yes, but I’m tempted to say that’s because they don’t have macros per se, so they need some other mechanism to serve a similar purpose.
Macros are more powerful (and trickier to master)
is there a clean way to count the maps in both vectors? ie: {:foo [{:a “test”} {:b “test2"}] :bar [{:a “test”}]}
at the moment i have a function that looks like this: (+ (count (get coll :foo)) (count (get coll :bar)))
maybe apply
?
@jeff.engebretsen im not sure how i would use it in this case
(reduce + (map count ((juxt :foo :bar) {:foo [{} {}] :bar [{}]})))
3
is one way to do itI can't find a good way to use apply either.
ignore the suggestion.
(count (mapcat vals maps)) - if the val for every key is a vector, and you want to count all of them
@noisesmith i believe you mean val
, not vals
no I mean vals
oh! input is a single map, I missed that
so really it’s (count (mapcat val map))
it's not special syntax, it's just the name of the function. When you make a defrecord Foo, in addition to the defrecord you get two functions for free, one positional ->Foo
and one associative map->Foo
(defrecord Foo [a b c])
would give you a positional function (->Foo a b c)
and a map/associative function (map->Foo {:a 1 :b 2 :c 3 :other 4})
you'll see ->
in a lot of different contexts in clojure. like the threading macros ->
->>
cond->
etc. or conversion functions in libraries: long->Date
hi! i'm trying to read a part of a file (up until certain line) -- and it appears not so easy from a first sight
here is my attempt which fails with an exception
returning one line in repl
(with-open [r ( yaml-file)]
(take-while #(not (re-matches #"Mappings:" %)) (line-seq r)))
the exception is
IOException Stream closed java.io.BufferedReader.ensureOpen
take-while is lazy
why is it closed exactly after one line outputted
you don’t force inside the with-open, so you don’t try to read the file until it is closed by with-open
don’t mix laziness and io
so i need a doseq there?
wrapping take-while
doseq, or doall
doseq would throw away all your data and return nil though
which probably isn’t what you want
is that a good way at all to read a part of the file?
imean using that with-open
sure, just make sure if anything inside with-open is potentially lazy, force the realization before leaving the with-open body
ok understood, thank you @noisesmith
is there a way to get a str version of the function?
(defn foo [x] x)
(??? foo)
;;=> "foo"
@drewverlee Well, type
or class
would give you the class name of the compiled function... so .getName
on that would yield a string.
boot.user=> (defn foo [x] x)
#'boot.user/foo
boot.user=> (.getName (class foo))
"boot.user$foo"
Is that what you want?and then
boot.user=> (clojure.repl/demunge *1)
"boot.user/foo"
to get the more readable version.So I can understand core.async chan
this (chan 10)
would be like prefetch 10 from rabbitmq? meaning 10 execution in parallel?
10 would just be the buffer size
there’s a couple options
one of the easier ones to use is pipeline
, https://clojuredocs.org/clojure.core.async/pipeline
but it won’t fit every use case
there’s also pipeline-async
, pipeline-blocking
you can also do it manually, by creating multiple consumers from multiple thread
or go
blocks
you can see how pipeline does it
https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async.clj#L478
although it’s a little bit hard to follow
like if you look at https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async.clj#L503
line 503
you can see them choosing between go
blocks and thread
there’s probably some library on top of core async that provides a higher level abstraction
I am repeating something like the following several times in my code:
(-> highlight .-style .-width)
I wanted to see what it would look like to make the above into a function so the attr (`.-width`) could be dynamic. For example:
(defn get-attr
[attr]
(-> (.-style highlight) attr))
The end result being that I could use it like this:
(set! (get-attr .-width) 45)
I have tried a few variations of the above and can't seem to get this to work without crashing the compiler.so, .-attr
isn't a first-class value. it's a special form, like if
, that is built into the clojure compiler, and can only be used statically
@U61HA86AG this being the case, does this mean that the way I was approaching it will not work, yes?
@U6GNVEWQG that is correct 🙂
Is there a way to do this though? I imagine it would be a macro?
At this point, it is a point of interest for me
I think that already exists in goog.object
, https://google.github.io/closure-library/api/goog.object.html
specifically the get
and set
functions in that library
eg. (goog.object/set (goog.object/get highlight "style") "width" 45)
with (:require [goog.object])
I don’t have a repl to test that, but I think that should work