2

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]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
GambitSquared
  • 2,311
  • 15
  • 23

2 Answers2

3

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:

kglr
  • 394,356
  • 18
  • 477
  • 896
2

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}

eldo
  • 67,911
  • 5
  • 60
  • 168