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$

marked as duplicate by Mr.Wizard Jul 25 '17 at 16:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ see Nothing $\endgroup$ – kglr Jul 24 '17 at 21:10
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$
  • $\begingroup$ Do you feel that this question is not duplicate of those referenced? $\endgroup$ – Mr.Wizard Jul 25 '17 at 11:51
  • 1
    $\begingroup$ @Mr.Wizard, looks like it is a duplicate. $\endgroup$ – kglr Jul 25 '17 at 15:24

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