Skip to content

Instantly share code, notes, and snippets.

View mads-hartmann's full-sized avatar

Mads Hartmann mads-hartmann

View GitHub Profile
/**
* THE FOLLOWING EXERCISE IS FROM (THE SAMPLE OF) "PURELY FUNCTIONAL DATA STRUCTURES"
* http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504/ref=sr_1_1?s=books&ie=UTF8&qid=1282766033&sr=1-1
*
* Exercise 2.1
*
* Write a function suffixes of type a list -> a list list that takes
* a list xs and returns a list of all the suffixes of xs in decreasing
* order of length. For example:
*
// In a method that returns a NodeSeq.
// compiles
val seq = for {
moduleId <- S.param("moduleId")
moduleInstanceId <- S.param("id")
backupId <- S.param("backupId")
} yield bind("backup", xhtml, "path" -> backupId)
seq openOr (redirectTo("/"))
@mads-hartmann
mads-hartmann / JqAttrUsage.scala
Created December 2, 2010 14:38
Shows how to use JqAttr (Lift)
package sidewayscoding.example
// normal imports goes here
import net.liftweb.http.js.JE._ // implicit string => Str
import net.liftweb.http.js.JsCmds._ // implicit JsExp => JsCmd
import net.liftweb.http.js.jquery.JqJE.{JqId,JqAttr, Jq}
class Test {
def test =
@mads-hartmann
mads-hartmann / bundles.sh
Created December 7, 2010 16:04
This is how I manage my textmate bundles.
#
# This script will install the following Textmate bundles
#
# Languages
# - c https://github.com/textmate/c.tmbundle
# - coffeescript https://github.com/jashkenas/coffee-script-tmbundle
# - context free https://github.com/textmate/context-free.tmbundle
# - erlang https://github.com/textmate/erlang.tmbundle
# - haskell https://github.com/textmate/haskell.tmbundle.git
# - html https://github.com/textmate/html.tmbundle
@mads-hartmann
mads-hartmann / patternmatching.scala
Created January 23, 2011 09:45
Pattern matching as a control structure
/*
I want to use pattern matching in coffeescript as a more capable switch. It uses destructuring
assignments to figure out which of the cases that matches. So it can match on the entire
structure of the object instead of just an integer.
(* in F# *)
let list = [1;2;3;4]
match list with
| [] -> printfn "empty list"
| x :: xs -> printfn "first element is " + x
@mads-hartmann
mads-hartmann / possible_bug.scala
Created March 24, 2011 08:20
Possible bug in Scala
package com.reversebackup
import scala.xml._
import scala.xml.transform._
import scala.xml.XML
/*
This snippet creates two methods: removeAllNamedTagsTransformer & createRewriteRule.
They both create a RuleTransformer which removes all tags with a specific name.
However, it seems that it doesn't work if the name of the parameter is 'name'.
@mads-hartmann
mads-hartmann / Coffeescript ctags
Created April 7, 2011 07:44
ctags definitions for Coffeescript. Very basic for now. "> ctags -e -R source_folder" and then M-. to jump to the definition of any function or variable (if you're using emacs)
--langdef=coffee
--langmap=coffee:.coffee
--regex-coffee=/^[ \t]*([A-Za-z.]+)[ \t]+=.*->.*$/\1/f,function/
--regex-coffee=/^[ \t]*([A-Za-z.]+)[ \t]+=[^->\n]*$/\1/v,variable/
#!/usr/bin/env ruby
# Alignes the source
def align(text, regexp, width)
text.to_a.map do |line|
if offset = offsetOfRegexpInLine(line, regexp)
if shouldInsertBefore(line, regexp)
before = line[0..offset-1]
before + ' ' * (width - (before.length)) + line[offset..line.length-1]
else
before = line[0..offset]
Jun 8 08:21:15 localhost Unknown[305]: Launching the Language Chooser for an OS Install
Jun 8 08:21:24 localhost configd[102]: bootp_session_transmit: bpf_write(en1) failed: Network is down (50)
Jun 8 08:21:24 localhost configd[102]: DHCP en1: INIT transmit failed
Jun 8 08:21:24 localhost LCA[306]: Bookmark failed to issue extension for item (depth=4000): No such file or directory
Jun 8 08:21:28 localhost LCA[306]: Using keyboard layout 9
Jun 8 08:21:28 localhost Unknown[307]: Keyboard Layouts: duplicate keyboard layout identifier -16899.
Jun 8 08:21:28 localhost Unknown[307]: Keyboard Layouts: keyboard layout identifier -16899 has been replaced with -28673.
Jun 8 08:21:28 localhost Unknown[307]: Keyboard Layouts: duplicate keyboard layout identifier -16900.
Jun 8 08:21:28 localhost Unknown[307]: Keyboard Layouts: keyboard layout identifier -16900 has been replaced with -28674.
Jun 8 08:21:28 localhost LCA[306]: Found primary language hint "en"
// Attempt to do tail-recursion in JavaScript without getting a stack-overflow exception.
// This is horribly slow because of all of the extra stack frames pr. thunk.
// What would you call this? Lazy-tail-recursion?
var tailrec = function(f) {
for(var x = f() ; typeof x == "function" ; x = x()){}
return x;
};
var counter = function(){