This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-06
Channels
- # beginners (90)
- # boot (83)
- # cider (39)
- # clara (4)
- # cljs-dev (124)
- # cljsrn (10)
- # clojure (208)
- # clojure-boston (1)
- # clojure-italy (13)
- # clojure-nlp (3)
- # clojure-russia (34)
- # clojure-spec (63)
- # clojure-uk (101)
- # clojurescript (65)
- # community-development (13)
- # copenhagen-clojurians (1)
- # core-async (1)
- # cursive (24)
- # datascript (1)
- # datomic (65)
- # emacs (20)
- # graphql (20)
- # hoplon (21)
- # instaparse (18)
- # jobs (5)
- # jobs-discuss (2)
- # leiningen (8)
- # luminus (32)
- # midje (1)
- # mount (3)
- # off-topic (18)
- # om (10)
- # parinfer (6)
- # pedestal (2)
- # planck (2)
- # precept (22)
- # protorepl (7)
- # re-frame (45)
- # reagent (9)
- # ring (1)
- # ring-swagger (4)
- # rum (2)
- # spacemacs (5)
- # sql (2)
- # unrepl (13)
- # untangled (8)
- # yada (5)
I'm using compojure and midje to develop a small REST application and run some test. I wanted to mock some requests, but I'm failing. Can somebody help me out with this? Tyvm
victora: perhaps this link? http://blog.josephwilk.net/clojure/isolating-external-dependencies-in-clojure.html
There’s ring-mock: https://github.com/weavejester/ring-mock
How can I “map” vector of functions over a single value? I.e. is there a simpler way to achieve this:
(map (fn [f] (f 3)) [str (partial + 3) (partial * 5)])
;;=> ("3" 6 15)
Thanks!yeah, that’s pretty much the same 🙂
I was looking for a function that would take a value to be applied for a couple of functions 😉
what about juxt
?
@manutter51 indeed it’s juxt
Thanks!
So, I'm writing code to manipulate a BufferedImage, and am thinking that a nested list comprehension would be the right tool for the job. However, it feels imperative and thus morally impure. Does this feeling ever happen to anyone else?
a nested list comprehension would return a new list which wouldn’t help you at all
what that data type requires is a nested procedural loop
morality is for suckers
OK - I don’t really mean that, what I mean is that moral / immoral is an extremely unhelpful model in software design. The right question is what are the benefits and costs of a particular programming approach, and an immutable / declarative approach is unlikely to help you make a buffered image
A new list is what I do want. getRGB()
gave me an array of integers, and I want to produce a new set of integers which I can pass to setRGB()
and create a new BufferedImage
.
why a list?
I mean, clojure has immutable associative sequential data (vectors) if you want immutable data
Because getRGB()
returns a one-dimensional array of integers, (int-array)
will take my output list and make it into the array of integers that setRGB()
expects.
so what exactly did you have in mind that would be less imperative and therefore more admirable?
Using map and reduce to transform the input data to the output.
reduce is more imperative than a list comprehension, if we are ranking things
for is lazy and declarative, reduce is eager and procedural
though if the problem matches reduce, we have areduce that can be used to directly consume an array and put the result into another array https://clojuredocs.org/clojure.core/areduce
especially since the size of the output and the type of every element is known beforehand
or perhaps more directly, if it’s a pixel->pixel transform, amap https://clojuredocs.org/clojure.core/amap
all that said, if you need to speculatively do many parallel transforms, or do more complex and generalized operation where immutability helps, sure, use for or reduce…
Thank you; I did not know about those.
@jdeisenberg though I was dismissive about concerns about code being “morally impure”, I do think I understand and agree with the underlying concern. If code is meant to be reusable with idiomatic clojure code, it should prefer immutable and declaritive code that is referentially transparent (immutable, avoids hidden state, avoids custom data types). But code that transforms one image into another is fundamentally not thing IMHO - it’s not something that could be at the core of an idiomatic clojure ecosystem, it’s an edge touching a specific data representation useful to a specific domain
if you were writing a library for making images, you could consider how to design things so that clojure patterns of using plain immutable and referentially clear data and functions would be able to cleanly transform an image, but when directly interacting with the image, you not only need to get your hands dirty with a mutable array type and some wonky non-general apis, you also will benefit from not trying to impose clojure design concerns on that boundary
there’s a place to think about good clojure design! - but that place isn’t the direct interaction with an existing procedural / OO api - it’s a bit further into a thing that you may not even need to build
that’s way too much… TL;DR - if you want to transform one array into another, amap is super direct; if you want to make a general and pure interface to images from clojure that’s a much bigger job and you still might end up wanting something like amap in the code that directly interacts with the underlying api
somewhat related to the discussion about purity and edge-touching: i’m reading daniel shiffman’s The Nature of Code, and have been trying to think about how to translate it into quil
but have been tripped up w/r/t the checkEdges
function: https://github.com/shiffman/The-Nature-of-Code-Examples/blob/master/chp02_forces/NOC_2_1_forces/Mover.pde#L37
the problem is that if the position is out-of-bounds, then both position and velocity need to be updated
Hi @swizzard, I wrote that code a while back. Maybe another approach is to accumulate changes (for x and then y tests) and then swap the atom after checking if anything changed (touched an edge)
If you're avoiding atoms, I think that approach would work if it's wrapped in a fn and spits out the accumulated changes (changed coords)... or the same coords (for no changes)
but that a) uses an atom and b) won’t address cases when the x and y coordinates are oob
@swizzard (let [[x y] (if (...) [....] [....])] ...)
is a general pattern for when you need to update two valus
just be sure to return both values from each branch of the if
I’d just destructure the 4
or reify the condition and reuse it in a series of bindings
that’s a decent option, yes
sure, let lets you do multiple bindings
(defn check-bounds [^PVector loc ^PVector vel]
(let [lx (.x loc) ly (.y loc)
vx (.x vel) vy (.y vel)]
(when (> lx (q/width))
(do (.set loc q/width ly)
(.set vel (* vx -1) vy)))
(when (< lx 0)
(do (.set loc 0 ly)
(.set vel (* vx -1) vy)))
(when (> ly (q/height))
(do (.set loc lx (q/height))
(.set vel vx (* vy -1))))
(when (< ly 0)
(do (.set loc lx 0)
(.set vel vx (* vy -1))))))
i really want to believe there’s a better move here, i’m just not smart/experienced enough to figure it out
(cond-> [loc vel] (x-wrap? loc vel) (do-x-wrap) (x-under? loc vel) (do-x-reverse-wrap))
where do-x-wrap and do-x-reverse-wrap return a new loc and vel
or smth like
(defn check1 [[pos vel]]
(let [px (:x pos)
vx (:x vel)
npx (if (> px width)
width
(max 0 px))
nvx (if (or (> px width)
(< px 0))
(- vx)
vx)]
[(assoc pos :x npx)
(assoc vel :x npx)]))
(defn check2 [[pos vel]]
(let [py (:y pos)
vy (:y vel)
npy (min py height)
nvy (if (> py height)
(- vy)
vy)]
[(assoc pos :y npy)
(assoc vel :y nvy)]))
(-> [position velocity]
check1
check2)
right - it’s the same pattern, each function takes both position and velocity and returns them both
the only annoying thing about cond-> is that the checks can’t use data created mid stream
the checks have to use data that’s visible outside the cond-> block
but if you can make that work, cond-> is great
sometimes that means that instead of one (cond-> …) you have (-> x (cond-> ….) (as-> … (cond-> ….)))
where as-> is the thing that saves your ass by letting you bind things mid stream
but there’s always plain old let blocks too - remember that a let value expression can refer to bindings before it in the block
Good evening all. I'm looking at Clojure templating libraries. While I like hiccup, I prefer creating my HTML templates separately and getting them looking just right, so I was looking at Selmer, Enlive, Laser, or Fleet. Unfortunately all the ones I looked at have no commit more recent than 2 years ago. What templating libraries are current, or is pretty much everyone just using hiccup?
Selmer is still active: https://github.com/yogthos/Selmer/commits/master
not crazy busy, but not devoid of commits, either.
Thanks. I must have been looking at a clone
Is it in wide use, or do most just use Hiccup?
I can’t speak for the community as a whole - my last project was via Hiccup through.
that said, there’s nothing stopping you from doing an API on the backend, and a client-side SPA in the front, either.
https://clojars.org/selmer vs https://clojars.org/hiccup downloads show about a 1:10 ratio between the two projects
Interesting, thanks. I wonder if that disparity is due to hiccup being used for people learning clojure with web experiment. Hiccup seems great for small projects, or experimenting with clojure. My problem may be my newness to Clojure too
I imagine if I want my next job to be as a Clojure dev I need to learn to be functional (pun) with both Selmer and Hiccup, so I'll look at both of them.