Skip to content

Instantly share code, notes, and snippets.

View MaxPleaner's full-sized avatar

Max Pleaner MaxPleaner

View GitHub Profile
@MaxPleaner
MaxPleaner / files_list.md
Last active October 10, 2016 18:50
sublime packages list

This is the list of files in ~/.config/sublime-text-3/Installed\ Packages/.

They can all be installed through the sublime package manager.

0_package_control_loader.sublime-package
AdvancedNewFile.sublime-package
auto-save.sublime-package
Better CoffeeScript.sublime-package
Coffee2Js.sublime-package
@MaxPleaner
MaxPleaner / times.coffee
Last active January 27, 2017 15:57 — forked from FGRibreau/times.js
Ruby .times & .upto & .downto methods in Coffeescript
# Ruby = 5.times { |i| puts i }
# Coffee = (1).times (i) -> console.log i
Number.prototype.times = (cb) ->
i = -1;
while (++i < this) then cb(i)
+this
# Ruby = 1.upto(5) { |i| puts i }
# Coffee = (1).upto 5, (i) -> console.log i
Number.prototype.upto = (t, cb) ->
@MaxPleaner
MaxPleaner / example_7.rb
Last active September 20, 2016 05:03
priority merge example 7 (out of order)
a1 = 50.times.map { 1 }
a2 = 50.times.map { 2 }
puts "when priority is 1.0"
result = 50.times.map do
result = a1.priority_merge(1.0, a2)
result.select { |item| item == 2 }.count.to_f / a1.length.to_f
end
puts "results contain on average #{result.mean}% elements from the merged array"
@MaxPleaner
MaxPleaner / example_6.rb
Created September 20, 2016 04:00
priority merge example 6
result = set_a.priority_merge(1, set_2).uniq
@MaxPleaner
MaxPleaner / example_5.rb
Last active September 20, 2016 05:01
priority merge example 5
class Array
# helper method to help with testing
def mean
map(&:to_f).reduce(&:+) / length.to_f
end
# target is another array
# priority is a number between 0 and 1
@MaxPleaner
MaxPleaner / example_4.rb
Created September 20, 2016 03:36
priority merge exmaple 4
Y-AXIS: percent chance that next element will come from "set b"
X-AXIS: percent completed through iteration
Desired result
100% | x
75% | x
50% | x
25% | x
0% | x
@MaxPleaner
MaxPleaner / example_3.rb
Created September 20, 2016 03:23
priority merge example 3
Y-AXIS: percent chance that next element will come from "set b"
X-AXIS: percent completed through iteration
Example using zip:
100% |
50% | x x x x
0% |
|___________________
0% 33% 66% 100%
@MaxPleaner
MaxPleaner / example_2.rb
Last active September 20, 2016 04:13
priority merge exampel 2
set_a = result_sets[:top_rated]
set_b = result_sets[:top_rated_from_following_topics]
# Using zip
# --------------------
# Assuming both arrays are the same length, zipping them will create a result
# that includes all elements from both a1 and a2.
# It pulls from them in alternating order (a, b, a, b, etc)
result = set_a.zip set_b
@MaxPleaner
MaxPleaner / example_1.rb
Last active September 20, 2016 10:10
priority merge example 1
result_sets = {
top_rated: Post.all.order(rating: :desc).first(50),
top_rated_in_followed_topic: Post.all.where(topic_id: @user.followed_topics.pluck(:id)).order(rating: :desc).first(50)
}
@MaxPleaner
MaxPleaner / recursive_flatten.rb
Last active September 6, 2016 01:08
recursive Array#flatten
class MyArray < Array
def flatten
reduce [] do |memo, item|
memo.concat item.is_a?(Array) ? MyArray.new(item).flatten : [item]
end
end
end
test_arr = MyArray.new [ 1, [ 1 ], [ 1, [ 1 ] ] ]