filter

Description

Apply a lambda function to filter out elements of a list.

Copy
 list filter( list input , lambda definition )

Evaluates a lambda for every element in the list, and returns a new list with only the elements that evaluated to true. If you are filtering on a data feed, you should instead use filter_content() to preserve the location of items that were pinned to specific feed locations with Recommendations.

Examples

Include only those items that are greater than 2:

{content = filter([1, 2, 3, 4, 5], lambda x: x>2)} ---> [3, 4, 5]

Filter out pieces of content that are tagged “explicit-content”, including pinned items:

{content = filter(content, lambda x: !contains(x.tags, 'explicit-content'))}

Note that if you are filtering on a data feed, filter_content() works the same way as filter(), but preserves pinning locations. See filter_content() for additional feed filtering examples.


Filter Out Content with Empty ‘Image’ URL Value

Use Case: You discover that some of your content has an image field where the value is an empty string instead of an image URL. Since Sailthru recognizes the presence of the sailthru.image tag, it won’t be filtered out at the feed level. Thankfully, you can use Zephyr to filter out any content that doesn’t have an actual value for the image.

Zephyr: In the Setup:

{content = filter(content, lambda c: length(c.image) > 0)}

In the example Media feed, the “Spider-Man: Threat or Menace?” article would be thus excluded as its “sailthru.image” parameter is an empty string.

Explanation: This script uses a lambda, which creates an anonymous function, in conjunction with the length() function to check the “image” field of each item in a content array to see if the length is greater than 1, or in other words, if there’s a value. If there isn’t, the filter() function will remove it from the “content” object. This is beneficial if there’s a chance that you might spider an empty string for the sailthru.image parameter.

Cancel if There’s Not Enough Content to Show a User

Use Case: Cancel if there’s not enough content to show a user

{content = filter(content, lambda c: c.vars.sailthru_vertical && c.vars.sailthru_topic == 
profile.vars.favorite_topic)}

{cancel(length(content) < 1, "No content in the user's favorite topic!")}


Explanation: This script is using the filter function to only include any content that has a topic variable that equals a “favorite_topic” variable on the user’s profile. It then checks how many items are left using the length() function. If there’s less than 1 (i.e. no items), then the cancel() function stops the email from going out to that specific user.