Skip to content

Instantly share code, notes, and snippets.

View betandr's full-sized avatar
🦄
Vague, but exciting...

Beth Anderson betandr

🦄
Vague, but exciting...
View GitHub Profile
@betandr
betandr / Lists
Last active August 29, 2015 14:16
Scala: Lists
object Lists {
val emptyList = List()
val otherEmptyList = Nil
val items = List("foo", "bar", "baz")
val consStyleItems = "foo" :: "bar" :: "baz" :: Nil // cons prepend to front of Nil list
val concatStyleItems = List("foo", "bar") ::: List("baz")
items(2) // returns 'baz'
items.count(s => s.length == 3) // counts number of items with length of three chars
@betandr
betandr / Loops
Created March 6, 2015 16:20
Scala: Loops
object Loops {
val items = Array("Hello", ", ", "World!")
val moreitems = Array.apply("Hello", ", ", "World!")
items.foreach(arg => println(arg))
items.foreach((arg: String) => println(arg))
val greetStrings = new Array[String](3)
@betandr
betandr / Functional
Created March 6, 2015 16:22
Scala: Functional
object Functional {
def printItemsImperative(items: Array[String]): Unit = {
var i = 0
while (i < items.length) {
println(items(i))
i += 1
}

Although you may have great version control in place, sometimes stuff gets inadvertently overwritten or messed up…in this case it's always good to do a quick backup before you do anything potentially serious. I've written a little Bash shell hack which I thought might be useful:

In your ~/.bash_profile (or ~/.bashrc if you load that in your .bash_profile) file, add the line:

alias bag='b=$(basename `pwd`);cd ..;tar -cvf  $b"_"$(date +'%Y-%m-%d_%H-%M-%S')".tar.gz" "./"$b"/";cd $b'

...then re-load your bashrc by running 'source ~/.bash_profile' or 'source ~/.bashrc'.

When you're then in your working directory, simply run 'bag' which will create a time-stamped, gzipped tar file in the directory above the one you're in, containing the contents of the directory you're currently in. Such as:

@betandr
betandr / image_pickle.rb
Created June 29, 2015 18:42
Pickle an image to a string, then unpickle it and display it in a terminal
#!/usr/bin/env ruby
require "tco"
require "rmagick"
require "json"
include Magick
class PickledImage
attr_accessor :rows
@betandr
betandr / RadioHomepageServicesSimulation.scala
Last active August 29, 2015 14:24
Radio Homepage Services Simulation
package bbc
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class RadioHomepageServicesSimulation extends Simulation {
val httpConf = http
.baseURL("http://www.bbc.co.uk")
@betandr
betandr / Roulette.java
Created July 10, 2015 11:58
Genetic algorithm Roulette selection
public class Roulette {
/* program n_select=1000 times selects one of n=4 elements with weights weight[i].
* Selections are summed up in counter[i]. For the weights as given in the example
* below one expects that elements 0,1,2 and 3 will be selected (on average)
* 200, 150, 600 and 50 times, respectively. In good agreement with exemplary run.
*/
public static void main(String [] args) {
int n=4;
double weight[] = new double [n];
@betandr
betandr / listrecursion.scala
Created August 13, 2015 16:21
Scala List recursion
// basic recursion; will not handle large Lists
def sum(nums: List[Int]): Int = ints match {
case Nil => 0
case x :: tail => x + sum(tail)
}
// tail recursion
def sum2(nums: List[Int]): Int = {
@betandr
betandr / fvi.scala
Created August 13, 2015 16:23
Functional vs Imperative
object Imperative {
def run(x: Int): Int = {
var n = x
while(n < 20){
n = n + 1
}
n
@betandr
betandr / CacheControlHeaderFinder.scala
Last active August 26, 2015 13:12
Recursive find of a value in a Cache-Control HTTP header value string
def find(key: String, in: String): scala.util.Try[Int] = {
scala.util.Try {
def findValue(items: Array[String]): String = {
val item = items.head
if (item.contains(key)) { item.split("=").last }
else { findValue(items.drop(1)) }
}
findValue(in.split(", ")).toInt
}
}