Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / c-compiler-convenience.sh
Created November 24, 2014 10:29
convenience functions for dealing with c compilation
#!/bin/bash
function compile-c() {
if [ $# -eq 0 ]
then INPUT="*.c"
else INPUT="$1.c"
fi
gcc -ansi -pedantic -Wall -Werror $INPUT -o program
}
@ElectricCoffee
ElectricCoffee / latex-setup.sh
Last active August 29, 2015 14:07
Sets up a latex file with the bare minimum amount of text
function setup-latex() {
if [ $# -eq 0 ]
then FILENAME="default.tex"
else FILENAME="$1.tex"
fi
echo "Creating $FILENAME for you..."
touch $FILENAME
echo "\documentclass[12pt, a4paper]{article}" > $FILENAME
echo "\usepackage[utf8]{inputenc}" >> $FILENAME
@ElectricCoffee
ElectricCoffee / gist:9387e27397a70e1a6d4d
Last active August 29, 2015 14:07 — forked from rand00/gist:e8a382e3aa1b64a53727
OSX Version of rand00's copy function
(defun copy-to-clipboard ()
(interactive)
(if (region-active-p)
(progn (shell-command-on-region
(region-beginning) (region-end)
"pbcopy")
(message "Copied region to clipboard!")
(deactivate-mark))
(message "No mark set.")))
@ElectricCoffee
ElectricCoffee / copy-files.sh
Last active August 29, 2015 14:07
utility function to copy all the contents of a text file from the terminal
#pbcopy only exists on OSX (as far as I know), for linux find an equivalent
function cp-file-contents() {
if [ $# -eq 0 ]
then echo "Missing arguments"
elif [ $# -gt 1 ]
then echo "Too many arguments"
else
cat $1 | pbcopy
echo "contens copied"
fi
@ElectricCoffee
ElectricCoffee / .bashrc
Last active August 29, 2015 14:06
custom string for a fancy bash prompt
# Fancy prompt
# \u = user, \H = hostname, \D = formatted date, \A 24-hour time in hours:minutes
# \w = full path of current working directory, with ~ as home, \$ for # when root and $ when user
# example output:
# ╓[ElectricCoffee]─[coffeebook-pro]─[2014-09-22, 21:58]─[~/Desktop]
# ╙[$]-[
PS1="╓[\u]─[\H]─[\D{%Y-%m-%d}, \A]─[\w]\n╙[\$]-["
@ElectricCoffee
ElectricCoffee / MOption.scala
Created September 9, 2014 22:39
Adds map and flatMap as extension methods to Scala, for those who don't want/need scalaz for their project, is fully compatible with for-comprehensions
package extension.monad
trait Monad[A, M[_]] {
// >>= :: Monad m => m a -> (a -> m b) -> m b
def flatMap[B](input: A => M[B]): M[B] // AKA "bind"
}
trait Functor[A, F[_]] {
// fmap :: Functor f => (a -> b) -> f a -> f b
def map[B](input: A => B): F[B] // AKA "fmap"
@ElectricCoffee
ElectricCoffee / SafeConvert.scala
Created September 6, 2014 14:33
Got a little miffed by the fact that str.toInt threw an exception if a non-convertible string was encountered (a-la "hello".toInt), so I figured it would make more sense if it returned an option instead, using this library you can do this: "hello".opt.toInt which would return None, saving you a tedious try/catch
package com.wausoft.extensions
object SafeString {
class StrOps(input: String) {
private def toOption[A](in: => A) = try {
Some(in)
} catch {
case _: Throwable => None
}
@ElectricCoffee
ElectricCoffee / 0_program.scala
Last active January 4, 2016 13:02
Based on Dr. James Grime's nontransitive dice (aka. "Grime Dice") I set to test if they actually work as advertised, by throwing every possible combination 1000 times and printing the result on-screen. (spoiler alert they work). The Die class works on all dice of all sizes, with any numeric value on the faces
package com.wausoft.core
import com.wausoft.dice.Die
import com.wausoft.dice.Die.Multiple
object Program extends App {
// define the five different Grime Dice
val red = new Die("red" , 4, 4, 4, 4, 4, 9)
val olive = new Die("olive" , 5, 5, 5, 5, 5, 0)
val blue = new Die("blue" , 7, 7, 7, 2, 2, 2)
@ElectricCoffee
ElectricCoffee / then.scala
Last active May 27, 2016 22:58
Lets you call subroutines sequencially without code blocks, more info in the file
/*
* The premise of this is simple; it allows you to chain subroutines without using a block
* The idea comes from Haskell's Monads, where by language design you can't normally
* predict the order of statement execution due to the language being lazy,
* so instead an operator was added that allowed you to call statements sequencially
*
* Rather than writing something like this:
* def printStuff(): Unit = {
* println("blah")
* println("bloo")
@ElectricCoffee
ElectricCoffee / ackermann.scala
Last active August 29, 2015 14:03
An attempt to calculate Ackermann in Scala... It keeps overflowing the stack near Ack(3,11)
package io.wausoft
import org.joda.time.{Seconds, DateTime}
import scala.collection.mutable
case class Ack(m: Int, n: Int)
object Program extends App {
// Basic ackermann without cache, overflows at around Ack(3,11) after a few seconds
def compute(a: Ack): Int = a match {