Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@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 / 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 / .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 / 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 / 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 / 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 / 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 / superliga-eksamen-nikolaj.c
Created December 5, 2014 12:06
my first semester final exam program in imperative programming
/* Navn: Nikolaj Lepka
Email: [email protected]
Gruppe: A425b
Studieretning: Datalogi */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DAYSIZE 4
#define TEAMSIZE 4
@ElectricCoffee
ElectricCoffee / list-example.c
Last active August 29, 2015 14:11
Macros for easy generic creation of lists in C
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
DEFINE_LIST(int); /* defines the int_list type */
int main(void) {
int_list *lst = EMPTY;
PUSH(8, &lst, int_list);
PUSH(4, &lst, int_list);
@ElectricCoffee
ElectricCoffee / Pair.cs
Created January 21, 2015 23:20
make-shift Tuple class for .NET 3.5 which doesn't include it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Utilities
{
static class Pair
{
public static Pair<T1, T2> Make<T1, T2>(T1 fst, T2 snd)