This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-22
Channels
- # aws (1)
- # beginners (102)
- # boot (5)
- # cljs-dev (59)
- # cljsjs (1)
- # clojure (154)
- # clojure-australia (1)
- # clojure-brasil (1)
- # clojure-dusseldorf (4)
- # clojure-greece (36)
- # clojure-italy (10)
- # clojure-poland (5)
- # clojure-romania (1)
- # clojure-russia (7)
- # clojure-spec (32)
- # clojure-uk (113)
- # clojure-ukraine (3)
- # clojurescript (107)
- # cursive (13)
- # data-science (25)
- # datomic (23)
- # emacs (3)
- # events (1)
- # fulcro (72)
- # funcool (10)
- # graphql (1)
- # leiningen (1)
- # luminus (2)
- # lumo (38)
- # off-topic (14)
- # onyx (78)
- # planck (4)
- # re-frame (55)
- # reagent (1)
- # ring (3)
- # ring-swagger (2)
- # rum (19)
- # shadow-cljs (89)
- # spacemacs (101)
- # sql (2)
- # unrepl (88)
Real quick for anyone who's up, is [this](https://pastebin.com/TV3Ebabk) a correct database-esque map?
It all depends on what you are trying to do. However, within your paste you are using defn
within map declaration. This isn’t doing what you think it is doing I think. It is now creating three named functions into current namespace: those will be visible directly on the namespace. If you want to have those functions within the map and still have them as named functions you have two options:
1) use (fn deposit-calc [& x]...)
which will create a named function which isn’t tied to any namespace. Name in this context is mostly useful for stacktraces.
2) do following:
`
(defn deposit-calc [& x]
....)
...
(def account-map {...
:dep-amnt deposit-calc
... })
´``
But anyway, if you want to model data within clojure datastructures in such a way that it would be your “database” then no, this is not a proper way to model such a thing.
Also, these days I would suggest you to dig into Clojure Spec to define the spec definitions for your data (more or less can be described as your “database schema”)
I have a incoming JSON post request and if I do a (js->clj ( :body req)) I get [object Object] --- I figure I am missing something simple?
boy is it annoying trying to accomplish an indexed kind of do-seq
without shooting your own foot
@matan Check out indexed
from https://github.com/weavejester/medley
I think I need a slightly more detailed use-case to understand what you're after @matan
you can use doseq, and just wrap your sequence in (map-indexed vector coll)
(defn indexed [coll]
(map-indexed vector coll))
(doseq [[idx row] (indexed rows)]
(write-to-file idx row))
yeah, like that
I'd change ((merge {product []} @products-ticks) product)
to (get @products-ticks product [])
@madstap Ah yes, that is a leftover from when I tried to implement something like pythons defaultdict. That doesn't make any sense here anymore. Thanks for spotting?
looping with an index sure does bring python to mind @tobias994
a simple dumb language that lets you shoot your own foot, but lets you have readable loops like you were 5 years old lol 🙂
@matan It might be a dumb language but at least I didn't feel as dumb as I feel know when trying to get Clojure. But what do you mean with "looping with an index".
@madstab Is the use of let ok? Would there be a readable version without? What about the fomating/intending?
@matan https://clojurians.slack.com/archives/C053AK3F9/p1511370752000273 indexed
here is the equivalent of Python's enumerate
@tobias994 yes clojure requires a mind flip if you've done non-functional programming for a long while, it is not because anyone is stupid, rather because rewiring what you already know to do into a different form is oftentimes one of the more difficult things
@tobias994 Yeah, I'd keep the let. The code's pretty alright, I think, without really understanding what it does. I'd change the underscores to hyphens and write some docstrings though.
An alternate implementation to consider could be something like:
(defn store-tick [product time best_ask best_bid]
(let [f #(push-cyclic % {:time time
:best_ask best_ask
:best_bid best_bid} 1000)]
(swap! products-ticks update product (fnil f []))))
I’m felling dirty .. finally I started my new startup project with clojure I’ve read a lot of books and did a lot of questions here ( I thought I was ready …. I’m not 🙂 )
How would you guys change that simple code feature: signup user , and after signup send sms to mobile user
(defn send-activation-sms [phone-number text]
;; it will be a sms component ( twilio / amazon sms )
(println phone-number))
(defn activation-codes [n]
(clojure.string/join
(take n
(repeatedly (partial rand-int 10)))))
(defn user-does-not-exists [db phone-number]
(not (boolean (users/find-by-phone-number db {:phone_number phone-number}))))
(defn create-user [db phone-number]
(let [api-key (.toString (UUID/randomUUID))
activation-code (activation-codes 6)
activation-code-expiration (t/now)]
(users/create db {:phone_number phone-number
:activation_code activation-code
:activation_code_expiration activation-code-expiration
:api_key api-key})))
(defn signup [{:keys [components params] :as request}]
(let [phone-number (:phone_number params)
db (:db components)]
(if (user-does-not-exists db phone-number)
(let [user (create-user db phone-number)
_ (send-activation-sms phone-number "sms")]
(r/response {:api_key (:api_key user)} ))
(r/response {:error "User already exists"}))))
(defn books-by-author [author books] (filter (fn [book] (has-author? [book author] author)) books ))
I'm having dificulties because has-author?
has two parameters and don't know how to build the helper function for filter
well your predicate is separate. can you test that (has-author? wild-seed octavia)
returns what you expect?
can you paste your books-by-author
function? It looks like the call is malformed in your previous code
@madstapThanks a lot. That looks much better. Using let to move an anonymous function to the top makes it more readable. I didn't know about fnil yet and you forced me to understand !swap a bit better.👍
@dpsutton Thank you very much. It's the first time I had this situation. The brain is not yet completely formatted to Clojure 😉
@matan regarding indexing, why not just (let [v (vec col)] (dotimes [i (count v)] (do something with (get v i) ...)))
@noisesmith sorry to ask you directly … https://clojurians.slack.com/archives/C053AK3F9/p1511375305000122 how would you change that feature ?
@oliv - some small trivial things to start with - (not (boolean x))
is (not x)
, (clojure.string/join coll)
can be replaced with (apply str coll)
, in the body of signup, it might make more sense to move the nested let into the let above, - I find 1 let and three ifs nicer than two lets and an if in between
oh, and .toString
can be replaced with str
- almost always
yeah, looks decent over all
I thought once upon a time I saw someone make an uberjar with leiningen and make it so that the resultant jar file was saved to a different path. Is that a thing or did I just imagine it? How do you do that?
I tried altering :target-path
but that just made it so all the build artifacts moved there.
I have a piece of code like this:
(defn do
[]
(when condition-met
do-stuff))
The above works and does what I expect. The issue is when condition-met
is false. The above function do
is going to return nil
. I would like it to always return an empty list instead.
I could resolve this with if
, but I wonder if there is a better way?I can’t imagine a more straightforward way to do this than using if
possibly using cond
(cond
condition-met
(do-stuff)
:else
())
but i would still use an if
I think what is holding me back is that do-stuff
looks like this:
(let [sorted-events (sort-by :start-time events)]
(remove nil?
(for [[x :as xs] (take (count sorted-events) (iterate next sorted-events))
y xs
:when (not= x y)]
(find-conflict x y))))
(or (and condition-met (do-stuff)) ()) would work
which makes the false difficult to see
never mind, clearly (if condition-met (do-stuff) ())
not making the else branch of an if
apparent is something I’ve noticed
but i’m not sure how to improve it
especially if the else is something like ()
I always put the true / false cases of the if on separate lines
no matter how small they are
sometimes, if the else case is really small, I use if-not
so that the shorter branch is on top
but that doesn’t always improve readability
I don't necessarily agree. In my experience, an if
with no else clause isn't uncommon as long as you know it returns nil
on falsy.
I usually use when
instead of if
if there’s no else clause
Well, you learn something new every day. From now on, I'll be using when
rather than if
when it's idiomatic.
Funny story, though. If you look at the source for the when
macro, it actually expands to just use if
. 😄
True. Thanks all, going to go with the if
.