Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@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 / 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)
}
cleanup() {
count=0
filename=$1
for file in $(find .); do
if [ -f $file ] && [[ $file =~ $filename || $file == $filename ]]; then
let "count++"
fi
done
show() {
for file in $(find .); do
if [[ $file =~ $1 || $file == $1 ]]; then
echo $file
fi
done
}
@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)
@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 / 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;
// Rudimentary implementation of the Collatz conjecture
fn next(input: u128) -> u128 {
if input % 2 == 0 {
input / 2
} else {
input * 3 + 1
}
}
extern crate rand;
use rand::Rng;
use std::env;
const MAX_DAYS: usize = 365;
/// Creates an array with `count` number of birthdays in it represented by numbers
/// if more than one birthday occurs on the same day, then that day's entry will be > 1
fn paradox(count: u16) -> [u16; MAX_DAYS] {
let mut arr = [0; MAX_DAYS];
@ElectricCoffee
ElectricCoffee / mmclock.css
Last active June 13, 2018 19:21
The Majora's Mask screen that appears whenever you start a new day
html {
font-family: "Arial Narrow", sans-serif
}
div {
font-size: 200%;
padding: 50px;
background-color: black;
color: white;
height: 1000px;