diff options
Diffstat (limited to 'script.md')
-rw-r--r-- | script.md | 35 |
1 files changed, 5 insertions, 30 deletions
@@ -105,36 +105,11 @@ There, now, we can make sure our friends are happy on their birthdays! But, this is getting annoying; what about Christmas, Thanksgiving, or Easter? Making a new black box to make a list of new cards, going through each person, every single time to construct a list of cards, is getting really tedious. -What if we generalized this? We create a bunch of black boxes that take a person, and generate them a card, specifically; like a template for a card you could print off and fill in manually. +[SPACE] What if we generalized this? [SPACE] We create a couple of black boxes that take a person, and generate them a card, specifically; like a template for a card you could print off and fill in manually. Like this, for alan turing. -```python -def valentine_letter(person): - return f"Dear, {person.name}\nYour smile lights up my world. Happy Valentine's Day!") - -def birthday_card(person): - today = new Date() - daysUntilBirthday = toDays(new Date(person.birthday, today.year) - today) - newAge = today.year - person.birthday.year - - card = f"Happy Birthday {name}\nI can't believe you're already {newAge} years old!" - cards.append({ "message": card, "deliverInDays": daysUntilBirthday }) -``` - -Then, we can use a black box that takes a list of people, and applies this template to each person. - -``` -def buildCards(people, cardMaker): - cards = [] - for person in people: - card = cardMaker(person) - cards.append(card) - return cards - -people = [{"name": "Joseph", birthday: new Date()}, {"name": "DeeDee", birthday: new Date()}] -buildCards(people, birthdayCard) -``` +Then, we can use a new black box that takes a list of people, and applies this template to each person. -The ability in a language to pass a function around like this - like a variable - is what makes functions "first class". And the `buildCards` function takes a function as input, making it a "higher order function". (TODO: slides) +The ability in a language to pass a function around like this - like a variable - is what makes functions "first class". And the `buildCards` function takes a function as input, making it a "higher order function". Functional Reproduction === @@ -211,7 +186,7 @@ Immutability === We briefly mentioned side effects and alluded them to the unpredictability of a partner. We love our black boxes because they're reliable. -But, we've actually had an impostor among us (AMOGUS sound??) throughout this presentation. Specifically in ~buildCards~: +But, we've actually had an impostor among us (AMOGUS sound??) throughout this presentation. Specifically in `buildCards`: ``` def buildCards(people, cardMaker): @@ -244,7 +219,7 @@ def build_cards(people, card_maker): return [card] + build_cards(rest_people, card_maker) ``` -Here we're not changing anything at all (except the stack), with the same functionality; this code is "immutable". When we call ~build_cards~ on a list of people, we're 100% certain we're not gonna get something odd short of a bug. +Here we're not changing anything at all (except the stack), with the same functionality; this code is "immutable". When we call `build_cards` on a list of people, we're 100% certain we're not gonna get something odd short of a bug. At a high level there are so many benefits to immutability: + Concurrency (TODO: go into more detail) |