Fork me on GitHub
#clojure
<
2017-01-18
>
sova-soars-the-sora01:01:45

So i'm trying to work out Djikstra's Algorithm in clojure... How can I "iterate over" a #{:hash-set :of :elements} ? very beginner fp question.

seancorfield01:01:54

A set can be treated as a sequence so you can map and reduce over it.

seancorfield01:01:06

boot.user=> (map inc #{3 1 2})
(2 4 3)

seancorfield01:01:46

(there is a #beginners channel in case you feel more comfortable asking those sorts of questions there)

seancorfield01:01:57

map and reduce etc call seq on their collection argument — you can call seq directly on a set:

boot.user=> (seq #{3 1 2})
(1 3 2)

seancorfield01:01:31

(you always get the same order from the same set, but that order is essentially “random” across all sets)

sova-soars-the-sora01:01:12

Cool. Thanks @seancorfield ! 😃 map and reduce are powerful indeed.

qqq06:01:24

in spec, how do I efine somethign that matches everything of the form [:foo any?] so I want everhthing that is a vector of length 2, where the first arg is :foo, and the second arg can be anything

qqq07:01:08

@bfabry: exactly what I needed; thanks!

qqq08:01:26

I've already used spec/fdef to spec a function. How do I have spec (1) generatea . test case and (2) tell me how it worked?

qqq10:01:54

@qqq: s/exercise-fn

xificurC10:01:05

is there a macro capturing an idiom like this? (let [x '()] (if (test x) x :some-default)) Something along the line of

(defmacro if-test [x testfn else] 
  (let [x# ~x testfn# ~testfn else# ~else] (if (testfn# x#) x# else#)))

tvalerio11:01:15

@sveri @tcrawley good morning! So, I've fixed the problem of the main function and the schedule fires ok, but not as I spected.

tvalerio11:01:11

Fox example, I need the schedule to fire every day at 00:30, but when I start the application, the schedule fires immediately

tvalerio11:01:34

Using quartzite the result is almost the same, the schedule does not fire

tvalerio11:01:23

if I change the interval, to 1 minuto for example, the schedule waits 1 minuto and then fires, not respecting the time I need

tvalerio11:01:47

I am doing something wrong setting the schedule properties?

schmee11:01:11

xifi the closest thing would be if-let

schmee11:01:49

or maybe if-some

xificurC11:01:55

schmee yeah I looked at a couple of these macros and some resemble the task more, some less, none perfectly. Since an empty sequence is true in boolean context one cannot do (or x :default)

qqq12:01:15

in spec, number? allows NAN; is there a somethign similar but does NOT allow NAN? NAN breaks everything as NAN != NAN

gfredericks12:01:21

there oughta be an anything-but-NaN too

gfredericks12:01:54

which I guess is ambiguous though, since you might want to also exclude collections which contain a NaN in any fashion

gfredericks12:01:11

NaN-free? augh it's all terrible

joshjones12:01:42

double-in allows a range of numbers and allows excluding NaN, +/-Infinity, etc

qqq12:01:06

yeah, I just realized that; looks like spec / math may not be as fun as I thought

qqq12:01:22

guess I should be greateful it's cosidering all these edge cases I'm not thinking of, lol

qqq12:01:39

speccing (fn [a b] (+ a b)) on int? isn't een fun either

qqq12:01:44

it's "this hollds, only if there's no overflow"

gfredericks12:01:54

it makes you not want to write functions with edge cases :)

qqq12:01:32

@gfredericks : how would you rewrite (fn [a b] (+ a b)) ?

qqq12:01:41

if I can't even spec +, I may just not spec at all

gfredericks12:01:28

it would depend on the domain; you could respec it to be only bigints, or you could use +' instead, for example

joshjones12:01:54

@qqq if overflow is a concern, you should be using long anyway

qqq12:01:02

it's not the conern

qqq12:01:06

the concern is lerning to use spec on functions

qqq12:01:14

in such a way that there's no false positives and it's as powerful as possible

qqq12:01:37

s/fdef and test/check are very powerful, but I don't know how ot use them properly yet

gfredericks12:01:35

I think PBT makes you a lot more conscious of edge cases, but clojure.core has a lot of edge cases

joshjones12:01:51

spec'ing + is fine, but if you’re learning to spec functions, maybe start with something a little more specific to your domain.

gfredericks12:01:55

I use tools like this to avoid those edge cases

gfredericks13:01:22

property-based testing; test.check etc.

qqq13:01:27

I'm getting breath analyzer tests and forms of plastics on gogole.

jannis13:01:53

Hi. Is there any way to do something like (let [(make-bindings …) {:foo :value-of-foo}] …) where make-bindings generates the bindings at runtime?

jannis13:01:26

Obviously, I can create a macro that does this. I assume there is no other way?

gfredericks13:01:01

only at compile-time via macros

jannis13:01:09

Mhm. I’m trying to make the following two scenarios work, where I generate a destructuring form from a macro input that is either already a “query” or an expression that generates a “query”. The problem is that I can’t really evaluate the second case prior to expanding the inner macro (`with-query-bindings`): https://gist.github.com/Jannis/af5c0dfed612fae19cb7b38705ad561c#file-nested-macros-clj-L19

gfredericks13:01:38

@jannis (make-query) is a function call, so that result isn't even available until runtime

jannis13:01:17

Even if make-query was a macro, it would only be expanded after the surrounding with-query-bindings macro is expanded, right?

gfredericks13:01:03

yeah that's also a problem; you might want to back up and describe your higher-level goal

h.elmougy14:01:15

I need to route based on parameter presence (GET "/transactions" {{trxid :trxid} :params}) (GET "/transactions" {{trxdate:trxdate} :params})

h.elmougy14:01:29

how can I achieve this

h.elmougy14:01:52

I'm always got the first route got routed

sveri14:01:14

@h.elmougy How about you define one route and dispatch on the parameter?

nooga14:01:21

Is there a clever standard function that computes ((juxt (partial filter odd?) (partial remove odd?)) (range 5)) simultaneously? Or, perhaps, a neat way to achieve this with transducers?

h.elmougy14:01:42

@sveri I already done that but i wanned to eliminate the if else with delegating the condition to destructuring the params

sveri14:01:27

I am not sure if compojure can do that

karol.adamiec15:01:40

there is int and UUID, but you should be able to write own easily?

sveri15:01:07

@karol.adamiec Nice, I did not know about these 🙂

weavejester15:01:30

You could also use if or when. If a route returns nil, it’s treated as not matching.

karol.adamiec15:01:37

(GET "/basket/:uuid" [uuid :<< as-uuid ] {:body  (get-basket db nil uuid)})
 (GET "/basket/:username" [username] {:body  (get-basket db username)})

weavejester15:01:23

[trxid :<< identity] would probably work as well.

weavejester15:01:05

(GET “/transactions” [trxid :<< identity] …)

h.elmougy15:01:38

@weavejester this seems elegant. thanks

joshjones15:01:22

@nooga not that I know of, let me know if you work something out like this because it’s an intriguing concept. just did a reduce-based solution for a single pass but it was not faster than the filter/remove version you did

djjolicoeur15:01:46

is there anywhere I can look to track the Status of Clojure 1.9? or does anyone know approximately when it will be released? I’m trying to do a bit of planning for what we want to do this year on my team and just trying to figure out if/when 1.9 and spec could become a possibility. I can branch with the 1.9 alpha and get things rolling on our various products, but I’d like to limit the time I have to maintain a parallel fork

nooga15:01:56

@joshjones I’ll keep you posted 🙂

Lambda/Sierra15:01:38

@djjolicoeur Rich Hickey et al have generally avoided roadmaps with specific features or dates. "It's done when it's done." That said, the Clojure 1.9 alpha releases are only really "alpha" with respect to new features such as spec. Generally, I would regard it as safe to use Clojure 1.9 alphas, with the understanding that new features, such as spec, may still be subject to breaking API changes. Of course, you should still run tests on your application and watch the release announcements for potential issues.

baptiste-from-paris16:01:30

hello, I am looking at source-fn source code; I can’t understand how the reader knows the end of the line to read (which is not specify in (meta ‘sym)

(defn source-fn  [x]
  (when-let [v (resolve x)]
    (when-let [filepath (:file (meta v))]
      (when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)]
        (with-open [rdr (LineNumberReader. (InputStreamReader. strm))]
          (dotimes [_ (dec (:line (meta v)))] (.readLine rdr))
          (let [text (StringBuilder.)
                pbr (proxy [PushbackReader] [rdr]
                      (read [] (let [i (proxy-super read)]
                                 (.append text (char i))
                                 i)))
                read-opts (if (.endsWith ^String filepath "cljc") {:read-cond :allow} {})]
            (if (= :unknown *read-eval*)
              (throw (IllegalStateException. "Unable to read source while *read-eval* is :unknown."))
              (read read-opts (PushbackReader. pbr)))
            (str text)))))))

baptiste-from-paris16:01:41

does someone can help me ?

dpsutton16:01:18

I believe its how read works

dpsutton16:01:39

read will read one valid form

dpsutton16:01:57

so it looks like if you have two defns on a line it may not work correctly with source

baptiste-from-paris16:01:42

ok, I should try then

baptiste-from-paris16:01:00

@dpsutton you are right, if you define 2 defn on the same line and try to`source` the 2nd one, it wont work

dpsutton16:01:27

it returns the first for both?

dpsutton16:01:31

or does it say source not found?

abdullahibra16:01:32

(/ 22 7.0) 3.142857142857143

abdullahibra16:01:44

how can i take only two or 3 number after dot ?

baptiste-from-paris16:01:45

@dpsutton metagive you the column information, so it should be possible to make it work but not very usefull. I never saw 2 inline defn

dpsutton16:01:07

not a huge bug

dpsutton16:01:19

i'm sure there are other issues on the clojure team's platge

baptiste-from-paris16:01:55

lol, offcourse, I was just playing with source

joshjones16:01:28

@abdullahibra this may be too naive but i just did it so it gives you an idea at least. might be rounding issues, just test thoroughly:

(defn precision
  [n x]
  (let [p (Math/pow 10 x)]
    (/ (Math/floor (* p n)) p)))

joshjones16:01:14

call with (precision 3.123456 3) for example

seancorfield17:01:23

@djjolicoeur As a testament to Clojure’s stability in alphas, at World Singles, we’ve taken alpha and beta builds to production with every release dating back to 1.3 alpha 7 or 8 (back in 2011). We’re on 1.9 alpha 14 in production right now.

seancorfield17:01:51

We also always test our code against whatever “release” we plan to use in production and the current master snapshot — so we don’t get surprised by changes coming down the pipe.

djjolicoeur17:01:18

thanks @seancorfield, my only concern here is leaning too hard on the alpha spec API, as we are thinking of replacing schema with spec. I suppose if breaking changes are really only scoped to that narrow domain we might be ok.

seancorfield17:01:07

Like I say, set up your tests to run against the released version and master so you get advanced notice of any breaking changes and you can prepare for them.

seancorfield17:01:45

We’re using spec fairly heavily already and so far, across 14 alphas, we’ve only had to make a few changes.

djjolicoeur17:01:47

excellent, that sounds like a viable option. Thank you, that is very helpful information

geoffs18:01:06

@nooga @joshjones

((juxt #(get % true) #(get % false)) (group-by odd? (range 5)))

geoffs18:01:52

Which is unfortunately much slower than @nooga's original implementation

Alex Miller (Clojure team)18:01:59

@djjolicoeur I don’t have anything official to add at this time re Clojure 1.9/spec. I do hope that we can say more in the near future about plans, which we have been discussing. There is a lot of pending work in the various jira tracking reports and those are the most likely bug/enhancement kinds of things to get in there as that’s what has been worked on. I do not expect there to be major changes in the current API or the spec forms - most changes at this point are likely to be either in implementation or additive.

Alex Miller (Clojure team)18:01:04

one area of the api that we are unhappy with is the consistency with which things like gen overrides, stub/spec/replace options, etc are specified across various parts of the api (exercise, instrument, check, etc) and that’s something that may require a bit of tweaking.

djjolicoeur18:01:11

thanks @alexmiller, really appreciate the info

tcrawley19:01:41

@tvalerio: I'm not sure why the schedule-seq would fire immediately, but for your needs, you don't need it. You can just do (schedule my-job (-> (at "00:30") (every :day)))

tcrawley19:01:07

@tvalerio we may want to move this discussion to #immutant, since it's lib specific

bbloom19:01:27

Dear #clojure, I love you guys. Awful hacker news comments makes me love you guys more.

dpsutton19:01:14

oh is there a good one right now?

dpsutton19:01:22

i've seen a lot of clojure articles on reddit and HN recently

bbloom19:01:56

nah, it’s just people complaining about Go

bbloom19:01:01

for the record, Go and Clojure have a lot in common

bbloom19:01:07

in terms of design principals

bbloom19:01:32

all the Swift and Rust programmers come out of the woodwork to complain about Go, but they just don’t understand why Go is the way it is....

seancorfield19:01:04

I so dearly wanted to like Go. It has some very cool stuff in it. But it has some very weird stuff too (and, last I looked, its ecosystem was still pretty fragile?). I think, deep down, I wanted it to be Rust tho’… and then when Rust came along, I loved it… but found myself in the same love/hate position as I do with Haskell 🙂 So Clojure remains the sweet spot for me.

seancorfield19:01:25

Clojure just makes me happy.

bbloom19:01:15

me as well - but i’ve written a fair amount of very useful software in Go for situtations where clojure just wouldn’t have been appropriate

bbloom19:01:35

and the code i wrote was very similar to the code i’d have written in Clojure, although a bit more verbose

bbloom19:01:22

in go, struct embedding is quite similar to open maps in clojure

bbloom19:01:40

type switch and interfaces are quite similar to multi-methods switched on a single keyword or protocols

bbloom19:01:07

it’s a quirky language, but then again, what language isn’t?

schmee19:01:56

Go isn’t functional at all right?

schmee19:01:17

i.e it’s more loops, less map reduce et. al

bbloom19:01:55

but structs are implicitly copied in most situtations, so it’s quite easy to write “functional” code in that you never mutate your arguments - although in Go there’s some annoying situations where you need to manually clone maps or arrays/slices

bbloom19:01:22

if you have heterogenous maps encoded as structs with “struct embedding”, then argument passing is effectively functional with values

shaunxcode19:01:09

top tip, never depend on the behavior of str on a map if you are evaluating a lazy sequence inside of println which binds print-readable to nil.

schmee19:01:19

the immutable structs sound cool, but I don’t think I can ever go back to loops after doing so much Ruby and Clojure 😛

schmee19:01:56

of course you can accomplish the same things, but it has to be fun to use as well 🙂

bbloom19:01:19

¯\(ツ)/¯ it’s pretty easy to squint and view for/range + if/continue + append as a direct analog to map, filter, and mapcat

bbloom19:01:27

it’s a bit more verbose

bbloom19:01:45

but in the absence of a compiler that does loop fusion, you’re going to get better perf with the imperative stuff

bbloom19:01:05

it’s the same tradeoff as static vs dynamic: you commit to some strategy earlier with loops etc than you do if you use functional operations

bbloom19:01:09

ideally you want to delay such commitment

bbloom19:01:16

but it’s not without its costs

tbaldridge19:01:55

I also have a love-hate thing with Haskell, as in I love to hate it

bbloom19:01:16

lol indeed

bbloom19:01:41

i think haskell is an awesome and valuable research project tho

tbaldridge19:01:43

If Go's interfaces were namespaced, it wouldn't be half as bad

bbloom19:01:03

oh yes. the fact that field names are global is a major problem

bbloom19:01:15

fire the employee, fire the missiles … WHATEVER

tbaldridge19:01:48

@bbloom let's try to keep current US politics out of this, okay?

enimiste21:01:16

hi clojurians !

qqq23:01:44

is there a tutorial on how to use gen/sample on tree like structures in spec?

qqq23:01:49

I'm getting stack overflows

qqq23:01:58

due to the child node generating more child note, and so forth

jr23:01:19

you might want to try #clojure-spec

qqq23:01:45

@jr: good call; moved!

alexbaranosky23:01:42

Has anyone run into core.logic as a performance bottleneck in any of their applications?

alexbaranosky23:01:23

I guess the questions are: - can my usage of core.logic be optimized somehow? (cpu snapshot filled with core/logic/unify, clojure.core/count, hash - if not, are there much faster ways to do a similar type of scenario where you load tuples into memory and then query them?

qqq23:01:48

there are (paid) videos on youtube somewhere explaining odin design/impl too