Skip to content

Instantly share code, notes, and snippets.

@Nimster
Nimster / ruby_summary.rb
Created April 8, 2012 16:22
Quick reference to some common Ruby idioms/capabilities - mainly to serve as a reminder for me
## This is an example encompassing much of ruby's basic syntax.
## It is derived from various examples in the Pragmatic Bookshelf's "Programming
## Ruby" book. It is intended as a quick reference, and you should grep (search
## the file) for various terms like "splat args", "exception" or "iterator" to
## get a quick look at how they are used. It is probably very hard to make any
## sense of this by reading it linearly, nor did I attempt to have the example
## make any logical sense at all - the operations done here are not at all
## coherent
##
## This is freely distributable with the following credit:
@Nimster
Nimster / gist:2624523
Created May 6, 2012 21:24
Quick reference to some common Matlab idioms/capabilities - mainly to serve as a reminder for me
[I, J] = find(A > 0) % Col-order search
[J, I] = find(A' > 0) % Row-order (non-zero) search
arrayfun(@(x) length(x.field)>0, some_struct_array) % Apply length to 'field' in each element of a struct array
a_struct = struct('field1', value1, 'field2', value2) % Create a struct ("object")
list = [list 1] % add element to a vector
@Nimster
Nimster / ruby_lazy_enums.rb
Created July 4, 2012 00:52
Lazy enumerator helpers with Ruby
# Motivation:
#
# Using lazy evaluation is the way truly functional (not just 'functional style') programming languages
# achieve performance. Ruby is not there yet for several reasons, but at least some of them can be remedied
# with basic amendments to the standard library.
#
# If you want to find, for instance, the first number n<1000 such that the approximation (1+(1/n))^n to e is
# less than a certain distance, say, eps = 0.01. If you just write
(1..1000).to_enum.map { |d| ((1+(1.0/d))**(d) - (Math::E)).abs }.find_index { |x| x < 0.01 }
# It works, and returns 134. But you calculated 1000-134=876 more values than you had to. With the code below,
@Nimster
Nimster / gist:3683049
Created September 9, 2012 06:49
Liam Gibbing's Python Cheat Sheet
This is taken from Liam Gibbing's Python Cheat Sheet at https://docs.google.com/file/d/0B9VT_L2CDnKvODYyNTc5NjktYmMyOC00NDFkLTliNTctMzQzMTAzYjUyYmYy/view?pli=1&sle=true
I just don't like the DocX format so I imported it here.
FUNCTIONS, METHODS & ATTRIBUTES
NUMBERS
• math.pi and math.e: Returns values of both pi and exponential const. respectively.
• math.sqrt(integer1): Returns square root of integer, use cmath.sqrt for negatives.
• math.floor(integer1): Returns integer rounded down to nearest whole.
• math.ceil(integer1): Returns integer rounded up to nearest whole.
• round(integer1, [NumOfDigits]): Rounds integer depending on number of digits specified, default is to nearest whole.
@Nimster
Nimster / gist:2838511
Created May 30, 2012 19:45
Quick reference to some common R idioms/capabilities - mainly to serve as a reminder for me
### Most of the summary is taken from the awesome R twotorials at http://www.twotorials.com/ by Anthony Damico
### Some of it are my additions from my experience. This is intended so you can Ctrl+F and find what you want using
### common names of functions and concepts from other languages or statistics.
### Troubleshooting: Search http://tolstoy.newcastle.edu.au/R/ , http://www.r-bloggers.com/, http://www.rseek.org/
### Basics
traceback() # Get the call stack after an error, for debugging
32 %% 2 # == 0 mod operator
5 %/% 3 # == 1 integer division
@Nimster
Nimster / intro_to_R
Created February 11, 2013 20:49
An introduction to R, as presented at http://www.meetup.com/Big-Data-Israel/events/96536782/
######### Intro to R ###############
######### The Data Frame ###########
df <- data.frame(
row.names = c('HaLikud', 'Yesh Atid', 'HaAvoda', 'HaBait HaYehudi', 'Yehadut HaTora', 'Meretz', 'Shas'),
LeaderName = c('Netanyahu', 'Lapid', 'Yehimovitch', 'Bennet', 'Litzman', 'GalOn', 'Yishai'),
Category = c('Right', 'Center', 'Left', 'Right', 'Religious', 'Left', 'Religious'),
Mandates = c(31, 19, 15, 12, 7, 6, 11)
)
@Nimster
Nimster / ringbuffer.rb
Created November 15, 2012 11:21 — forked from eerohele/ringbuffer.rb
A simple ring buffer for Ruby.
class RingBuffer < Array
attr_reader :max_size
def initialize(max_size, enum = nil)
@max_size = max_size
enum.each { |e| self << e } if enum
end
def <<(el)
if self.size < @max_size || @max_size.nil?