This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-12
Channels
- # adventofcode (6)
- # beginners (148)
- # boot (5)
- # calva (1)
- # cider (10)
- # cljdoc (10)
- # cljs-dev (8)
- # cljsrn (10)
- # clojure (180)
- # clojure-dev (24)
- # clojure-europe (2)
- # clojure-finland (1)
- # clojure-italy (32)
- # clojure-losangeles (1)
- # clojure-nl (40)
- # clojure-spec (10)
- # clojure-uk (44)
- # clojured (4)
- # clojurescript (88)
- # community-development (33)
- # core-async (7)
- # cursive (19)
- # datomic (98)
- # duct (3)
- # events (1)
- # figwheel-main (10)
- # fulcro (62)
- # leiningen (23)
- # luminus (18)
- # off-topic (19)
- # pedestal (6)
- # re-frame (46)
- # reagent (21)
- # ring (17)
- # ring-swagger (3)
- # shadow-cljs (94)
- # slack-help (9)
- # spacemacs (14)
- # sql (1)
- # testing (4)
- # tools-deps (14)
When you map a function over something what is returned? is it a collection of return data?
But yes a collection of that function applied over the data, just not realized until you ask for it.
I see. It worked the way I thought it should. I was trying to then map println which resulted in nil. Instead I had to apply println.
Did you do that in the repl?
repl will realize your map function for you
Gotcha
Trying a simple project in Clojure. I've literally been reading about it for years, but never done anything with it.
Are you enjoying it so far?
So far so good. There are definitely some nice libraries to make what I'm trying to do easy.
That’s good! Calva looks like a neat tool I will have to check it out sometime.
I had to rework my approach so it would be REPL friendly. I was nesting my functions in a way that made it difficult to work with at first.
Ah yeah, thread macros ->>
->
make nesting readable
working with thread macros lets you think about composability of your code
I'm looking to store a map in an atom, then use clojure.data.diff to see when it changes. If it changes, I want to update the value in the atom, then trigger an action.
@scott.archer You can add a watcher to an atom so that function is called when it changes.
It sounds like a watcher could work to trigger the event based only on the change. Would that be a good way to go about this?
Then you could check the old and new state and see if it changed in a way that you need to trigger your event.
But if your "check" also updates the value in the atom, you might get into a loop 🙂
I'm not really sure what you mean by "If it changes, I want to update the value in the atom"
I'm scraping a website looking for product changes, then I want to generate a text message if something has changed.
Yeah, I think this would work for you then. The watcher function would check whether old state = new state and, if not, generate the message.
Yep it's worth trying. Thanks for the help. The last time I tried to get started on a clojure project, I got some stuff working, but it wasn't done the clojure way and it didn't feel any different than java.
Getting into the functional mindset is (at least) half of the battle if you're coming from Java, I think.
It is and I think writing code that decomposes well so that the REPL is useful seems really important too.
Aye, the REPL is a game-changer, once you get into the flow with it (see Stu Halloway's talks "REPL-Driven Development" and "Running With Scissors").
Key advice: never type directly into a REPL. Type into a (source) file instead, and evaluate each form as you type it. You can put REPL experiments inside a (comment ..)
form to stop them affecting the source file when it is loaded. And keep them there as notes on what worked (and perhaps what didn't).
@seancorfield do you use Calva by any chance?
I have used it occasionally. Mostly I use Atom and Chlorine.
It's fairly standard in editor integrations to prevent crashing/locking up when you accidentally print very large data structures.
If it shows ...
in the output, it may be clickable to expand the data some more.
There's a #calva-dev channel in case you need to ask detailed Qs about it.
This might be relevant (from that channel): https://clojurians.slack.com/archives/CBE668G4R/p1549729716098300
Hi there, I'm trying to update the specific entry in a map upon predicate and at the end I get a map with only the updated entry (the rest of entries eventually turn into nil):
(update data :sms (fn [coll]
(map #(when (= id (:id %))
(conj % {:sent? true}))
coll)))
You can use (map #(merge % (when (= 1 (:id %)) {:sent? true})) [{:sent? false :id 2} {:sent? false :id 1}])
you can in this case also use assoc instead of conj, personally conj is confusing for me in this instance
I derived t
by multiplying by 1000, or more to the point, by editing out the "
s and the .
:face_with_rolling_eyes:
But this is no good:
(let [given-t "1549913529.537"
t (Double. given-t)]
(java.util.Date. t))
nor is this:
(let [given-t "1549913529.537"
t (Integer. (* 1000 (Double. given-t)))]
(java.util.Date. t))
I've just constructed my first ever BigInteger
.... which java.util.Date
has also never heard of
Float/Double are formats for values with decimals
They are neat (and terrible) in that they can represent a very wide range of values between absolutely tiny (e.g. 0.00000000000001) and massive in 32/64 bits respectively
they do this by trading off accuracy for range. So some values you can device are not representable in float/double
0.1 + 0.2 = 0.30000000000000004 https://floating-point-gui.de/
not sure what the constraints of the initial string are, but if its precision is always 3 you can just delete the decimal to multiply by 1000 and then parse it?
I think that is the case --- this is super-prototypey so it's okay if it explodes later
@mathpunk I'm sure BigInteger is fine here, but I can think of very few cases I'd use Integer
over Long
similar with Float vs. Double
prefer the 64 bit types
(in Clojure, longs can store values between -(2^63 + 1) — 2^63, so they are signed)
[-2^63, 2^63-1] inclusive (sorry can't resist pointing out off by one bugs)
… right
Thanks!
I actually googled it even, but assumed the wrong value :’)
i remember hearing a neat thing. The minimum value in ranges like this is not representable as a literal
ie if the range is [-16, 15] you can't represent -16 as a literal of that type. you get type promoted
You mean, in Java programs?
I know of rust that did strange things with non-inclusive ranges for all byte values
like if you said 0…256 non-inclusive
its because -X is parsed as unary operator negation on X. and the minimum is usually larger in absolute value terms than the maximum. so in the range [-16, 15] -16 is (negate 16) and the 16 there is too large for your type so you get type promotion
user=> Long/MIN_VALUE
-9223372036854775808
user=> (type -9223372036854775808)
java.lang.Long
and (type -9223372036854775809)
?
ah bigint as expected
First time I have seen Slack's animation for deleting a message. Cool!
java does not have this problem apparently. (type (- 9223372036854775808))
is BigInt
user=> (type (unchecked-subtract 0 9223372036854775808))
clojure.lang.BigInt
of course this is because 9223372036854775808 is already bigint
yes that number is too large for Long. There are some languages that don't read -9223372036854775808 (a valid Long) as this number but as negating 9223372036854775808 (a positive number) which is too large for longs
and java does have this defect (it seems)
HelloWorld.java:5: error: integer number too large: -9223372036854775808
long x = -9223372036854775808;
^
1 error
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
long x = -9223372036854775808;
}
}
another way clojure is better than java :D
-
is a unary operator in Java right
so, it first parses 9223372036854775808
as long, but that isn’t a long
yeah - and since clojure doesn't need to use java for parsing, I'm not surprised that bug is fixed
i just thought that was really cool when i learned it. a value is not expressable as a literal
yeah, but it makes sense if you think about how that would be structured in a grammar
class Main {
public static void main(String[] args) {
System.out.println(- - 6);
}
}
Yeah, unary operatorit looks like the compiler uses a regex to match integers. but it looks like it always returns a bigInteger. so i'm a little confused
can you link the source ^^ kinda intrigued
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L69
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L467
Cool, thank you both!
I have almost no experience in the compiler but I want to change that. Noisesmith is well versed in it. I assume a few patches as well?
oh no, just a lot of inquisitive poking :D
but yeah, the pattern on line 69 contains -
/`+`, so the value is parsed whole instead of being negated later on
(as is the case in the Java grammar)
ah no, it reads everything into biginteger and then tries to go down to longs if possible
I have never seen this time format before --- 0211/133208.940471
. That's MMDD/??????.???
star date?
133208 is more than the number of seconds in one day, so not sure what that is intended to be.
Where did you find that?
thanks, @andy.fingerhut, i was hoping someone would have recognition on whether this was quite likely ridiculous
Seconds since the process started, maybe?
Logging tends to proliferate time representations.
s/time representations.//