Skip to content

Instantly share code, notes, and snippets.

@marshall-lee
marshall-lee / lazy_primes.hs
Last active December 26, 2015 11:48
Lazy prime numbers in Haskell
sqrti x = (truncate . sqrt . fromInteger) x
primes = filter (\n -> all (\y -> n `mod` y /= 0) [2..sqrti(n)]) [2..]
isPrime x = elem x (takeWhile (<= x) primes)
-- ghci> take 10 primes
-- [2,3,5,7,11,13,17,19,23,29]
-- ghci> take 8 (drop 2 primes)
-- [5,7,11,13,17,19,23,29]
-- ghci> take 100500 primes
-- ...
@marshall-lee
marshall-lee / lazy_primes.rb
Last active July 3, 2017 16:48
Prime numbers in Ruby. Demonstrates the power and the bounds of lazy enumerators in Ruby.
require 'benchmark'
# returns lazy enumerator
def primes_lazy_but_stupid
(2..Float::INFINITY).lazy.reject{ |i| (2..Math.sqrt(i)).any?{ |j| i % j == 0 } }
end
# returns just array of primes <= n
def primes_just_stupid(n)
(2..n).reject{ |i| (2..Math.sqrt(i)).any?{ |j| i % j == 0 } }
@marshall-lee
marshall-lee / sleep_sort.erl
Last active December 26, 2015 11:59
Sleep Sort in Erlang
-module(sleep_sort).
-export([sort/1, sort_better/1]).
sort(List) ->
Self = self(),
lists:foreach(fun(X) -> spawn(fun() -> timer:sleep(X), Self ! X end) end, List),
[receive X -> X end || _ <- lists:seq(1, length(List))].
sort_better(List) ->
@marshall-lee
marshall-lee / promise_sort.js
Last active May 15, 2016 15:47
Promise Sort: The First Truly Asynchronous Quicksort Algorithm!
PromiseSort = {
pivotIndexMedian: function (ary, pred, startIndex, endIndex) {
var midIndex = Math.floor((startIndex + endIndex) / 2),
indexes = [startIndex, midIndex, endIndex];
return indexes.sort(function (i, j) {
return pred(ary[i], ary[j]);
})[1]; // return median index
},
pivotIndexRandom: function (_ary, _pred, startIndex, endIndex) {
return Math.floor(Math.random() * (endIndex - startIndex + 1)) + startIndex;
@marshall-lee
marshall-lee / timestamps_fix.rb
Last active August 29, 2015 14:12
Add `null: true` to timestamps migrations when upgrading to Rails 4.2 or higher
# this is a rewriter class for adding `null: true` to timestamps migrations when upgrading to Rails 4.2 or higher.
# calling `timestamps` without specifying `null: false|true` is now deprecated, so it must be fixed in all of your db migrations
#
# script uses this approach: http://whitequark.org/blog/2013/04/26/lets-play-with-ruby-code/
# to run:
# gem install parser
# ruby-rewrite -l timestamps_fix.rb -m ~/code/my_rails_app/db/migrate
# (c) Vladimir Kochnev 2014
class TimestampsFix < Parser::Rewriter
str = "abc"
1000.times do
action = [:upcase, :downcase, :reverse].sample
str = str.send action
end
puts str
# What is the probability that "CBA" will be output?
# Какова вероятность, что будет выведена строка "CBA"?
@marshall-lee
marshall-lee / leye.rake
Created March 4, 2015 18:27
deploy you apps with capistrano and Eyefile!
# put this to lib/capistrano/tasks/leye.rake
namespace :leye do
desc "Show leye information"
task :info do
on roles(:app) do
within current_path do
with fetch(:leye_env) do
if execute :leye, 'load'
puts capture(:leye, 'info')
end
@marshall-lee
marshall-lee / online_change_table.rb
Last active November 15, 2018 11:15
Online (non-blocking) ActiveRecord migrations using pt-online-schema-change utility from Percona Toolkit
# Online (non-blocking) ActiveRecord migrations using pt-online-schema-change utility from Percona Toolkit.
#
# In your Rails app put it as lib/online_change_table.rb
# Now you can use it in your migration file:
#
# require 'online_change_table'
#
# class AddSomethingToSomethings < ActiveRecord::Migration
# def up
# online_change_table :somethings do |t|
@marshall-lee
marshall-lee / 9gag-gif
Last active August 29, 2015 14:20
Download GIFs from 9gag
#!/usr/bin/env ruby
require 'open-uri'
url = ARGV[0]
$stderr.puts 'URL not specified' and exit 1 unless url
uri = URI.parse url
$stderr.puts 'This should be 9gag.com URL' and exit 1 if uri.host != '9gag.com'
m = uri.path.match %r{^/gag/(\w+)$}
$stderr.puts 'Malformed URL!' and exit 1 unless m
h = { [] => :ok }
p h[[]] # ok
h = { [:lol] => :ok }
p h[[:lol]] # ok
h = { [] => :ok }
h.first.first << :lol
p h[[:lol]] # nil