This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-02
Channels
- # announcements (1)
- # babashka (19)
- # beginners (85)
- # calva (1)
- # cider (23)
- # clara (19)
- # clj-kondo (28)
- # clojars (4)
- # clojure (60)
- # clojure-australia (8)
- # clojure-dev (14)
- # clojure-europe (117)
- # clojure-nl (3)
- # clojure-uk (11)
- # clojurescript (68)
- # conjure (2)
- # core-async (39)
- # cryogen (11)
- # cursive (7)
- # data-science (1)
- # datomic (9)
- # emacs (10)
- # etaoin (1)
- # events (1)
- # fulcro (1)
- # helix (10)
- # jobs (4)
- # kaocha (2)
- # keechma (3)
- # leiningen (1)
- # malli (6)
- # pathom (15)
- # pedestal (10)
- # re-frame (5)
- # reitit (1)
- # remote-jobs (1)
- # rum (3)
- # shadow-cljs (18)
- # tools-deps (30)
- # vim (6)
Hey guys, I got two questions first would be, what would be a better way of writing this?? (into {} (map (fn [x] (hash-map x (hash-map "booked?" (random-boolean) "nameOfEvent" "nothingBookedYet"))) (get-next-4-weeks-dates))) it would get assigned to a def and returns something like {2020-11-07 {nameOfEvent nothingBookedYet, booked? true} 2020-12-07 {nameOfEvent nothingBookedYet, booked? false} ... } (get-next-4-weeks-dates) returns vector of dates as strings ex ["2020-11-01" ...] second, is how could I filter all trues so that it returns all trues with date aswell 11/01/20 {nameOfEvent nothingBookedYet booked? true}
maybe something like:
(defn get-next-4-weeks-dates []
["2020-11-01" "2020-11-07" "2020-11-14" "2020-11-21" ])
(def date->event
(into {}
(map (fn [dt]
{dt {"booked?" (rand-nth [true false])
"nameOfEvent" "nothingBookedYet"}}))
(get-next-4-weeks-dates)))
(def booked-events
(filter (fn [[dt {:strs [booked?]}]]
booked?)
date->event))
Thank you, ill give that a shot
You can use reduce instead:
(reduce
(fn[acc e]
(assoc acc e
{:booked? (random-boolean)
:name-of-event :nothing-booked-yet}))
{}
(get-next-4-weeks-dates))
Or just using map literals instead of hash-map
already reads a lot better:
(->> (get-next-4-weeks-dates)
(map
(fn[x]
{x {:booked? true
:name-of-event :nothing-booked-yet}}))
(into {}))
I havent uaed a macro before, ill check it out.
I'd reach for zipmap:
(zipmap (get-next...) (repeatedly (fn [] {:booked ...})))
(On mobile, so abbreviated)Ill try zipmap aswell, thank you
With the reduce way, you can even just skip doing the assoc when random-boolean isn't true, so it can filter and create the map on one go.
Is ->> not a macro? Eventually a user will select a date and flip booked to opposite boolean. I appreciate the different approach though.
It is, but, you've definitly been using macros without realizing, things like if
, or
, and
, defn
and more are all macros as well
I just haven't really looked into it. Last time I did the syntax and how it worked looked advanced so I backed off.
Ya fair enough, it is a little more advanced, I remember I took a bit of time before learning to use it as well when I started
Could someone please point me to where Clojure's memory model is implemented? The part where differences are branched off
Do you mean where path copying is implemented for collections that you are updating, so that they return a new collection instead of modifying the one they are given?
If you mean something else, it is not clear from your question
Clojure's memory model is Java's memory model since it runs on the jvm
look at the source for https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentHashMap.java then
based on Bagwell's HAMT http://infoscience.epfl.ch/record/64398/files/idealhashtrees.pdf (with some mods for performance)
I am trying to deploy a package in clojars
. I am getting following error
java.lang.IllegalArgumentException: Must provide valid :files to deploy-artifacts
Can anyone help me with this?@dharrigan thanks buddy
Is there currently a “best” Clojure SFTP client library? “Best” is kind of squishy but something that is relatively well maintained and provides good examples. I was thinking https://github.com/clj-commons/clj-ssh but its tests are currently failing and it is somewhat lacking in examples (was looking specifically for a put
example from an in-memory file).
We’re suddenly having production issues with org.httpkit.client throwing java.lang.IllegalStateException: Client/Server mode has not yet been set
whenever we try to connect to one of our other servers over https. The only thing that’s changed since last Friday when everything was fine is that Daylight Savings Time ended over the weekend. Java version is 1.8 (and not Java 11, which seems to have had a lot of issues with this). It’s a long shot, but has anyone else ever seen a problem like this?
@mafcocinco We use this one: https://github.com/miner/clj-ftp
Thanks! The documentation implies that it is FTP only. Does it work with SFTP connection?
awesome, thanks!
@mafcocinco You actually gave me an idea :) https://github.com/borkdude/babashka.curl/issues/27
sweet!
take-while
as a transducer "takes while" the input satisfies some predicate. easy enough to write but is there a transducer that "takes while" the accumulated value satisfies some predicate?
(defn foo [rf finish?]
(fn
([] (rf))
([result] (rf result))
([result input] (let [result' (rf result input)]
(if (finish? result')
(reduced result')
result')))))
@dpsutton, why not just use the built in take-while
?
(defn take-while
"Returns a lazy sequence of successive items from coll while
(pred item) returns logical true. pred must be free of side-effects.
Returns a transducer when no collection is provided."
{:added "1.0"
:static true}
([pred]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(if (pred input)
(rf result input)
(reduced result))))))
([pred coll]
(lazy-seq
(when-let [s (seq coll)]
(when (pred (first s))
(cons (first s) (take-while pred (rest s))))))))
I agree with hiredman, this is something out of scope for an individual transducer (imagine if it's attached to a channel, what's the accumulator value?). But I guess you could use clojure.core/reductions and wrap the reducing function manually with some transducers if needed, and then take-while the resulting sequence.
transduce calls (f) before applying the xform to f, so you cannot assume result is the value returned by your step fn's init arity
@smith.adriane because no inputs are bad. its when i've taken "enough" inputs that i can stop
the accumulator could be anything. it seems like you may want to write your own transducible process if you want to make assumptions about the accumulator value
i don't follow. using a reducing function assumes i know something about the accumulator shape.
the transducer (map f)
can work when the accumulator is a core/async channel, a clojure sequence, a socket connection, standard out, outputstream, and many other accumulator types. not sure how your take-while
would work with all those examples
(defn part [n]
(fn [retf]
(fn
([] {:value (retf)
:part []})
([{:keys [value part]}]
(if (seq part)
(retf (retf value part))
(retf value)))
([{:keys [value part]} item]
(let [new-part (conj part item)]
(if (= (count new-part) n)
{:value (retf value new-part) :part []}
{:value value :part new-part}))))))
#'user/part
user=>
user=> (let [f ((part 2) conj)] (f (reduce f (f) (range 10))))
[[0 1] [2 3] [4 5] [6 7] [8 9]]
user=> (transduce (part 2) conj (range 10))
([8 9] [6 7] [4 5] [2 3] (1 0))
user=> (into [] (part 2) (range 10))
Execution error (NullPointerException) at user/part$fn$fn (REPL:13).
Cannot invoke "clojure.lang.ITransientCollection.conj(Object)" because "coll" is null
user=>
part there is a kind of naive purely functional transducer that wraps the input result to keep a buffer, and unwraps it at the end
and if you sort of manually do the things when you use it with reduce, it works, but in a "real" usage with a function that takes a transducer it doesn't
it almost sort of works in that example with transduce, but will completely fall apart with different types and reducing functions
if you need to keep a count then you need to keep it in an atom closed over by your reducing function, similar to how the stateful transducers work
https://groups.google.com/g/clojure/c/HK9LkmlRyjY/m/StlzdHxaCwAJ might be the mailing list thread