Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Translation Desk's Quote Pipeline

Porting the Translation Desk’s Quote Pipeline

You ported part of the translation desk’s quote script to JavaScript. The desk counts the words in a museum guide, prices each request in pence per word, records every request, never calculates an identical price twice, and formats pence in the desk’s currency. This part prints five lines. Python gets the counts from two decorators on two functions. JavaScript has no @ line, so you place each wrapper, and the counts must come out the same.

The project folder, translation-desk/, has a package.json with { "type": "module" }, so Node treats desk.js and helpers.js as ES modules, and desk.js imports from helpers.js. withAudit is the wrapper that keeps its receiver, in place of Python’s with_audit.

Python caches with lru_cache. JavaScript has no built-in memoizer, so memoizePrice keys a Map by both inputs, through JSON.stringify([words, ratePence]), because a Map matches arrays by identity, not by value the way lru_cache matches arguments. It only suits a pure calculation with two whole-number inputs.

Python decorates price with @lru_cache. JavaScript has no @ line for a function, so you pass the arrow to memoizePrice and keep what it returns. The arrow adds one to calculations each time it really runs: Python needs a global statement for that, and JavaScript assigns the outer let directly. The calculation reads only its two arguments, never this or the desk, so it’s safe to cache by those two inputs alone.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Here, you’ll port the translation desk’s quote script to JavaScript. The desk counts the words in a museum guide and prices each request in pence per word.

00:09 It records every request, never calculates an identical price twice, and formats pence in the desk’s currency. This part prints five lines: 400 words, two quotes of 48 pounds, one calculation and two audit entries.

00:26 Python gets them from two decorators on two functions. JavaScript has no at-sign line, so you place each wrapper, and the counts must come out the same. The desk’s one-time setup and rush request are yours to add in the next exercise.

00:41 In JavaScript, the folder’s package.json sets the type to module, so Node treats the files as ES modules, and desk.js can import from the helpers file.

00:53 withAudit() is the wrapper that keeps its receiver, in place of Python’s with_audit. Python caches with lru_cache.

01:01 JavaScript has no built-in memoizer, so memoizePrice keys a map by both inputs, through JSON.stringify(), because a map matches arrays by identity, not by value the way lru_cache matches arguments.

01:15 It only suits a pure calculation with two whole-number inputs.

01:21 Python decorates price with lru_cache. JavaScript has no at-sign line for a function, so you pass the arrow to memoizePrice and keep what it returns.

01:32 The arrow adds one to calculations each time it really runs. Python needs a global statement for that; JavaScript assigns the outer let directly.

01:42 The calculation reads only its two arguments, never this or the desk. So it’s safe to cache by those two inputs alone.

01:52 Python’s desk is an instance of a class, with a currency attribute. The port is one object literal: currency, then two methods in shorthand.

02:02 quote hands its two numbers to price. format reads this.currency and shows pence as pounds, to two decimal places.

02:10 Here the analogy stops. Python saves self as soon as you read desk.format, before any call. JavaScript only looks at the object before the dot when the call happens.

02:23 Python applies with_audit with an at-sign line. In JavaScript, you replace the method once the object exists: desk.quote becomes the audited version.

02:34 That’s the call the at-sign line makes for you, written out by hand. The wrapper is a regular function, so it gets the desk as this from the call, and apply forwards that to the original.

02:45 Python’s audit line finds the word count at index one, because self arrives as the first argument. In JavaScript, the receiver travels as this, outside the arguments, so the words are at index zero.

02:59 Now the count. countWords walks the guide, and each call adds its children’s returned counts to its own words. Then come two requests at 12 pence a word, both through the audited desk.quote method.

03:13 .map() calls the formatter plainly, with no object before a dot. Python’s bound method saves its receiver. Here, .bind() saves the desk as the formatter’s receiver.

03:24 Spread puts both quotes into the list, and .join() puts each line on its own row.

03:32 First, flip the order. This version audits the calculation itself, and the cache wraps the audit. Your turn: pause the video and predict how many audit entries the same two requests leave.

03:50 One. The second request stops at the cache and never reaches the audit. Stack lru_cache above with_audit on quote, and Python records one entry too.

04:02 Now run the port. The five lines match Python’s: two entries, one calculation. Each request goes through the audit wrapper first. The wrapper calls quote, quote asks the cached price, and then the wrapper records the entry.

04:17 The second finds its key and skips the calculation.

04:23 So port each decorator as an explicit step, in the order you mean. Cache the pure price by both inputs. Audit the request method outside that cache, so every request is recorded.

04:35 And bind the formatter before you hand it on.

04:38 Next up is the exercise Finish the Desk’s Setup and Rush Quotes. You’ll add the desk’s one-time setup and the rush request at 15 pence a word, then compare all seven lines with Python’s.

Become a Member to join the conversation.