Skip to content

Instantly share code, notes, and snippets.

View ramn's full-sized avatar

ramn ramn

View GitHub Profile
@ramn
ramn / view_attachment.sh
Created July 26, 2013 08:06
View attachment from Mutt on MacOSX
#!/bin/bash
#
# Author: Eric Gebhart
#
# Purpose: To be called by mutt as indicated by .mailcap to handle mail attachments.
#
# Function: Copy the given file to a temporary directory so mutt
# Won't delete it before it is read by the application.
#
# Along the way, discern the file type or use the type
@ramn
ramn / show_migrations_stripped.sh
Created July 24, 2013 15:28
Show migrations in upcoming release
git diff current_release..master --name-only -- . | grep migration | xargs -n1 sed '/^\s\+models = /,$d' | view - -c 'set ft=python'
@ramn
ramn / HList.scala
Created July 8, 2013 23:08
HList in Scala (v1 from Scala in depth)
// HList implementation
sealed trait HList
final case class HCons[H, T <: HList](head: H, tail: T) extends HList {
def ::[T](v : T) = HCons(v,this)
override def toString = head + " :: " + tail
}
final class HNil extends HList {
@ramn
ramn / MergeSort.scala
Last active August 15, 2016 23:53
Merge sort in scala
import annotation.tailrec
import scala.math.Ordering.Implicits._
object MergeSort {
def apply[T : Numeric](xs: Seq[T]): Seq[T] = {
val n = xs.length / 2
if (n == 0)
xs
else {
@ramn
ramn / Deserialization.scala
Last active February 26, 2025 11:43
Object serialization example in Scala
import java.io._
@SerialVersionUID(15L)
class Animal(name: String, age: Int) extends Serializable {
override def toString = s"Animal($name, $age)"
}
case class Person(name: String)
// or fork := true in sbt
@ramn
ramn / Times.scala
Created April 30, 2013 22:01
times function in Scala. Do something a certain number of times. Uses type class to choose between Unit and value returning behaviour. Examples: times(5) { print("hi ") } => Unit hi hi hi hi hi times(2) { "a" } => Seq("a", "a")
package object util {
object Times {
trait TimesDoer[-Value, Result] {
def times(n: Int)(f: => Value): Result
}
implicit object TimesUnit extends TimesDoer[Unit, Unit] {
def times(n: Int)(f: => Unit): Unit = for (_ <- 0 until n) f
}
@ramn
ramn / irc_from_bash.sh
Last active May 10, 2017 16:46
IRC client in Bash
# Communicate with an IRC channel from the shell
#
# Runs in background, writes messages to stdout in tty
# Depends on sic
TMPF=ircsession.tmp; CHAN='#testircfrombash'; ME=bot001; cat /dev/null > $TMPF; (tail -f $TMPF | sic -h irc.freenode.net -p 6667 -n $ME | while read MSG; do case "$MSG" in "$ME"*) echo ":j $CHAN" >> $TMPF;; "$CHAN"*) echo "$MSG";; esac; done;) &
# Then, to write to the channel:
echo "Hello all!" >> $TMPF
# or open a buffer to write many rows:
@ramn
ramn / watch_bitcoin_last_price.sh
Created March 26, 2013 10:26
Watch Bitcoin last price (shell)
CMD='last=JSON.load(ARGF.read)["data"]["last"]["display"]; puts "#{Time.now}: #{last}"'; watch "curl https://data.mtgox.com/api/2/BTCUSD/money/ticker 2>/dev/null | ruby -r json -e '$CMD'"
@ramn
ramn / FuturesWithFixedThreadPool.scala
Last active February 19, 2016 09:21
Futures with fixed thread pool
import java.util.concurrent.Executors
import concurrent.ExecutionContext
import concurrent.Future
import concurrent.Await
import scala.concurrent.duration._
implicit val executionContext = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(2))
Await.ready(Future { println("sdf") }, 3.seconds)
@ramn
ramn / S3ObjectUtil.scala
Last active December 11, 2015 15:18
S3 utils in Scala, for listing and fetching S3 objects.
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.