This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-20
Channels
- # announcements (2)
- # architecture (5)
- # beginners (118)
- # cider (5)
- # clara (13)
- # cljdoc (8)
- # cljs-dev (49)
- # cljsjs (2)
- # clojure (107)
- # clojure-dev (9)
- # clojure-europe (3)
- # clojure-italy (58)
- # clojure-japan (2)
- # clojure-nl (6)
- # clojure-spec (89)
- # clojure-uk (27)
- # clojurescript (9)
- # core-async (33)
- # cursive (2)
- # datascript (2)
- # datomic (31)
- # duct (4)
- # emacs (1)
- # events (1)
- # figwheel-main (1)
- # fulcro (69)
- # hoplon (7)
- # hyperfiddle (16)
- # incanter (4)
- # instaparse (4)
- # kaocha (1)
- # mount (2)
- # nrepl (19)
- # off-topic (40)
- # onyx (6)
- # other-languages (3)
- # pedestal (2)
- # re-frame (48)
- # reagent (2)
- # reitit (10)
- # ring-swagger (9)
- # shadow-cljs (63)
- # spacemacs (13)
- # sql (8)
@andrea.crotti siamo bloccati alla 1.6 di Spark (non per volere nostro) quindi Python è un no-no. L'unico clojurista sono io, per lo più abbiamo dei javisti oltre ai data scienziati più puri che non vanno più in là di R o Python. Alla fine Scala non è così male, da quello che ho capito basta non farsi prendere dalla smania di assegnazione compulsiva 😂
buongiorno!
avete per caso idea perchè questo snippet mi da' errore?
(let [message (generate-message)
count-message (count message)]
(message))
sì può fare binding multiplo in clojure vero?
l'errore che mi sputa fuori è Wrong number of args (0) passed to: PersistentVector
si nessun prob con quello. Strano che qualcosa che puoi contare (message) lo puoi anche invocare… appunto
(message)
stai cercando di usare un vector come una funzione, che teoricamente e’ possibile se passi un parametro
ah! è count
che trasforma message
in vector per caso?
ok ok!
hmm e come faccio a tornare message
quindi?
basta che gli tolgo le parentesi giusto?
grazie mille @reborg 🙂
ma a quel punto puoi togliere count-message
perche’ apparentemente non lo usi (a meno che non manchi del codice che non si vede)
voglio fare un loop fino a quando count-message
non è maggiore di 3 e quando lo è ritornare message
🙂
mmmh, molto side-effecty. Guarda http://clojuredocs.org/clojure.core/while
è quello che voglio risolvere è parecchio non deterministico 😄
@andrea.imparato se hai ancora problemi con la sintassi (parentesi vs no) e cose basilari come "come loopare in clojure" io ti consiglio di studiarti le basi su qualche libro, altrimenti non andrai da nessuna parte a cercare di imparare clojure "a occhio"
Vero, ma credo sempre in un buon 50-50% mix libro + side project dove inevitabilmente si sbattono i denti 🙂
@bronsa ho letto parecchio nell'ultimo periodo, sto facendo pratica adesso un po' a caso sì ma è quello che voglio fare 🙂
La soluzione loop
e’ quella piu’ vicina allo stile imperativo:
(defn generate-message [] (vec (drop (rand-int 10) (range 10))))
(loop [msg (generate-message)] (if (> (count msg) 3) msg (do (println "not taking" msg) (recur (generate-message)))))
;; not taking [7 8 9]
;; not taking [7 8 9]
;; [5 6 7 8 9]
C’e’ anche l’opzione di usare lazy sequences. (repeatedly generate-message)
crea una lista infinita di messaggi, ovviamente consuma solo fino ad un certo punto. Aparte lo stile, tra le due opzioni il loop e’ piu’ veloce.
(first (drop-while #(>= 3 (count %)) (repeatedly generate-message)))
non mi piace in realta ma e possibile: (reduce #(when (> (count %2) 3) (reduced %2)) (repeatedly generate-message))
(let [return reduced for-each reduce] (for-each #(when (> (count %2) 3) (return %2)) (repeatedly generate-message)))
😄
La prima reduce
non e’ male secondo me. Non e’ che forza la sintassi o che, solo richiede si apere come funziona reduce
(che alla fine e’ un loop). Metto nel libro 🙂
in english sorry: reduce is arguably actually much better (for eager stuff) since it doesn't force you to know if/when it will hit ireduceinit/coll-reduce, if it can it will go fast, with loop you might have to juggle a bit to hit the right path on the input coll
Not sure I understand the last bit, what do I need to juggle in the loop
above? I see the advantage compared to sequential stuff tho, but reduce
drives a loop behind.
ex for stuff that can be reduced over (ex stuff produced by (range ...) (cycle ...) eduction and whatnot)
it's possible to get to quite same byte-code probably via loop, but that would require quite a lot of know-how of the internals of the colls and it's way more advanced in these cases
grazie a tutti! è per questo che amo questa community 🙂
Faccio una domanda anche io: in questo caso fare un channel bufferizzato e recuperare in modo non bloccante solo quando si riempe potrebbe essere un'altra soluzione? (ovviamente dipende molto da quello che uno deve farci)
e` un overenginnering mostruoso rispetto a quello che doveva fare @andrea.imparato