2
$\begingroup$

I stumbled upon this code by Leonid Shifrin. He uses Sequence[] to create void or non-existent output. I was wondering whether this is really the best way to generate non-existent output in that way? What are alternatives? I was looking for a function named Void[] or something, but it doesn't exist?

mylist = Range[Range[10]];
oddSublists[x_List] := 
  Map[If[EvenQ[Count[#, _?OddQ]], # /. # -> Sequence[], #] &, x];
oddSublists[mylist]
$\endgroup$
1
  • $\begingroup$ see Nothing $\endgroup$
    – kglr
    Commented Jul 24, 2017 at 21:10

2 Answers 2

2
$\begingroup$

With Version 11 you can use Nothing

Before use Sequence[] or Missing[]

{1, 2, 3} /. (3) -> Sequence[]

{1, 2}

list = {1, 2, 3} /. (3) -> Missing[3]

{1, 2, Missing[3]}

DeleteMissing @ list

{1, 2}

$\endgroup$
3
$\begingroup$

Nothing for version 11. For older versions, there is also ##&[] and Unevaluated@Sequence[]

oddSublists2[x_List] := Map[If[EvenQ[Count[#, _?OddQ]], ## &[], #] &, x];
oddSublists3[x_List] := Map[If[EvenQ[Count[#, _?OddQ]], Unevaluated@Sequence[], #] &, x];

mylist = Range[Range[10]];
oddSublists2[mylist] == oddSublists3[mylist] == oddSublists[mylist]

True

See also:

$\endgroup$
2
  • $\begingroup$ Do you feel that this question is not duplicate of those referenced? $\endgroup$
    – Mr.Wizard
    Commented Jul 25, 2017 at 11:51
  • 1
    $\begingroup$ @Mr.Wizard, looks like it is a duplicate. $\endgroup$
    – kglr
    Commented Jul 25, 2017 at 15:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.