Skip to content

Instantly share code, notes, and snippets.

View syntacticsugar's full-sized avatar
🎯
Focusing

RomyRomy syntacticsugar

🎯
Focusing
View GitHub Profile
@syntacticsugar
syntacticsugar / 6th_euler.rb
Created March 7, 2013 20:42
Euler #6, coded on the subway in Washington Heights. "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."
=begin
The sum of the squares of the first ten natural numbers is,
1**2 + 2**2 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)**2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
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
@syntacticsugar
syntacticsugar / gist:5059926
Created February 28, 2013 20:40
Euler #1, in bloated, x-rated, nasty, perverse, lecherous, spaghetti Javascript ! (evil cackle)
// Find the sum of all the multiples of 3 or 5 below 1000.
//
//
// ruby version: (inspired by Lfborjas' Ruby Fuckery)
//multi = ->(x) { (x % 3).zero? or (x % 5).zero? }
//puts (1...1000).select{ |x| multi[x] }.inject(:+).to_s
//233168
function arrayFromRange(lo, hi){
(define (abs x)
(cond ((= 0 x) 0)
((< 0 x) x)
((> 0 x)(- 0 x))))
(define abs
(lambda (x)
(cond ((= 0 x) 0)
((< 0 x) x)
((> 0 x)(- 0 x)))))
<?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.
@syntacticsugar
syntacticsugar / korean_woman_in_a_shop.markdown
Created November 26, 2012 05:33
korean woman in a shop

Well, I've certainly been on the lookout for leggings.

I told myself this as I entered the small clothing shop on 14th St. It was a Saturday afternoon in Union Square when I spotted the rack of navy, brown, and black. '$3.99' was scrawled in cardboard. I chose a pair in black and headed toward the register to pay.

The second I stepped inside I heard her voice. An older Asian woman, perhaps in her fifties, held a phone to her face and manned the register in the back. She spoke in loud and brusque Korean, sans affectation. I imagined she was talking to her sister on a different continent. Or an old friend.

Or a daughter.

As I brought the pair of leggings up to the counter her voice lowered slightly in acknowledgement. Polite, I noted. She held up four fingers. I reached for my wallet, and waited for the questions that would inevitably follow.

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 / curly_braces_do_end_ruby.md
Created November 20, 2012 19:06
Ruby: {} versus 'do/end' syntax

Curly braces vs. do/end in code block syntax

The difference between the two ways of delimiting a code block is a difference in pre- cedence. Look at this example, and you’ll start to see how this plays out:

array = [1,2,3]
  => [1,2,3]
array.map {|n| n * 10 }
  => [10, 20, 30]
array.map do |n| n * 10 end
  => [10, 20, 30]
puts array.map {|n| n * 10 }
@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"');
};