Fork me on GitHub
Matej Šarlija19:02:30

Having a real fun time with into vs merge gotchas :)

Nathan Rogers23:02:23

I am looking for information regarding data driven boolean expressions. For instance the following expression could be written several ways

(if (and (> a b)
         (< c d)
         (> d e)
         (> a d)) 
(cond (> a b) -1
      (< c d) -1
      (> d e) -1
      (> a d) -1
      :else default)
Or we could notice that swapping the ordering yields 1 function
(if (every? (partial apply >) 
            [[a b]
             [d c]
             [d e]
             [a d]]))
Or if you're trying to be clever
(if (every? true? (map > [a d d a] [b c e d]))
Are there more idiomatic ways to apply functions to data? Or to represent your flow control using data? Or is using data to drive control flow not encouraged favoring more literal expressions? Also are there any simple, idiomatic ways to convert a boolean to its integer equivalent (i.e. 1 for true, 0 for false)?

Nathan Rogers23:02:35

Is there an equivalent to repeatedly which takes an initial arg, and passes previous results to the same function?


Like iterate?

Nathan Rogers23:02:23

(repeatedly 5 #(+ 2 %) 0) which would yield (0 2 4 6 8)


(take 5 (iterate #(+ % 2) 0)) -> (0 2 4 6 8)

Nathan Rogers23:02:41

Ah, I made a mistake, it would yield 8 only

Nathan Rogers23:02:47

so I guess I could call last on that

Nathan Rogers23:02:01

but if its a sufficiently large list, that'd be poor performance, no?


yes a seq is a poor datastructure to just get the last item


If your computation for an item is dependent on previous calculations, you don't have much of a choice.

Nathan Rogers23:02:03

it is only dependent up on the immediately previous calculation


Still, that creates a dependency chain all the back to x_1


there are lots of primitives that would work for this. you could do something like

(letfn [(go [f x n]
            (if (pos? n)
              (recur f (f x) (dec n))
  (go #(+ % 2) 0 1000))

Nathan Rogers23:02:38

I suppose I could write a recur wrapper which simply returns the final value when n is reached

Nathan Rogers23:02:51

oh what is go?


a function i defined


which invokes f and recurs decrementing a counter

Nathan Rogers23:02:34

Ah, should have seen that before asking... I do see a go in clojure.core


there's a go in clojure.core.async but not in clojure.core

Nathan Rogers23:02:21

oh, are the .'s not nested?


no. namespaces have names, not a hierarchy