Skip to content

Instantly share code, notes, and snippets.

View syntacticsugar's full-sized avatar
🎯
Focusing

RomyRomy syntacticsugar

🎯
Focusing
View GitHub Profile
# Euclid's formula yields Pythagorean triples for integers m and n with m < n:
# a = m**2 - n**2 ; b = 2*m*n ; c = m**2 + n**2
x = 1000
def euclids upto
result = []
(2..upto).each do |m| # Start at 2 as 1 results nothing anyway
(1...m).each do |n| # Euclid's formula only works for m > n
result << [m**2 - n**2, 2*m*n, m**2 + n**2]
class Array
def insert_every!(skip, str)
orig_ary_length = self.length
new_ary_length = orig_ary_length + ((orig_ary_length-1) / skip)
i = skip
while(i<new_ary_length) do self.insert(i,str); i+=(skip+1) end
self
end
end
<?php
/**
* Higher order functions in PHP
*
* Please let me know of any differences in the the uses and actual definitions of partial v. curry.
*/
/**
* Returns a partially applied function.
countZeroes :: [String] -> Int
countZeroes = sum . map length . map (filter (=='0'))
countZeroesAgain :: Show a => [a] -> Int
countZeroesAgain = countZeroes . map show
-- and for the one line, because I guess that's the challenge
-- btw, it's type is
-- cz :: Show a => [a] -> Int
-- which means that this function works any list of things that can be printed
@syntacticsugar
syntacticsugar / gist:4041425
Created November 8, 2012 20:37 — forked from nicholasbs/gist:3259846
Implementing the "new" operator in JavaScript
// New is a function that takes a function F
function New (F) {
var o = {}; // and creates a new object o
o.__proto__ = F.prototype // and sets o.__proto__ to be F's prototype
// New returns a function that...
return function () {
F.apply(o, arguments); // runs F with o as "this", passing along any arguments
return o; // and returns o, the new object we created
}
@syntacticsugar
syntacticsugar / cat.js
Created November 7, 2012 00:55 — forked from lfborjas/cat.js
kittehs
sys = require("child_process");
var Cat = function( name ){
this.name = name;
};
Cat.prototype.meow = function(){
sys.exec('say "' + this.name + ' says MEEEEOOOOOOOOOWOWOWOW"');
};
@syntacticsugar
syntacticsugar / gist:3936358
Created October 23, 2012 02:39 — forked from lfborjas/gist:817504
Filter even numbers in a list; fork it for maximum fun!
#these are meant to be run in a REPL, and the java one in beanshell of something of the sort:
#ruby
[1,2,3,4].select{ |x| x.even? }
#python
[x for x in [1,2,3,4] if not x%2]
#or, more norvingly
filter(lambda x: not x%2, [1,2,3,4])
#clojure
# -*- encoding: utf-8 -*-
require 'sinatra'
require 'slim'
require 'warden'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new(STDOUT, :debug)
@syntacticsugar
syntacticsugar / Gemfile
Created October 16, 2012 22:49 — forked from fairchild/Gemfile
An example sinatra omniauth client app
source :rubygems
gem 'sinatra'
gem 'json'
gem 'omniauth'
gem 'omniauth-oauth2'
gem 'omniauth-github'
# gem 'omniauth-att', :path => File.expand_path("./../../omniauth-att", __FILE__)
gem 'thin'
@syntacticsugar
syntacticsugar / huffman.py
Created August 4, 2012 18:10 — forked from ryanwitt/huffman.py
silly huffman coding example
__all__ = (
'build',
'endode',
'decode',
)
phrase = 'A man a plan a canal Panama'.lower()
def build(phrase):
"""