Message Boards Message Boards

0
|
4972 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Can DeleteCases accept an anonymous function as a pattern?

Posted 2 years ago

I suspect this is just a misunderstanding on my part, but I want to check if what I am trying is not possible.

I have a list of lists. I want to delete elements that have a length of less than 20 items. I can get the following to work.

Select[meetingSet, Length[#] >= 20 &]

But I was surprised that

DeleteCases[meetingSet, Length[#] <20 &]  

Just returns the original list.

Is it possible to make the pattern an anonymous function?

Cheers, Andy

POSTED BY: Andrew Burnett
4 Replies
Posted 2 years ago

Andy:

I'm not at a place where I can try anything right now. But, let me give this my best guess.

I think the answer might be yes, but the function would have to return a pattern. Your example returns true/false.

POSTED BY: Mike Besso

Andy,

You need a Pattern in DeleteCases while Select uses a function that returns true/false. An example would be

DeleteCases[list, (x_ /; Length[x] < 20)]

Regards,

Neil

POSTED BY: Neil Singer

You can use a pure function in DeleteCases if it is packaged as a PatternTest:

DeleteCases[{{1, 2}, Range[21]},
 _?(Length[#] < 20 &)]
POSTED BY: Gianluca Gorni
Posted 2 years ago

Thank you very much everyone. I need to go and practice more with pattern matching. I can see it's very powerful, but I haven't properly understood it up to now.

POSTED BY: Andrew Burnett
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract