Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-12 Thread Ant
Thanks for the replies guys - has given me things to mull over.

On Thursday, 10 May 2012 22:11:18 UTC+1, Ant wrote:

 Hi all, 

 I am battering my head against the following problem which I'm sure is 
 straightforward if only I knew how. I want to partition the following 
 list: 

 '(aa123 x y z bb123 ccq23 3 yg) 

 into the following: 

 ((aa123 x y z) (bb123) (ccq23 3 yg)) 

 The predicate is: 

 #(re-matches #^(\w)\1.* %) 

 partition-by doesn't work, since it splits the sequence when the 
 result of applying the predicate changes. I want to partition when the 
 predicate becomes a particular value. 

 Any clues on how to accomplish this would be gratefully received! 

 Thanks, 

 Anthony.

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-11 Thread Stephen Olsen
I think you'd just have to do it manually with a reduce

Something like this should work.

(defn foldfn [i n]
  (let [result (first i)
current (second i)]
(if (re-matches #^(\w)\1.* n)
  (if (= [] current)
[result [n]]
[(conj result current) [n]])
  [result (conj current n)])))

(defn partition-lst
  [lst]
  (let [reduced (reduce foldfn [[][]] lst)
result (first reduced)
current (second reduced)]
(if (= current [])
  result
  (conj result current



-- 
Stephen Olsen
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


On Thursday, May 10, 2012 at 4:11 PM, Ant wrote:

 Hi all,
 
 I am battering my head against the following problem which I'm sure is
 straightforward if only I knew how. I want to partition the following
 list:
 
 '(aa123 x y z bb123 ccq23 3 yg)
 
 into the following:
 
 ((aa123 x y z) (bb123) (ccq23 3 yg))
 
 The predicate is:
 
 #(re-matches #^(\w)\1.* %)
 
 partition-by doesn't work, since it splits the sequence when the
 result of applying the predicate changes. I want to partition when the
 predicate becomes a particular value.
 
 Any clues on how to accomplish this would be gratefully received!
 
 Thanks,
 
 Anthony.
 
 -- 
 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 
 (mailto:clojure@googlegroups.com)
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com 
 (mailto:clojure+unsubscr...@googlegroups.com)
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 
 


-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Ant
Hi all,

I am battering my head against the following problem which I'm sure is
straightforward if only I knew how. I want to partition the following
list:

'(aa123 x y z bb123 ccq23 3 yg)

into the following:

((aa123 x y z) (bb123) (ccq23 3 yg))

The predicate is:

#(re-matches #^(\w)\1.* %)

partition-by doesn't work, since it splits the sequence when the
result of applying the predicate changes. I want to partition when the
predicate becomes a particular value.

Any clues on how to accomplish this would be gratefully received!

Thanks,

Anthony.

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Ant
After posting, I had the idea of checking out the source for partition-
by, and solved the problem:

(defn partition-when
  Applies f to each value in coll, splitting it each time f returns
   the specified value.  Returns a lazy seq of partitions.
  [f value coll]
  (lazy-seq
(when-let [s (seq coll)]
  (let [run (cons (first s) (take-while #(not= value (f %)) (next
s)))]
   (cons run (partition-when f value (seq (drop (count run) s
  )
)
  )
)

So, I can see basically how this function works, but am not sure what
the when-let gives you. Could someone explain? The documentation makes
no sense to me at the moment:


(when-let bindings  body)

bindings = binding-form test

When test is true, evaluates body with binding-form bound to the value
of test


What test?

Thanks,

Anthony.

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Jack Moffitt
    (when-let [s (seq coll)]
      (let [run (cons (first s) (take-while #(not= value (f %)) (next
 s)))]
       (cons run (partition-when f value (seq (drop (count run) s
      )
    )
  )
 )
...
 What test?

(seq coll) is the test here. when-let (and if-let, etc) bind only when
the right hand side is truthy.

(when-let [x true]
  (println this prints))

(when-let [x false]
  (println this does not print))

It's a common shortcut for:

(let [x (something-something)]
  (when x
 ))

jack.

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Softaddicts
(seq coll) is the test, if the collection is empty seq returns nil, hence 
nothing gets done
in the (let ...) form.

Luc


 After posting, I had the idea of checking out the source for partition-
 by, and solved the problem:
 
 (defn partition-when
   Applies f to each value in coll, splitting it each time f returns
the specified value.  Returns a lazy seq of partitions.
   [f value coll]
   (lazy-seq
 (when-let [s (seq coll)]
   (let [run (cons (first s) (take-while #(not= value (f %)) (next
 s)))]
(cons run (partition-when f value (seq (drop (count run) s
   )
 )
   )
 )
 
 So, I can see basically how this function works, but am not sure what
 the when-let gives you. Could someone explain? The documentation makes
 no sense to me at the moment:
 
 
 (when-let bindings  body)
 
 bindings = binding-form test
 
 When test is true, evaluates body with binding-form bound to the value
 of test
 
 
 What test?
 
 Thanks,
 
 Anthony.
 
 -- 
 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 post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 
--
Softaddictslprefonta...@softaddicts.ca sent by ibisMail from my ipad!

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Alan Malloy
https://github.com/flatland/useful/blob/develop/src/useful/seq.clj#L189
is a more general version of partition-by, which can easily handle
your requirements.

On May 10, 2:11 pm, Ant ant...@gmail.com wrote:
 Hi all,

 I am battering my head against the following problem which I'm sure is
 straightforward if only I knew how. I want to partition the following
 list:

 '(aa123 x y z bb123 ccq23 3 yg)

 into the following:

 ((aa123 x y z) (bb123) (ccq23 3 yg))

 The predicate is:

 #(re-matches #^(\w)\1.* %)

 partition-by doesn't work, since it splits the sequence when the
 result of applying the predicate changes. I want to partition when the
 predicate becomes a particular value.

 Any clues on how to accomplish this would be gratefully received!

 Thanks,

 Anthony.

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Partitioning a list when the result of a predicate becomes a certain value.

2012-05-10 Thread Alan Malloy
On May 10, 3:19 pm, Jack Moffitt j...@metajack.im wrote:
     (when-let [s (seq coll)]
       (let [run (cons (first s) (take-while #(not= value (f %)) (next
  s)))]
        (cons run (partition-when f value (seq (drop (count run) s
       )
     )
   )
  )
 ...
  What test?

 (seq coll) is the test here. when-let (and if-let, etc) bind only when
 the right hand side is truthy.

 (when-let [x true]
   (println this prints))

 (when-let [x false]
   (println this does not print))

 It's a common shortcut for:

 (let [x (something-something)]
   (when x
      ))


Not quite true in the general case, though. Consider

(let [x 1]
  (if-let [x foo]
x   ;; should be foo
x)) ;; should be 1

-- 
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 post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en