Re: Index of an element in a vector of vectors

2016-12-10 Thread Tyler Perkins
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]

Re: problem 58 on 4clojure

2012-08-26 Thread Tyler Perkins
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

Re: struggling with two simple list of lists operations

2012-06-24 Thread Tyler Perkins
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

Re: struggling with two simple list of lists operations

2012-06-24 Thread Tyler Perkins
, 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

Re: so why it has to be so complicated / Longest Increasing Sub-Seq

2012-06-14 Thread Tyler Perkins
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))] (-

Re: Getting index of char from end

2011-11-12 Thread Tyler Perkins
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

Re: partial, but instead of args + additional, get additional + args

2011-10-22 Thread Tyler Perkins
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

Re: (:key map) lookup vs accessor functions in large applications

2011-08-08 Thread Tyler Perkins
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

Re: Pimp my algorithm: finding repeating subsequences

2011-02-19 Thread Tyler Perkins
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.)

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
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

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
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

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
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

Re: sort-by reverse order?

2010-11-23 Thread Tyler Perkins
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

Re: collections within collections

2010-03-11 Thread Tyler Perkins
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

Re: collections within collections

2010-03-10 Thread Tyler Perkins
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 ]