Skip to content

Instantly share code, notes, and snippets.

View ramn's full-sized avatar

ramn ramn

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / ScalaReplJail.scala
Created October 11, 2013 17:23
Run scala app continuously with hidden break out
SECRET_BREAKOUT_RETURN_CODE=119
while scala; [ $? != $SECRET_BREAKOUT_RETURN_CODE ] ; do continue; done
@ramn
ramn / zeromq_demo_publisher.py
Last active August 24, 2022 21:52
Python ZeroMQ pub/sub example
import time.sleep
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')
# Allow clients to connect before sending data
sleep(10)
socket.send_pyobj({1:[1,2,3]})
@ramn
ramn / sockets_as_devices_in_bash.sh
Created October 29, 2013 16:18
Sockets as devices in Bash
exec 4<>/dev/tcp/gopher.meulie.net/70
echo "" >&4
cat <&4
@ramn
ramn / selfdestructing_tempdir.py
Created November 11, 2013 14:30
Selfdestructing tempdir (Python)
class selfdestructing_tempdir(object):
def __enter__(self):
import tempfile
self._tempdir = tempfile.mkdtemp()
return self._tempdir
def __exit__(self, type, value, traceback):
import shutil
shutil.rmtree(self._tempdir)