Skip to content

Instantly share code, notes, and snippets.

View mads-hartmann's full-sized avatar

Mads Hartmann mads-hartmann

View GitHub Profile
#!/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]
@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/
@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 / 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 / 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 / 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 =
// 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("/"))
/**
* 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:
*
import sbt._
class Project(info: ProjectInfo) extends DefaultProject(info)
{
lazy val sctags = compileWithSctags
protected def compileWithSctags = task {
val compilerConfig = new MainCompileConfig {
override def baseCompileOptions = CompileOption("-Xplugin:sctags_2.8.0.Beta1-RC2-1.0.jar") :: super.baseCompileOptions.toList
<%@ var word:String %>
So Long, and Thanks for All the ${word}