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
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 | |
-- ... |
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' | |
# 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 } } |
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(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) -> |
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
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; |
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
# 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 |
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
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"? |
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
# 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 |
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
# 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| |
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
#!/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 |
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
h = { [] => :ok } | |
p h[[]] # ok | |
h = { [:lol] => :ok } | |
p h[[:lol]] # ok | |
h = { [] => :ok } | |
h.first.first << :lol | |
p h[[:lol]] # nil |
OlderNewer