Fork me on GitHub
#beginners
<
2017-04-07
>
seancorfield00:04:13

@sb sounds like your URLs should be root-relative (i.e., start with /)

yanglin01:04:40

Hey guys, what would be an idiomatic clojure approach to implement a next or prev function where given a vector and an element, it returns the next/prev element if it exists.

yanglin01:04:46

(next [:a :b :c :d] :a)  ;; => :b
(next [:a :b :c :d] :b)  ;; => :c
(next [:a :b :c :d] :d)  ;; => nil
(next [:a :b :c :d] :foo)  ;; => nil

(prev [:a :b :c :d] :c)  ;; => :b
(prev [:a :b :c :d] :b)  ;; => :a
(prev [:a :b :c :d] :a)  ;; => nil

yanglin01:04:53

Only way I can think of is by finding the index of the element first.

seancorfield01:04:13

(defn next [s k] (k (zipmap s (rest s)))) and (defn prev [s k] (k (zipmap (rest s) s)))

seancorfield01:04:57

boot.user=> (defn prev [s k] (k (zipmap (rest s) s)))
#'boot.user/prev
boot.user=> (defn next [s k] (k (zipmap s (rest s))))
WARNING: next already refers to: #'clojure.core/next in namespace: boot.user, being replaced by: #'boot.user/next
#'boot.user/next
boot.user=> (next [:a :b :c :d] :a)  ;; => :b
:b
boot.user=> (next [:a :b :c :d] :b)  ;; => :c
:c
boot.user=> (next [:a :b :c :d] :d)  ;; => nil
nil
boot.user=> (next [:a :b :c :d] :foo)  ;; => nil
nil
boot.user=> (prev [:a :b :c :d] :c)  ;; => :b
:b
boot.user=> (prev [:a :b :c :d] :b)  ;; => :a
:a
boot.user=> (prev [:a :b :c :d] :a)  ;; => nil
nil
boot.user=> 
^ @yanglin

yanglin10:04:06

seancorfield: Huh, I would have never thought to use zipmap here. Thanks so much!

mbcev01:04:25

I have a question regarding recursion. Say I have something that looks something like (fn [arg1 arg2 col] (loop [element (dec (count col)), col col] (if (>= element 0) (print arg1 arg2) (recur (dec element) (pop col))))) my question is, because loop has 2 arguments, recur expects 2 arguments, but if I'm say printing or otherwise interacting with arg1 and arg2 which are defined in the original function call but not directly in the loop, is my recursive function still using tail-recursion? Or will this overflow my stack if my initial collection is large enough?

seancorfield02:04:59

loop/`recur` compiles to just a simple loop — you’re guaranteed that.

seancorfield02:04:56

The same is true of fn/`recur` and defn/`recur` — the Clojure compiler simply won’t accept recur in a position where it can’t compile it as a loop, i.e., in a non-tail-recursion position.

mbcev02:04:43

oh okay, so I don't necessarily need loop/recur in my function. I didn't realize fn/defn worked with recur

seancorfield02:04:19

Of course, for your function as written, there are many better ways to write that but I’m sure you mean it as just an example, not a real function? 🙂

mbcev02:04:52

No my function doesn't look like that, but it was the best way I could "summarize" my question I guess

seancorfield02:04:20

(for code that is evaluated purely for side-effects — such as printing — doseq or dotimes or even run! are more likely what you want)

seancorfield02:04:33

Also, when you have a collection (or sequence), the idiomatic solution is rarely going to be loop/`recur` (sure, it will be sometimes, especially if you need to “loop” over several unrelated things at once or the “recursion” is conditional and does different things will each of those… but even there reduce might be better).

dimovich05:04:09

ran into this issue

dimovich05:04:37

making a project using webica

sb06:04:12

@seancorfield: thanks, I thought the resources map is every case absolute

vinai07:04:13

I'm doing (is (= expected-long-string (generate-long-string))) in a clojure.test test. The string contains many lines separated by newlines. If the test fails, clojure.test displays them as a single long line comparison failed in the report, the newlines are shown as \n. Is there a function that will show me some kind of diff of the two strings? Someone pointed me to pjstadig.humane-test-output, but unfortunately it is not compatible with cider > 0.10, and I'm using version 0.14. It would be much more easy to spot the difference in the actual result compared to the generated string that way.

noisesmith17:04:02

I know you got an answer already for this, but another simple option is lein difftest which diffs the expected and found data and shows that

jumar07:04:31

@vinai using Expectations test library could help, but I'm not sure if that's an option for you.

jumar07:04:22

The integration story with Cider is not completely smooth, but it should work if you use defexpect macro

vinai07:04:27

@jumar Thanks for the recommendation, I'm investigating one other option, but if that doesn't turn out I'll try Expectations.

sb10:04:06

Hello

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script>var $j = jQuery.noConflict(true);</script>
    <script>
      $(document).ready(function(){
       console.log($().jquery); // This prints v1.4.2
       console.log($j().jquery); // This prints v1.9.1
      });
   </script>
somebody successfully implement this in hiccup? because in my code.. isn’t works.. can’t manage the noConflict() function

sb10:04:17

somebody have experience with that problem?

sb10:04:23

I tried with [:script {…}] and hiccup element javascript-tag "..." too

sb10:04:19

@ilevd thanks! I check it!

sb10:04:19

Ok, I transformed with similar tools too. That isn’t problem.

sb10:04:06

Syntactically correct what I wrote.. just hiccup can’t manage the version conflicts. I will check it in different way.

sveri10:04:44

@sb what do you mean with "hiccup managing version conflicts"?

sb11:04:04

sorry..

sb11:04:30

I translate from html to hiccup language with correct syntact (the original script)

sb11:04:53

but isn’t working.. that kind of jquery conflict management

sb11:04:45

I try to solve with d3.. because maybe that is a jquery bug

Drew Verlee20:04:41

Whats the simplist way to build a clojure program that runs tell i tell it to exit and can take arguments at run time?

Drew Verlee20:04:04

just a while loop and bind it to a port?

Drew Verlee20:04:40

It seems like this should be obvious to me, but its been a while sense i have had to worry about this instead of having something take care of it for me :0

noisesmith20:04:36

@drewverlee loop on read-line ?