map
Applies a lambda function to every element in a list.
list map( list input , lambda definition )
Evaluate a lambda for each element of input
, transforming the elements in the list.
Examples
{map([1, 2, 3, 4, 5], lambda x: x*2)} ---> [2, 4, 6, 8, 10]
You cannot modify the length of the input array within the lambda. For example, the following code would result in an error:
{numbers = [1,2,3]} {map(numbers, lambda n: push('numbers', n))}
Give a Higher Weight to Highly Stocked Items
Use Case: You’re using Sailthru’s inventory tracking to manage the amount of inventory on an item, and you want to artificially adjust the weight of an item (i.e. give it higher priority if there’s a lot of stock left). Used in conjunction with the filter() function and assignments. In the example below, the weight of every item with 1,000 or more pieces of stock will have its weight multiplied by 10, thus making them more likely to be recommended Show/hide sample e-commerce feed used for this example
{ "feed": { "name": "Ecommerce Feed", "url": "http://feed.sailthru.com/ws/feed?id=ecomm" }, "content": [ { "title": "To Kill a Mockingbird", "description":"The unforgettable novel of a childhood in a sleepy Southern town and the crisis of conscience that rocked it.", "date": 1489437759, "image": "http://example.com/fiction/tokillamockingbird-full.jpg", "weight": 75, "tags": [ "site-store", "genre-mystery-thriller", "author-harper-lee", "classics", "fiction", "price-11-20" ], "author": "Harper Lee", "price": 1500, "inventory": 200, "vars": { "sailthru_genre":"mysteries-and-thrillers", "sailthru_category":"books", "membership_price":"12.99" }, "url": "http://example.com/fiction/tokillamockingbird/?utm_medium=site", "images": { "full": { "url": "http://example.com/fiction/tokillamockingbird-full.jpg" }, "thumb": { "url": "http://example.com/fiction/tokillamockingbird-thumb.jpg" } } }, { "title": "Salem's Lot", "description":"Ben Mears has returned to Jerusalem's Lot -- but things are more sinister than he remembers.", "date": 1474547794, "image": "http://example.com/books/fiction/salemslot-full.jpg", "weight": 30, "tags": [ "site-store", "genre-horror", "author-stephen-king", "vampires", "fiction", "price-5-10" ], "author": "Stephen King", "price": 999, "inventory": 1567, "vars": { "sailthru_genre":"horror-and-suspense", "sailthru_category":"books", "membership_price":"8.15" }, "url": "http://example.com/fiction/salemslot/", "images": { "full": { "url": "http://example.com/books/fiction/salemslot-full.jpg" }, "thumb": { "url": "http://example.com/books//fiction/salemslot-thumb.jpg" } } }, { "title": "Women's Leather Jacket", "description":"A classic look that never goes out of style with an exposed metal closure that zips at the cuff.", "date": 1474547794, "image": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg", "weight": 55, "tags": [ "site-store", "womens-clothing", "outerwear", "material-leather", "color-brown", "price-201-300" ], "price": 45000, "inventory": 1720, "vars": { "sailthru_color": "brown", "sailthru_material":"leather", "membership_price":"325.75" }, "url": "http://example.com/fashion/womens/outerwear/leatherjacket/", "images": { "full": { "url": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg" }, "thumb": { "url": "http://example.com/fashion/womens/outerwear/leatherjacket-thumb.jpg" } } }, { "title": "Invisible Monsters: A Novel", "description":"Love, betrayal, petty larceny, and high fashion fuel this deliciously comic novel from the author of <i>Fight Club</i>.", "date": 1474547794, "image": "http://example.com/books/fiction/invisiblemonsters-full.jpg", "weight": 30, "tags": [ "site-store", "genre-mystery-thriller", "author-chuck-palahniuk", "post-modern", "fiction", "price-5-10" ], "author": "Chuck Palahniuk", "price": 899, "inventory": 678, "vars": { "sailthru_genre":"mysteries-and-thrillers", "sailthru_category":"books" }, "url": "http://example.com/fiction/invisiblemonsters/", "images": { "full": { "url": "http://example.com/books/fiction/invisiblemonsters-full.jpg" }, "thumb": { "url": "http://example.com/books//fiction/invisiblemonsters-thumb.jpg" } } }, { "title": "Men's Chelsea Boots", "description": "Look great with jeans or a suit with these slick, black boots.", "date": 1474547794, "image": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg", "weight": 40, "tags": [ "site-store", "mens-clothing", "shoes", "material-suede", "color-black", "price-101-150" ], "price": 14000, "inventory": 15, "vars": { "sailthru_color": "suede", "sailthru_material":"leather", "membership_price":"115.50" }, "url": "http://example.com/fashion/mens/shoes/chelsea-boots/", "images": { "full": { "url": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg" }, "thumb": { "url": "http://example.com/fashion/mens/shoes/chelsea-boots-thumb.jpg" } } }, { "title": "From the Fashion Blog: The Most Comfortable Socks You'll Ever Own", "description":"They'll literally change your life. Find out where and how to get them!", "date": 1489092159, "expire_date": 1520628159, "image": "http://example.com/fashion/most-comfortable-socks-full.jpg", "weight": 25, "tags": [ "site-store", "vertical-fashion", "type-footwear", "author-richard-wilkins", "socks", "unisex-clothing" ], "author": "Richard Wilkins", "vars": { "sailthru_category": "fashion", "sailthru_storyid": 87422 }, "url": "http://example.com/fashion/most-comfortable-socks/", "images": { "full": { "url": "http://example.com/fashion/most-comfortable-socks-full.jpg" }, "thumb": { "url": "http://example.com/fashion/most-comfortable-socks-thumb.jpg" } } } ] }>
Zephyr:
{foreach content as c}
{if c.inventory >= 1000}
{content = map(content, lambda c: c + { "weight": c.weight * 10 })}
{/if}
{/foreach}
Output: The two items in the eComm feed that have more than 1000 pieces of inventory left (Salem’s Lost and Women’s Leather Jacket) will have their weight parameter multiplied by ten: Salem’s Lot’s weight increases from 30 to 300 Women’s Leather Jacket’s weight increases from 55 to 550
Explanation: This script loops through each piece of content from a Sailthru Content Feed and checks the “inventory” parameter, passed via the content API or content_update job via the job API. If the length of that parameter is greater than or equal to 1000, i.e. there are 1000 or more pieces of inventory for that particular item, we make a content assignment that uses a lambda, which creates an anonymous function, in conjunction with the map() function, to transform the “weight” value of each item, multiplying it by 10. This means that every item that is high in stock will now have a higher likelihood of being recommended (if using the personalize() function).