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
// Given a list of tasks, where each task has a duration, and a limited amount of available time to work, | |
// write a function to return the tasks that can be completed within the given time, without re-ordering | |
// the original list of tasks. | |
type Task = { name: string; duration: number }; | |
const tasks = [ | |
{ name: "Task 1", duration: 4 }, | |
{ name: "Task 2", duration: 2 }, | |
{ name: "Task 3", duration: 7 }, | |
{ name: "Task 4", duration: 5 }, |
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
class Foo | |
def call | |
'hi' | |
end | |
end | |
foo = proc { 'hi' } | |
Foo.new.call # 'hi' | |
foo.call # 'hi' |
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
class Notifier | |
attr_reader :notifier | |
def initialize(notifier) | |
raise ArgumentError, 'Expected the notifier to respond to `notify`' unless notifier.respond_to?(:notify) | |
@notifier = notifier | |
end | |
def notify(id, message) | |
notifier.notify(id, prettify(message)) |
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
class Notifier | |
attr_reader :notifier | |
def initialize(notifier) | |
raise ArgumentError, 'Expected the notifier to respond to `notify`' unless notifier.respond_to?(:notify) | |
@notifier = notifier | |
end | |
def notify(id, message) | |
notifier.notify(id, prettify(message)) |
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
module Notifier | |
def self.notify(id, message) | |
SlackClient.new.notify(id, prettify(message)) | |
end | |
def self.prettify(message) | |
HTMLCleaner.clean(message.strip) | |
end | |
end |
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
# a proc, which I will insist on calling a function | |
notifier = proc { |id, message| SlackClient.new.notify(id, message.strip) } | |
notifier.call('@kirill', 'hello, world ') |
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 users_with_discounts | |
@users_with_discounts ||= {} | |
if @users_with_discounts.has_key?(:return_value) | |
@users_with_discounts[:return_value] | |
else | |
@users_with_discounts[:return_value] = computation_that_returns_nil | |
end | |
end |
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
require 'benchmark' | |
Benchmark.bm do |measurer| | |
calculator = FibonacciCalculator.new | |
measurer.report 'with memoization' do | |
10.times { calculator.memoized_calculate(35) } | |
end | |
measurer.report 'without memoization' do |
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
class FibonacciCalculator | |
include RubyMemoized | |
def calculate(n) | |
return n if (0..1).include?(n) | |
calculate(n - 1) + calculate(n - 2) | |
end | |
memoized |
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
module RubyMemoized | |
class Memoizer | |
attr_reader :context, :method | |
def initialize(context, method) | |
@context = context | |
@method = method | |
end | |
def call(*args, &block) |
NewerOlder