Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / cond.rs
Last active May 27, 2018 20:58
Simple lisp-style cond blocks for chaining if-statements in rust without the boilerplate of writing if else if else if
macro_rules! cond {
($($e:expr => $body:expr),+, _ => $default:expr) => (
$(if $e { $body } else)+ { $default }
);
($e:expr => $body:expr) => ( if $e { $body })
}
fn main() {
let age = 25;
@ElectricCoffee
ElectricCoffee / pick.js
Last active December 25, 2017 14:58
A function similar to Python's array.pop(n). Give it an index, and it'll pick out the value stored within, removing it from the array
Array.prototype.pick = function(index) {
let element = this.splice(index, 1)
if (element == null || element.length == 0) {
return undefined
}
return element[0]
}
@ElectricCoffee
ElectricCoffee / context.hs
Last active November 15, 2017 22:54
This is an exploration of two different ways of keeping track of state in Haskell. One uses the State monad, the other doesn't
import Control.Monad.Trans.State
data Expression = Var Char
| Const Int
| Expression :+: Expression
| Int :*: Expression
deriving (Eq, Ord, Show)
type Context = State (Maybe Expression)
show() {
for file in $(find .); do
if [[ $file =~ $1 || $file == $1 ]]; then
echo $file
fi
done
}
cleanup() {
count=0
filename=$1
for file in $(find .); do
if [ -f $file ] && [[ $file =~ $filename || $file == $filename ]]; then
let "count++"
fi
done
@ElectricCoffee
ElectricCoffee / vigenere.js
Last active August 19, 2017 22:44
Simple vigenere ciphering script for the hell of it.
// modulo operation that behaves correctly with negative numbers
function mod(n, m) {
return ((n % m) + m) % m;
}
// converts a character to its digit representation
// same as indexOf, but more succinct
function toDigit(char, alphabet) {
return alphabet.indexOf(char)
}
@ElectricCoffee
ElectricCoffee / conditional-tree-formatting.rs
Created December 20, 2016 09:56
an attempt at making a tree which leaves out leaf nodes when printing. it uses the box keyword for pattern matching on boxes
#![feature(box_patterns)]
use std::fmt;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone)]
enum Tree<T: fmt::Display> {
Leaf,
Node { data: T, left: Box<Tree<T>>, right: Box<Tree<T>> },
}
impl<T: fmt::Display> fmt::Display for Tree<T> {
@ElectricCoffee
ElectricCoffee / touch-cpp.sh
Last active October 6, 2016 06:08
I had to make a bunch of C++ files, and couldn't be bothered to make all the cpp/hpp pairs, so instead I wrote a script that does it for me! It also makes sure not to overwrite any files that already exist
function touch-cpp {
for file in "$@"
do
cpp="$file.cpp"
hpp="$file.hpp"
# only actually attempt to write the files if they don't exist.
if [ -f $cpp -o -f $hpp ]
then
echo ">> Either $cpp or $hpp already exist, no action taken."
@ElectricCoffee
ElectricCoffee / CityNameGenerator.scala
Last active March 9, 2016 11:51
A simple idea for a city name generator I came up with based on Ben "Yahtzee" Croshaw's idea in The Consuming Shadow. The basic premise is that it takes common city prefixes and suffixes and mashes them together at random, causing generic sounding city names. The fun bit is when you start mixing cities from different countries together!
package eu.wausoft.city.gen
import scala.util.Random
import java.io._
/**
* Created by coffee on 3/8/16.
*/
object Generator {
val prefixes = Vector (
"Alt", "Alten", "Olden", "Groß", "Großen", "Hoh", "Hohen",
@ElectricCoffee
ElectricCoffee / lex.scala
Last active February 3, 2016 10:48
Primitive tokenizer in Scala
package eu.wausoft.lex
import java.lang.{String => JString} // renames the built-in string to JString to avoid token clashing
trait Token
case class String(value: JString) extends Token
case class Number(value: JString) extends Token
case class Atom(value: JString) extends Token
case class Keyword(value: JString) extends Token
case object Unknown extends Token