Don't know what you mean about "swapping", but here's a solution that
generalizes to a tree of any finite depth, where each node is either a leaf
element or a seqable (say, a vector) of any length:
(defn coord-of
([elems target]
(coord-of elems target []))
([elems target indexes]
It might help to simplify. Whenever you're accumulating over a
sequence of things, think of reduce:
(let [__ (fn [ fs]
;; Here's the function:
(reduce #(fn [x] (%1 (%2 x))) fs))
]
;; Testing:
[ (= 5 ((__ (partial + 3) second) [1 2 3 4]))
(= [3 2
I discovered partition-all and came up with this function:
(fn [l x] (concat (butlast l) (partition-all 2 (concat (last l)
[x]
user ((fn [l x] (concat (butlast l) (partition-all 2 (concat (last l)
[x]
'()
10)
((10))
user ((fn [l x] (concat (butlast l) (partition-all 2
, 2:54 pm, Tyler Perkins thinks.outs...@gmail.com wrote:
I discovered partition-all and came up with this function:
(fn [l x] (concat (butlast l) (partition-all 2 (concat (last l)
[x]
user ((fn [l x] (concat (butlast l) (partition-all 2 (concat (last l)
[x]
'()
10)
((10
Here's what I came up with, a pretty functional approach:
(fn [l]
(let [lt(partial apply )
pairs (- (map vector l (rest l))
(partition-by lt)
(filter (comp lt first)))
max-count (apply max 0 (map count pairs))]
(-
Interesting. I never knew how to use areduce before. However, it
always scans the entire array. If you had a very long string (or other
collection), it might be better to scan backwards:
user (defn last-indexof [cs c]
(loop [n (dec (count cs))]
(if (and (= 0 n) (not= c
Just take an idea from Haskell (as usual!). Function 'flip' returns a
function taking its first two arguments in order opposite the given
function:
user (defn flip [f] (fn [a2 a1 more] (apply f a1 a2 more)))
#'user/flip
user (- 10 2 3)
5
user ((flip -) 2 10 3)
5
user ((partial 2000) 1000)
false
Renaming accessor functions has the benefit of help from the compiler,
which will tell me if the function is not defined so I can easily
change the client code to use new name.
I think you could just define vars that evaluate to keywords. Adopting
the convention as follows gives protection
Interesting. This problem is like compression, but the first thing I
thought of was pattern recognition. If your tokens are just characters
in a string, you can let regular expressions do all the heavy lifting:
(defn rep-groups [s] (re-seq #(.+)\1+|. s))
Then (rep-groups .,ababcababc.,cc,.abab.)
How about just
(apply hash-map (split (slurp data) #,))
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first
On Dec 4, 12:21 pm, Tim Robinson tim.blacks...@gmail.com wrote:
Using 'apply hash-map' doesn't handle the transformations.
Note the original post requested keywords for keys and integers for
vals in the output.
I'm not the only one who overlooked that! :o) Might just as well use
strings as
Or, better yet,
(into {}
(- (str { (slurp data) })
read-string
(map (fn [[k v]] [(keyword (str k)) v]
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to
Nice! And with just a bit more, we have a clean, sorting DSL:
(def asc compare)
(def desc #(compare %2 %1))
;; compare-by generates a Comparator:
(defn compare-by [ key-cmp-pairs]
(fn [x y]
(loop [[k cmp more] key-cmp-pairs]
(let [result (cmp (k x) (k y))]
(if
I'm afraid additional verbosity is required. But thanks to your
excellent book (page 229), I know how to import sqrt and floor:
(use '[clojure.contrib.import-static :only (import-static)])
(import-static java.lang.Math sqrt floor)
;;; ... your Clojure code ...
--
You received this message
I like Clojure, but as a point of comparison, here's a Haskell
solution, as typed in the REPL:
Prelude let bOf a = 1000*(500 - a)/(1000 - a)
Prelude let nearInt x = x - fromInteger(truncate x) 0.01
Prelude head [ ( a, b, sqrt(a^2 + b^2) ) | a - [1..], b - [bOf a],
nearInt b ]
15 matches
Mail list logo