Timeline for How to generate a list from an iterator and some arbitrary function
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 24, 2013 at 0:30 | comment | added | Noon Silk | rcollyer: Indeed, but I can't do that. Or, put another way, that's equivalent to my original problem :) | |
Mar 23, 2013 at 23:08 | answer | added | Mr.Wizard | timeline score: 3 | |
Mar 23, 2013 at 22:23 | answer | added | Verbeia | timeline score: 1 | |
Mar 23, 2013 at 16:22 | comment | added | rcollyer |
If you can pregenerate the list of integers that will work, then you can use another form of table: Table[...,{i, {list of ints that meet predicate}] .
|
|
Mar 23, 2013 at 13:39 | comment | added | Noon Silk |
J. M: Not really, no. The point is it's basically arbitrary; imagine my predicate just randomly returns True when passed an integer.
|
|
Mar 23, 2013 at 13:36 | comment | added | Sasha |
The best way, of course, if you know the mapping from $\mathbb{N}$ to your set. In the toy example you mentioned: 2 Range[Quotient[len,2]] , or even Range[2,len,2] does the same as Select[Table[i,{i,len}],EvenQ]
|
|
Mar 23, 2013 at 13:35 | comment | added | J. M.'s missing motivation♦ | Can you maybe include you actual problem? As I said, there might be structure in your problem that can allow a different solution... | |
Mar 23, 2013 at 13:34 | comment | added | Noon Silk | Haha, indeed J. M. in my actual case the things I want to find are less straightfoward. | |
Mar 23, 2013 at 13:33 | comment | added | Noon Silk |
Well, at the moment I can't make such a trade. The Select is slow (I believe) because it creates an inappropriately large array. Do with Reap/Sow also seems unfortunately slow. I was hoping there was a more idomatic (and so hopefully fast) way.
|
|
Mar 23, 2013 at 13:32 | comment | added | J. M.'s missing motivation♦ |
Of course, sometimes problems have a pattern/structure that allow you to do these things more cleverly. In this simple case, you could do Table[2 i, {i, 1, 5}] or Table[i, {i, 2, 10, 2}] for instance; so, exploit patterns when you can!
|
|
Mar 23, 2013 at 13:29 | comment | added | J. M.'s missing motivation♦ | It's Scylla and Charybdis, I think: you could have it fast, but at the expense of memory, or memory-conserving, but rather slow... | |
Mar 23, 2013 at 13:27 | comment | added | Noon Silk |
Yeah; that's basically what I had in mind. Interestingly, it doesn't appear to be particularly fast (though at least it probably doesn't waste memory like the Select option would do ...)
|
|
Mar 23, 2013 at 13:13 | comment | added | J. M.'s missing motivation♦ |
You could use Sow[] /Reap[] with Do[] ; e.g. Reap[Do[If[EvenQ[k], Sow[k]], {k, 10}]][[-1, 1]] .
|
|
Mar 23, 2013 at 13:09 | history | asked | Noon Silk | CC BY-SA 3.0 |