This file contains hidden or 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 'json' | |
| data1 = JSON.load('{"status": "fail", "messages": ["Out of capacity"]}') | |
| data2 = JSON.load('{"status": "success", "messages": [], "result": {"node": {"ip": "1.2.3.4", "description": "", "id": 974, "name": "VM#3"}}}') | |
| def get_from_json(data, query) | |
| query.split('.').inject(data) do |memo, key| | |
| key = key.to_i if memo.is_a?(Array) | |
| memo.fetch(key) | |
| end |
This file contains hidden or 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' | |
| =begin | |
| Math.sqrt(x) is consistently slower then x ** 0.5 in all major ruby implementations. | |
| =end | |
| SIZE = 1e6 |
This file contains hidden or 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 | |
| class LinearRegression | |
| attr_reader :coords, :size | |
| def initialize(coords) | |
| @coords = coords | |
| @size = coords.size | |
| end |
This file contains hidden or 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 Array | |
| def mean | |
| inject(:+).to_f / size | |
| end | |
| def median | |
| s = size | |
| idx1 = s / 2 | |
| sorted = sort | |
| if s.odd? |
This file contains hidden or 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 qSort[T <% Ordered[T]](list: List[T]): List[T] = list match { | |
| case Nil => Nil | |
| case l @ List(x) => l | |
| case x :: xs => { | |
| val (less, more) = xs.partition(_ < x) | |
| qSort(less) ::: x :: qSort(more) | |
| } | |
| } | |
| assert(qSort(Nil) == Nil) |
This file contains hidden or 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 lispyIf(condition: => Boolean)(thenClause: => Unit)(elseClause: => Unit) { | |
| if (condition) thenClause | |
| else elseClause | |
| } | |
| lispyIf(1 < 2) { | |
| println("true") | |
| } { | |
| println("false") | |
| } |
This file contains hidden or 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
| package qsort | |
| import "math/rand" | |
| func QuickSort(slice []int) []int { | |
| length := len(slice) | |
| if length <= 1 { | |
| sliceCopy := make([]int, length) | |
| copy(sliceCopy, slice) |
This file contains hidden or 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 quick_sort(list) | |
| return list.dup unless list.size > 1 | |
| x = list.sample | |
| h = list.group_by { |item| item <=> x } | |
| h.default = [] | |
| less, equal, more = h[-1], h[0], h[1] |
This file contains hidden or 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 | |
| IO.popen('git log --name-only --pretty=format:""') | |
| .readlines | |
| .map(&:chomp) | |
| .reject(&:empty?) | |
| .select { |file| File.exists?(File.expand_path(file, ENV['GIT_WORK_TREE'])) } | |
| .each_with_object(Hash.new(0)) { |file, changes| changes[file] += 1 } | |
| .to_a | |
| .sort_by(&:last) |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| /* | |
| concurrent hello world | |
| */ | |
| func main() { | |
| sayHello := make(chan bool) |