Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / i32_vec.c
Created September 19, 2018 10:05
A simple "for the hell of it" implementation of a dynamically resizing array in C
//
// i32_vec.c
// vector_idea
//
// Created by Nikolaj Lepka on 2018-09-19.
// Copyright © 2018 wausoft.eu. All rights reserved.
//
#include "i32_vec.h"
/**
@ElectricCoffee
ElectricCoffee / phantom.rs
Created September 19, 2018 17:36
A test of phantom types in Rust. A bit more cumbersome than Haskell, but definitely doable.
use std::marker::PhantomData;
use std::convert::From;
// Define the empty temperature markers
#[derive(Debug, Copy, Clone)] struct C;
#[derive(Debug, Copy, Clone)] struct F;
// Temperature Struct with Phantom Data
#[derive(Debug, Copy, Clone)]
struct Temp<T> {
use std::marker::PhantomData;
use std::convert::From;
#[derive(Debug, Clone, Copy)]
struct H;
#[derive(Debug, Clone, Copy)]
struct L;
#[derive(Debug, Clone, Copy)]
struct Sec<S, A> {
@ElectricCoffee
ElectricCoffee / prompt.rs
Last active September 28, 2018 18:29
provides a simple python-esque prompt
use std::io::{self, Write};
fn prompt(input: &str) -> io::Result<String> {
let mut output = String::new();
print!("{}", input);
io::stdout().flush()?;
io::stdin().read_line(&mut output)?;
Ok(output)
}
@ElectricCoffee
ElectricCoffee / vector2d.rs
Last active October 17, 2018 17:30
Vector2D struct in the style of Unity's Vector2
use std::f32;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Vector2D {
pub x: f32,
pub y: f32,
}
impl Default for Vector2D {
fn default() -> Self {
/** Python-style range function */
function range(n) {
let arr = new Array(n);
let result = [...arr.keys()].map(m => Number(m));
return result;
}
@ElectricCoffee
ElectricCoffee / grokking-algorithms-1.rs
Last active November 25, 2018 14:54
Rust implementations of all the algorithms found in the book "Grokking Algorithms"
//! Chapter 1: Introduction to Algorithms. Binary Search.
// NB: input must be sorted
fn binary_search<T>(lst: &[T], item: &T) -> Option<usize>
where T: PartialEq + PartialOrd
{
let mut low = 0;
let mut high = lst.len() - 1;
while low <= high {
@ElectricCoffee
ElectricCoffee / mkcpp.rb
Last active April 16, 2019 12:46
tool for generating cpp and hpp files
#! /usr/bin/ruby
# Somewhat counter-intuitively, Ruby's ARGV doesn't include the file name,
# so the vector is 1 shorter than normal.
# Typically writing ./mkcpp.rb foo would give a vector like ARGV = ["mkcpp.rb", "foo"], but not here.
if ARGV.length < 1 then
puts "Please provide a sufficient number of arguments"
exit
end
@ElectricCoffee
ElectricCoffee / cleanup.rb
Last active April 16, 2019 14:34
ruby implementation of my old cleanup.bash file.
#! /usr/bin/ruby
require './prompt'
# Exit early if a file name hasn't been provided
if ARGV.length < 1 then
puts "Please provide an identifier"
exit
end
# Gets all the strings that include the provided identifier
@ElectricCoffee
ElectricCoffee / razzle_dazzle.rb
Last active April 20, 2019 23:56
Inspired by the Razzle Dazzle ScamNation video on YouTube: https://www.youtube.com/watch?v=527F51qTcTg This models the dice version of the game.
# Inspired by the Razzle Dazzle scam on YouTube: https://www.youtube.com/watch?v=527F51qTcTg
# Note that this always assumes fair play
##
# Keeps track of the outcome of any given dice roll
# +points+ is how many points a given field is worth
# +prize+ is how many prizes are being added to your current pool of prizes
# +cost_mult+ is the cost multiplier of a given spot
class Outcome
attr_reader :points, :prize, :cost_mult