This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function distinctPreserveOrder(items) { | |
return [...new Set(items)]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def distinct_preserve_order_py37(items): | |
assert sys.version_info >= (3, 7), 'Please use Python 3.7 or above for this code.' | |
return list(dict().fromkeys(items).keys()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinct_preserve_order(items): | |
seen = set() | |
result = list() | |
for item in items: | |
if item not in seen: | |
seen.add(item) | |
result.append(item) | |
return list(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinct_set(items): | |
result = set() | |
for item in items: | |
# Checking is not needed, because the list.add() method is idempotent | |
result.add(item) | |
return list(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinct_naive(items): | |
""" | |
This naive implementation returns distinct elements of a list. | |
""" | |
results = list() | |
# iterate through all items | |
for item in items: | |
# append item to results if not member yet | |
if not item in results: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinct_naive_verbose(items): | |
""" | |
This naive and verbose implementation returns distinct elements of a list. | |
""" | |
results = list() | |
# iterate through all items | |
for item in items: | |
member = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinct_set_constr(items): | |
# The constructor of the set iterates through the item list in linear O(n) time: | |
# https://github.com/python/cpython/blob/3.9/Objects/setobject.c#L902-L909 | |
return list(set(items)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const apiPrefix = 'https://api.stripe.com/v1'; | |
const formBody = object => Object.keys(object).map(key => { | |
const encodedValue = encodeURIComponent(object[key]) | |
const encodedKey = encodeURIComponent(key) | |
return `${encodedKey}=${encodedValue}` | |
}).join('&'); | |
export default class StripeClient { | |
constructor(apiToken) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const coroutine = nextValue => iterator => { | |
const { done, value } = iterator.next(nextValue); | |
if (done) { | |
return; | |
} | |
if (value.constructor === Promise) { | |
value.then(promiseValue => { | |
coroutine(promiseValue)(iterator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const delay = (ms, result, cb) => setTimeout(() => cb(result), ms); | |
function delays() { | |
delay(800, "Hello, I'm in a", a => { | |
console.log(a); | |
delay(400, "callback!", b => { | |
console.log(b); | |
}); | |
}); | |
} |
NewerOlder