Avoiding 'null' output in lists

166 views
Skip to first unread message

Fulvio Gesmundo

unread,
Sep 11, 2017, 7:02:33 AM9/11/17
to Macaulay2
Hi,

I often have the following problem (not really a problem, just a slight annoyance). Just as a silly example, consider the line

apply( 10, i -> ( if i%2 == 0 then i))

that outputs {0, , 2 , , 4 , , 6 , , 8 , }, where the gaps are there because when the if statement is not satisfied a "null" gets spit out.

Now, I have a way to remove all the "null" by doing toList(set ( nameofthelist) - {null} )) but I feel that it is not the right way to do it; also, sometimes the order of the elements in the list is important, and using "set" messes it up.

So my question is: is there a way to make a list like that, but in a way that the output is directly {2,4,6,8}?

Thanks
Fulvio

Greg

unread,
Sep 11, 2017, 8:08:12 AM9/11/17
to Macaulay2
Dear Fulvio,

To achieve your goal, I would combine a "for" loop with the "continue" function:

i1 : for i to 10 list if i % 2 == 0 then i else continue

o1 = {0, 2, 4, 6, 8, 10}

o1 : List

Alternatively, you could use the "select" command:

i2 : select(10, i -> i % 2 == 0)

o2 = {0, 2, 4, 6, 8}

o2 : List

Greg

Fulvio Gesmundo

unread,
Sep 11, 2017, 2:30:47 PM9/11/17
to Macaulay2
This is very helpful. I like both the options. Thank you
Message has been deleted

Luke Oeding

unread,
May 30, 2018, 4:40:01 PM5/30/18
to Macaulay2
Dear Greg,

Is there a way to do this using the apply command? I run into an error:

i1 : : toList apply(1..6, i-> if i == 1 then 1 else continue )

stdio:1:1:(3): error: syntax error at ':'


i1 : stdio:266:57:(3): error: unhandled continue command

stdio:1:37:(3): error: unhandled continue command, with argument


On Monday, September 11, 2017 at 2:08:12 PM UTC+2, Greg wrote:

Greg

unread,
May 30, 2018, 5:25:52 PM5/30/18
to Macaulay2
No.  As I understand it, the BasicList returned by the apply method always has the same number of elements as the input one.
The for loop is much more flexible.

Luke Oeding

unread,
May 30, 2018, 5:27:37 PM5/30/18
to Macaulay2
Ok, thanks!
Reply all
Reply to author
Forward
0 new messages