(map (fn [x] + x (rand-int 10)) (range 4)
> (apply concat)
> (take 1))
> New seq...
> => (3)
> ```
>
> Lazy-flatten is unnecessary because concat already does what you want –
> the generalised problem you're running in to is that function argument
```
(->> (repeatedly
(fn []
(lazy-seq
(println "New seq...")
(map (fn [x] + x (rand-int 10)) (range 4)
(apply concat)
(take 1))
New seq...
=> (3)
```
Lazy-flatten is unnecessary because concat already does what you want – the
at-seq [0 1 2 3 4 10 11 12 13 14 20 21 22 23 24]))
>
>
> Alan
>
>
> On Tue, Nov 21, 2017 at 4:47 PM, Jason Wolfe > wrote:
>
>> I think this
>> <https://github.com/plumatic/plumbing/blob/318af7798bb701607aaae8639d12829014941184/src/plumbing/core.cl
:
> I think this
> <https://github.com/plumatic/plumbing/blob/318af7798bb701607aaae8639d12829014941184/src/plumbing/core.cljx#L182>
> will do it:
>
> (lazy-cat (first coll) (when-let [n (next coll)] (lazy-flatten n
>
>
> On Tuesday, November 21, 2017 at 2:34:15 PM
I think this
<https://github.com/plumatic/plumbing/blob/318af7798bb701607aaae8639d12829014941184/src/plumbing/core.cljx#L182>
will do it:
(lazy-cat (first coll) (when-let [n (next coll)] (lazy-flatten n
On Tuesday, November 21, 2017 at 2:34:15 PM UTC-8, Matt Anderson wrote:
>
&
" 2 "top level" seqs at a time and I'm
afraid that may be the best I get, but I thought it might be an exercise
worth presenting in case anyone had ideas. Here's a contrived example:
(defn lazy-flatten
[coll]
(when-let [s (seq coll)]
(lazy-seq
(if (seq? (
Yes, reading the source code and trying to understand why rest was being
called there is how I came up with this case.
On 2 July 2015 at 07:54, icamts wrote:
> Hi Pablo,
> I think you're right. Have a look at flatten source
>
> (defn flatten
> "Takes any nested
Hi Pablo,
I think you're right. Have a look at flatten source
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns an empty sequence."
{:added "1.2&qu
C+1, J. Pablo Fernández wrote:
>>
>> Hello Clojurists,
>>
>> Today I was surprised by the result of (flatten 1) which is '(). I was
>> expecting '(1) or an error. Talking in some other people in #clojure @
>> clojurians.net, not everybody agrees that '(1) i
On Wednesday, 1 July 2015 12:55:28 UTC+1, J. Pablo Fernández wrote:
>
> Hello Clojurists,
>
> Today I was surprised by the result of (flatten 1) which is '(). I was
> expecting '(1) or an error. Talking in some other people in #clojure @
> clojurians.net, not everybod
Hello Clojurists,
Today I was surprised by the result of (flatten 1) which is '(). I was
expecting '(1) or an error. Talking in some other people in #clojure @
clojurians.net, not everybody agrees that '(1) is a good result but that
'() is somewhat surprising. Would it be
Thanks - useful idioms to know about!
On Friday, 18 July 2014 16:18:33 UTC+9:30, puzzler wrote:
>
> Yeah, you've answered your own question. In practice, I doubt the
> difference is measurable.
>
> Another common idiom you see in Clojure code is:
> (defn f [xs]
> (if-let [s (seq xs)]
> ...
Yeah, you've answered your own question. In practice, I doubt the
difference is measurable.
Another common idiom you see in Clojure code is:
(defn f [xs]
(if-let [s (seq xs)]
...do something with (first s) and (f (rest s))...
...base case...))
This ensures that you seq-ify the input (r
I *think* I've found the answer to my own question...
In this post...
https://groups.google.com/forum/#!topic/clojure/Cuk_bJrIq-Y
I found this link (I changed the line number)...
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L2589
And if the implementat
Thanks again - that all makes sense.
One (hopefully) tiny question... an efficiency one... (and feel free not to
answer it if I've already taken up enough of your time)
If you do that and are careful, the performance of next/nil? is slightly
> better than rest/empty?.
>
If I use the next/nil?
On Thu, Jul 17, 2014 at 7:54 PM, Mark P wrote:
>
> I notice in your lazy version (below), you preface the last cons with a
> lazy-seq call, but do not do the same with the other cons calls (within the
> first recur form). I know it was only the last line that previously had a
> conj call and so
verflow in the realization of that lazy flattened list:
>
Why does going lazy avoid a stack overflow risk?
Again, to answer my own question :-) ... The resultant flat list from
my-flatten will be realized one element at a time. The call stack will be
used during this process, but lazy-seq pr
Woopse, typo towards the end of my last post...
Maybe the thing to do would be to use (next x) here for this
> implementation, which is angling to be lazy... But in your earlier
> acc-based my-flatten, to use (next x) instead
>
Should be... Maybe the thing to do would be to us
e last line is to defer the
recursive call to my-flatten until something wants to use it. In contrast,
the other cons-involving line only conses up things which are relatively
harmless to evaluate.
2. The "cons (first x) ..." will be "undone" almost immediately after
t
> http://clojure.org/reducers
i dare say the "When to use" part should not be at the bottom but come
right after the otherwise laughably specious "yielding code that will
get faster automatically as machines get more cores".
--
You received this message because you are subscribed to the Google
G
Slightly off-topic from original poster's question, but if you're interested in
another implementation of flatten, I suggest you take a look at the reducers
library. clojure.core/flatten is elegant but a bit slow. The reducers version
is very fast as part of a reduce/fold opera
he input as
> you go to achieve the desired result.
>
> I've come up with another version, that passes through both a "result so
> far" accumulator and a "still to be computed" stack. It is very much
> non-lazy, but that's okay in some applicatio
through both a "result so
far" accumulator and a "still to be computed" stack. It is very much
non-lazy, but that's okay in some applications (a performance feature
even!). Here it is...
(defn my-accandstackbased-flatten
[xs]
(letfn [(flatten-accandstack [xs acc
It's worth understanding how to go back and forth between accumulator-style
and a lazy construction. You can convert the above non-lazy accumulator
version into a similar version that is lazy but has no risk of stack
overflow in the realization of that lazy flattened list:
(defn my-flatten
To avoid the doall-concat, you'd just have the base case return [] (btw,
you don't need the apostrophe for vectors or for the empty list), and use
`into` rather than `concat`.
If you're looking for something that exploits the structure of flatten and
uses an accumulator, you could
. It is better to use a strict
version of the something in the first place. Does Clojure provide strict
versions of things like concat, or would I need to roll-my-own?
Thinking again of efficiency... I had a go at doing an
accumulator-passing-style (APS) version of flatten. Here it is...
(defn
nd as part of the
> learning process I've done a CPS version of a "flatten" function. Ie, it
> does the same thing as the standard clojure "flatten", but is implemented
> using a recursive local CPS helper function. I'm interested in comments /
> crit
Yes, here's the trampolined version which won't stack overflow:
(defn my-flatten
[xs]
(letfn [(my-flatten-cps [xs k]
(if (nil? xs)
(k '())
(let [x (first xs), ys (next xs)]
(if (sequential? x)
(recur
I'm very new to continuation passing style (CPS), and as part of the
learning process I've done a CPS version of a "flatten" function. Ie, it
does the same thing as the standard clojure "flatten", but is implemented
using a recursive local CPS helper functio
Thanks for the elaborate answer!
It also clears some other doubts I had regarding the core functions.
On Wednesday, August 29, 2012 11:34:56 PM UTC+2, miner wrote:
>
> flatten has been discussed before:
>
> https://groups.google.com/forum/?fromgroups=#!topic/clojure/ye70iNJ73zc
>
flatten has been discussed before:
https://groups.google.com/forum/?fromgroups=#!topic/clojure/ye70iNJ73zc
See also CLJ-400, but it looks like no patch was submitted.
http://dev.clojure.org/jira/browse/CLJ-400
I think the general policy for Clojure is that the core functions of course
should
Calling flatten on anything that is not 'sequential?' returns an empty
sequence:
(flatten 1); => ()
(flatten "Hi"); => ()
With sets if feels somewhat strange:
(flatten #{#{:a} #{:b :c}}); => ()
For some reason I expected #{#{:a} #{:b :c}} to equal #{:a :b
On Tue, Oct 4, 2011 at 2:25 PM, Shoeb Bhinderwala
wrote:
> Thanks. Didn't think it would exist in clojure.core.
I highly recommend trying out Chas Emerick's Clojure Atlas:
http://clojureatlas.com - it makes searching for functions AND
concepts really easy and provides a great way to see the relat
ot; "s8"]) "s9"])]))
> #'sandbox/s1
> sandbox> s1
> ("s1" ("s2" "s3") "s4" "s5" ("s6" ("s7" "s8") "s9"))
> sandbox> (flatten s1)
> ("s1" "s2"
your subject contains the answer :)
sandbox> (def s1 (seq ["s1" (seq ["s2" "s3"]) "s4" "s5" (seq ["s6"
(seq ["s7" "s8"]) "s9"])]))
#'sandbox/s1
sandbox> s1
("s1" ("s2" &q
user=> (flatten s1)
("s1" "s2" "s3" "s4" "s5" "s6" "s7" "s8" "s9")
Sincerely,
Michał
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To po
> user => s1
> ("s1" ("s2" "s3") "s4" "s5" ("s6" ("s7" "s8") "s9"))
>
> How to convert s1 to a flat sequence like this:
>
> ("s1" "s2" "s3" "s4" &q
(def s1
(seq
["s1"
(seq ["s2" "s3"]) "s4" "s5" (seq ["s6" (seq ["s7" "s8"])
"s9"])]))
user => s1
("s1" ("s2" "s3") "s4" "s5" ("s6" ("s7" "s8") "s9"))
How to convert s1 to a flat sequence like this:
("s1" "s2" "s3" "s4" "s5" "s6" "s7" "s8" "s9")
--
You received this message because
ChrisR writes:
> Hi there, possibly the flatten documentation is wrong as (flatten nil)
> for me is returning the empty list rather than nil. (1.3.0).
Indeed, that's because it uses `filter' which produces a lazy seq. Most
probably, in this case it's just the docstring
Hi there, possibly the flatten documentation is wrong as (flatten nil)
for me is returning
the empty list rather than nil. (1.3.0). Is there a better place to
post this?
(clojure.core/flatten nil)
=> ()
from docstring:
"Takes any nested combination of sequential things (lists, vect
You're right about rest, I didn't see that rest should be preferred to
next when consuming seqs in this style.
I agree that flatten returning the empty list for some of the things
that actually can be "seqed" is odd. I'm sure there's a good reason
somewhere, but it
On Jul 14, 2010, at 2:40 PM, Cam wrote:
> I definitely like this version a little better. If you change the else
> of the if to be just (list), it returns the empty list just as core/
> flatten does. Mind if I update the ticket with this patch?
It's all yours. Really, just a slig
I definitely like this version a little better. If you change the else
of the if to be just (list), it returns the empty list just as core/
flatten does. Mind if I update the ticket with this patch?
On Jul 14, 1:56 pm, miner wrote:
> I think it's worthwhile to have a faster flatten ev
I think it's worthwhile to have a faster flatten even if it doesn't
look as elegant as the current implementation. You could do a bit of
refactoring and save yourself a call to sequential? since the
recursive calls are guaranteed to have seqs (as a result of next).
Also, I'd pr
OK, all submitted. The tickets are up for discussion at
http://www.assembla.com/spaces/clojure/support/tickets/400-a-faster-flatten
http://www.assembla.com/spaces/clojure/support/tickets/401-promote--seqable---from-contrib-
I will mail my CA in tomorrow morning.
Thanks Stu and Mark!
On Jul 13
Hi Cam,
Please submit the modified version, and, if you want, create a separate ticket
for "seqable?". I would like to review the latter separately.
Stu
> Hi again, I modified my-flatten to return the empty list for sets and
> maps as core/flatten does. It doesn't seem to
Hi again, I modified my-flatten to return the empty list for sets and
maps as core/flatten does. It doesn't seem to handle Arrays anymore
though. I'm assuming it's because ArrayList and (int-array ...) don't
implement Sequential. None the less should I still submit this
m
Hi Cam,
>>
>> Your tests aren't testing the interesting part without a doall.
>>
>> That said, my quick tests with doall show your approach faring even better.
>> :-) Also, I think what my-flatten does with Java arrays is intuitive (and
>> the curren
he interesting part without a doall.
>
> That said, my quick tests with doall show your approach faring even better.
> :-) Also, I think what my-flatten does with Java arrays is intuitive (and the
> current flatten not so much).
>
> A patch that preserves the semantics of
Hi Cam,
Your tests aren't testing the interesting part without a doall.
That said, my quick tests with doall show your approach faring even better. :-)
Also, I think what my-flatten does with Java arrays is intuitive (and the
current flatten not so much).
A patch that preserves the sema
Unless you wrap a "doall" around the calls to flatten and my-flatten,
you're not really timing anything relevant.
--
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
Another flatten thread! Sorry..
Hello all, before I realized there was a flatten in the master branch
(and before I looked at contrib) I wrote this pretty standard code:
(defn my-flatten [coll]
(lazy-seq
(when-let [coll (seq coll)]
(let [x (first coll)]
(if (sequential? x
On Sep 3, 1:13 am, Sudish Joseph wrote:
> (defn flatten-2 [lst]
> (lazy-seq
> (if-let [x (first lst)]
> (let [xs (rest lst)]
> (if (seq? x)
> (concat (flatten-2 x) (flatten-2 xs))
> (cons x (flatten-2 xs)))
This version is brok
On Sep 3, 1:13 am, Sudish Joseph wrote:
> The other solutions seem higher level, but it's worth noting that
> destructuring -- (let [[x & xs] lst] ...) -- uses next and is therefore
> not fully lazy in that you will peek ahead by one into the lazy
> sequence, so to speak. You have to use explic
;; with destructuring
(defn flatten [lst]
(lazy-seq
(if (empty? lst) lst
(let [[x & xs] lst]
(if (list? x)
(concat (flatten x) (flatten xs))
(cons x (flatten xs)))
;; explicit first / rest
(defn flatten-2 [lst]
(lazy-seq
(if-let
Hello,
At some point I needed at "flatten" function taking a list of atomic
elements or nested lists, and producting a "flat" list of only atomic
elements (in the same order). It should be lazy.
This is what I came up with. Can anyone see a more elegant solution (I
feel I a
It's in c.c.seq-utils
You can look up stuff here:
http://richhickey.github.com/clojure-contrib/api-index.html
On Sep 2, 4:12 pm, tmountain wrote:
> I believe the flatten in contrib is defined as follows. I can't
> remember which module I found it in.
>
> (defn flatten
I believe the flatten in contrib is defined as follows. I can't
remember which module I found it in.
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns nil."
My thought we to use the test cases as a specification for the desired
behavior.
1. Assume that the following case is desired behavior
[:a 1 2 :b 3] [[:a [1 2]] [:b 3]]
My thought was that "If it's a seq, flatten it". That lead me to
develop the test case above. Here
Hi,
Am 08.08.2009 um 07:36 schrieb Sean Devlin:
In my opinion, flatten to behave more like this:
http://gist.github.com/164291
May I ask a stupid question?
What is the use of this case:
[:a 1 2 :b 3] {:a [1 2] :b 3}
Wouldn't it be more useful to flatten
only depending o
See Assembla ticket 13 in for clojure contrib to view the diff
containing the test cases
I just finished writing tests for the following functions in seq-
utils:
flatten
separate
includes?
indexed
group-by
partition-by
frequencies
reductions
rotations
partition-all
shuffle (invariants)
rand-elt
I wrote these and thought they might be useful. Feel free to add them
to clojure.contrib.seq-utils
(defn flatten-n [n coll]
"Like flatten, but only goes n levels deep."
(if (= n 0)
coll
(recur (dec n) (apply concat (map #(if (sequential? %) % (list %))
coll)
(defn-
2009/4/9 Chouser :
> (defn flat1 [coll]
> (mapcat #(if (coll? %) % [%]) coll))
Ah, I see. So for each item, if its already a collection we leave it
alone and if not we make a vector of one item, then at the end we use
mapcat to concatinate all the top-level items into one list.
Excellent - tha
On Wed, Apr 8, 2009 at 10:10 PM, Paul Drummond wrote:
>
> I am looking for something similar to flatten (in contrib.seq-utils)
> but the function will only flatten one level deep:
>
> [ 1 2 3 [4 5 [6 7] ] ] ---> [ 1 2 3 4 5 [6 7] ]
>
> I have tried combining functions in
I am looking for something similar to flatten (in contrib.seq-utils)
but the function will only flatten one level deep:
[ 1 2 3 [4 5 [6 7] ] ] ---> [ 1 2 3 4 5 [6 7] ]
I have tried combining functions in the seq library and I've studied
the code for flatten and tree-seq to look for hint
I always end up doing (filter identity '(:fred :mary nil :sue))
(remove nil? ...) is actually more clear. I'll try to remember that.
On Tue, Feb 24, 2009 at 10:42 PM, Timothy Pratley
wrote:
>
> user=> (remove nil? '(:a nil nil :b :a))
> (:a :b :a)
>
> On Feb 25, 2:38 pm, Sean wrote:
> > I've go
On Feb 24, 11:35 pm, David Sletten wrote:
> On Feb 24, 2009, at 6:07 PM, David Sletten wrote:
>
>
>
> > (defn kill-nil
> > ([l] (kill-nil l '()))
> > ([l result] (cond (nil? l) (reverse result)
> > (nil? (first l)) (recur (rest l) result)
> > true
On Feb 24, 2009, at 6:07 PM, David Sletten wrote:
>
> (defn kill-nil
>([l] (kill-nil l '()))
>([l result] (cond (nil? l) (reverse result)
> (nil? (first l)) (recur (rest l) result)
> true (recur (rest l) (cons (first l) result )
>
>
I forgot
On Feb 24, 2009, at 5:42 PM, Timothy Pratley wrote:
>
> user=> (remove nil? '(:a nil nil :b :a))
> (:a :b :a)
>
>
C'mon! Doesn't anybody do things the old-fashioned way anymore?
(defn kill-nil
([l] (kill-nil l '()))
([l result] (cond (nil? l) (reverse result)
(nil? (
Awesome! Thanks guys!
On Feb 24, 10:42 pm, Timothy Pratley wrote:
> user=> (remove nil? '(:a nil nil :b :a))
> (:a :b :a)
>
> On Feb 25, 2:38 pm, Sean wrote:
>
> > I've got the following list
>
> > (:a nil nil :b :a)
>
> > I want to call a "nil-killer" function, and get the following list
>
>
user=> (remove nil? '(:a nil nil :b :a))
(:a :b :a)
On Feb 25, 2:38 pm, Sean wrote:
> I've got the following list
>
> (:a nil nil :b :a)
>
> I want to call a "nil-killer" function, and get the following list
>
> (:a :b :a)
>
> How do I go about this? Could someone post a quick example?
--~--~--
filter, http://clojure.org/api#filter
On Tue, Feb 24, 2009 at 7:38 PM, Sean wrote:
>
> I've got the following list
>
> (:a nil nil :b :a)
>
> I want to call a "nil-killer" function, and get the following list
>
> (:a :b :a)
>
> How do I go about this? Could someone post a quick example?
> >
>
I've got the following list
(:a nil nil :b :a)
I want to call a "nil-killer" function, and get the following list
(:a :b :a)
How do I go about this? Could someone post a quick example?
--~--~-~--~~~---~--~~
You received this message because you are subscribed t
Now that we have "remove" (and "sequential?"), perhaps we should
redefine flatten and separate:
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence."
[x]
(remove se
74 matches
Mail list logo