Skip to content

Instantly share code, notes, and snippets.

View FranckyU's full-sized avatar
🎯
Focusing

Andy Kifer FranckyU

🎯
Focusing
View GitHub Profile
@FranckyU
FranckyU / functional_brute_force_bitwise_random.rb
Created December 15, 2014 13:39
Brute force bitwise number generator
->(m, n){->(collection){collection[rand(collection.size)]}.call(("1"*n).to_i(2).up_to(("1"*m).to_i(2)).select{|i| i.to_s(2).gsub('0', '') == "#{'1'*n}"})}.call(m_value, n_value).to_i(2)
@FranckyU
FranckyU / elegant_bitwise_random.rb
Last active August 29, 2015 14:11
Elegant bitwise number generator
lambda(m, n) do
seed = (rand(2).even?) ? 0b0 : 0b1
num_bit_1_left = (seed == 0b1) ? n-1 : n
bits_length = 1
(m-1).times do
bit_mask = if num_bit_1_left > 0
((m - bits_length) > num_bit_1_left) ? ((rand(2).even?) ? 0b0 : 0b1) : 0b1
@FranckyU
FranckyU / js_tree_display_helper.js
Last active August 29, 2015 13:57
Helper for Node tree display - for a FB post comment
function display_sub_tree(node, _level)
{
level = _level || 0 ;
result = '<ul class="level-'+level+'">' ;
children_of(node).each(function(child)
{
result = result + '<li>' + child.name ;
# V2, please see https://gist.github.com/FranckyU/9024250 for V1
# UPDATES
# - Monkey patching ActiveRecord::Base to to handle it all
# - The 'with_..._connection' methods get native without having to include any module
# -----------------------------------
# PUT THE FOLLOWING IN AN INITIALIZER
# -----------------------------------
@FranckyU
FranckyU / rails-response-to-cqrs.rb
Last active August 29, 2015 13:56
Rails response to CQRS
# I have been recently exposed to Martin Fowler's CQRS concept and saw some implementations on .Net
# Like this http://www.codeproject.com/Articles/555855/Introduction-to-CQRS
# Disliked it as it brings a lot of noise in the code, a lot of handlers, class imbrications that defies simple human logic
# To say shortly, it's not quite cool, and as a rubyist I like keeping it simple and human
# Though the core concept of CQRS is quite simple, separate writes and reads to different database connections to increase global performance
# But implementation is very complex
# Lastly, someone I know even spent 2 years on a .Net project using CQRS (don't know how many weeks/months spend for just implementing the CQRS thing)
# I wrote this gist to see whether it can be done in 10 minutes
# And I did it in 10 minutes
# I love ActiveRecord so I DRYed by just extending it