Skip to content

Instantly share code, notes, and snippets.

View mythz's full-sized avatar

Demis Bellot mythz

View GitHub Profile
@mythz
mythz / cash-register-mixin.coffee
Created May 20, 2011 02:15
Rewriting Peep Code app.coffee example
# CoffeeScript With Mixins
$ ->
template = _.templateFor '#meal-template'
meal = new Meal
_.focusOn '#entry'
_.onSumbitOf '#entry_form', ->
meal.add new Dish _.valOf '#entry'
_.setHtmlOf 'ul#meal', template meal.toJSON()
@mythz
mythz / cash-register-types.coffee
Created May 20, 2011 02:45
Cash Register classes in CoffeeScript and Javascript
class Dish
constructor: (rawDescription="") ->
[all, @title, @price] = @parseRawDescription rawDescription
@price = new Money @price
parseRawDescription: (rawDescription) ->
pattern = ///
([^$]+) #Title
(\$\d+\.\d+) #Price
///
@mythz
mythz / cash-register-spec.coffee
Created May 21, 2011 02:20
Peepcode cash resgister example of using Jasmine with CoffeeScript
describe "Dish", ->
describe "constructor", ->
beforeEach ->
@dish = new Dish 'Sirloin Steak $18.99 mains'
it "extracts title", ->
(expect @dish.title).toEqual('Sirloin Steak')
@mythz
mythz / hello-backbonejs-step5.coffee
Created June 13, 2011 14:51
Hello Backbone.js in CoffeeScript
# Rewrote excellent intro to Backbone.js http://arturadib.com/hello-backbonejs/ in CoffeeScript
$ ->
Backbone.sync = (method, model, success, error) ->
success()
class Item extends Backbone.Model
defaults:
part1: 'hello'
part2: 'world'
@mythz
mythz / OrmLiteAuthorSample.cs
Created June 17, 2011 01:59
Shorthand OrmLite db access with IDbConnectionFactory extension methods
IDbConnectionFactory dbFactory = new OrmLiteConnectionFactory("...",
SqlServerOrmLiteDialectProvider.Instance);
Author author = dbFactory.Exec(dbCmd => dbCmd.GetById<Author>(1));
List<Author> rows = dbFactory.Exec(dbCmd =>
dbCmd.Select<Author>("Username LIKE {0}", "%example.com"));
@mythz
mythz / redis-counters.coffee
Created August 14, 2011 13:14
Create and increment multiple counters with express, coffeescript and redis
#1. install node, npm and coffeescript
#2. >npm install express
#3. >npm install node_redis
#4. >npm install -d
redis = require('redis').createClient()
redis.on 'error', (err) -> console.log "Redis connection error to #{redis.host} : #{redis.port} - #{err}"
app = require('express').createServer()
#create and increment multiple counters in redis, via: http://localhost:3000/counters/my-counter
@mythz
mythz / weighted_sequences.coffee
Created September 2, 2011 04:21
Generate a weighted sequence of numbers with an optional multiplier
# Generate a sequence of numbers:
# - ordered with the highest values
# - has a sum of 1
# - has an optional multiplier to alter the decreasing rate
sequence = (n,m=0) ->
f = (p,multiplier) -> p * p + multiplier
s = ""
@mythz
mythz / Cat.cs
Created September 10, 2011 06:38 — forked from anonymous/cat.coffee
Go and CoffeeScript examples of writing a file to stdout
using System;
public class File {
public string Name { get; set; }
public string Read() {
return System.IO.File.ReadAllText(Name);
}
}
class Program {
@mythz
mythz / binary-search.coffee
Created September 13, 2011 19:09
Side by Side: CoffeeScript vs JavaScript - Algorithms Edition
# All CoffeeScript examples from: https://github.com/jashkenas/coffee-script/blob/master/examples/computer_science/
# All Java Script examples from: https://github.com/nzakas/computer-science-in-javascript - Copyright (c) 2009 Nicholas C. Zakas
# - Released under https://github.com/nzakas/computer-science-in-javascript/blob/master/LICENSE (*.js copyright headers reduced for clarity)
# Uses a binary search algorithm to locate a value in the specified array.
binary_search = (items, value) ->
start = 0