Skip to content

Instantly share code, notes, and snippets.

@wrwills
wrwills / OptionGolf.scala
Created October 8, 2010 14:19
applicative_example
import scalaz._
/**
* A simple example of how applicative functors can shorten code
* adapted from chapter 8? of Real World Haskell
*/
object OptionGolf {
scala> def apply[A, B](a: A)(f: A => B) = f(a)
apply: [A,B](a: A)(f: (A) => B)B
scala> apply(1)(_ * 2)
res0: Int = 2
scala> def apply[A, B](a: A, f: A => B) = f(a)
apply: [A,B](a: A,f: (A) => B)B
scala> apply(1, _ * 2)
@softprops
softprops / murder.sh
Created November 3, 2010 05:39 — forked from defunkt/murder.sh
# sh function to murder all running processes matching a pattern
# thanks 3n: http://twitter.com/3n/status/19113206105
murder () {
ps | grep $1 | grep -v grep | awk '{print $1}' | xargs kill -9
}
@softprops
softprops / browser
Created November 9, 2010 04:14 — forked from defunkt/browser
#!/bin/sh -e
#
# Usage: browser
# pipe html to a browser
# e.g.
# $ echo "<h1>hi mom!</h1>" | browser
# $ ron -5 man/rip.5.ron | browser
if [ -t 0 ]; then
if [ -n "$1" ]; then
// for comprehension
scala> for {
| l <- List(2,5,10)
| m <- List(8,10,11)
| } yield l * m
res17: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
// map a pure function into applicatives
scala> List(8,10,11) <*> (List(2,5,10) map (((_: Int) * (_: Int)).curried))
res18: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
@softprops
softprops / cookies.js
Created November 22, 2010 22:55
mini js cookie interface
var Cookies = function() {
return {
set: function (name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
},
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
// pattern matching
scala> l match {
| case s if s.forall(even(_)) => Some(l)
| case _ => None
| }
res37: Option[List[Int]] = None
@wrwills
wrwills / LensTest.scala
Created November 30, 2010 13:47
playing with some of the ideas from the Haskell fclabels library using the new Lens functionality in scalaz
import scalaz._
/**
* playing with some of the ideas from the Haskell fclabels library
* using the new Lens functionality in scalaz
*
* http://hackage.haskell.org/package/fclabels
*/
object LensTest {
@oxbowlakes
oxbowlakes / range.scala
Created December 6, 2010 22:28
Range class for ranges [a, b], (a, b), (a, b], [a, b) where (a and b) may be unbounded
package test
import scalaz._
import Scalaz._
import test.IRange.Increment
sealed trait Limit[+Z] {
def open : Boolean
def bound : Option[Z]
final def closed = !open
//Usage:
//override def pomPostProcess(node: Node): Node = mcPom(moduleConfigurations)(super.pomPostProcess(node))
trait McPom { self: DefaultProject =>
import scala.xml._
def mcPom(mcs: Set[ModuleConfiguration])(node: Node): Node = {
//make sure we have a trailing slash so we deduplicate URLs properly
def cleanUrl(url: String) = url match {
case null => ""