Skip to content

Instantly share code, notes, and snippets.

View ScottKaye's full-sized avatar
🌎

Scott Kaye ScottKaye

🌎
View GitHub Profile
@ScottKaye
ScottKaye / TrimFileLines.cs
Created September 22, 2018 01:35
Reads X lines from a file, stopping each line after Y characters.
void Main()
{
var path = @"R:\test.txt";
const int MAX_LINE_LENGTH = 15;
const int MAX_LINE_COUNT = 15;
using (var stream = GetTruncatedFile(path, MAX_LINE_LENGTH, MAX_LINE_COUNT))
using (var reader = new StreamReader(stream))
{
void Main()
{
foreach (var file in Directory.GetFiles(@"X:\Scott\Projects\Test XML Files", "*.xml"))
{
var doc = XDocument.Load(file);
var expando = XmlToDict.Convert(doc.Root.Elements(), new LibertyTransformer());
expando.Dump(20);
JsonConvert.SerializeObject(expando, Newtonsoft.Json.Formatting.Indented).Dump();
void Main()
{
var bytes = new byte[] { 0, 0, 1, 0, 255, 64 };
var reader = new ByteReader(bytes);
reader.Read<int>().Dump();
reader.Read<byte>().Dump();
reader.Read<sbyte>().Dump();
reader.Read<byte>(4).Dump();
}
public static class Benchmark
{
/// <summary>
/// Returns the total number of milliseconds elapsed running a function [iterations] times.
/// </summary>
/// <see cref="http://stackoverflow.com/a/1048708/382456" />
public static double Run(int iterations, Action func)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
void Main()
{
string input = Console.ReadLine();
input.EatHorror("abcdefg");
input.Dump("Eaten");
(input + " - Appended").Dump("Fix?");
"hello".Dump("Try entering \"hello\"");
}
/// <summary>
/// An in-memory repository of T.
/// </summary>
/// <typeparam name="T">Type of entities to emulate.</typeparam>
public class FakeRepository<T> : IRepository<T>
where T : class
{
private readonly PropertyInfo _key;
private readonly Dictionary<int, T> _entities;
"use strict";
const vm = require("vm");
const util = require("util");
const Constants = require("../constants");
let helpText = `you can evaluate **JS code and expressions** with \`!eval\`!
:information_source: Just use \`!eval <code>\`.
@ScottKaye
ScottKaye / wordrate.js
Last active May 1, 2016 04:21
Returns a value between 0 and 1 representing how often a word appears in a string.
// Full function
// Returns a value between 0 and 1 representing how often a word appears in a string
function wordRate(word, str) {
return str.split(word).filter(Boolean).length / str.split(" ").length;
}
// Golfed/minified, 58 bytes
const w = (w,s)=>s.split(w).filter(Boolean).length/s.split` `.length
// Usage
@ScottKaye
ScottKaye / getParams.js
Last active May 1, 2016 04:24
Getting the querystring into an array throughout the ages
// ES3
var getParams = function() {
var results = [], parts = window.location.search.slice(1).split("&"), i, len;
for (i = 0, len = parts.length; i < len; ++i) results.push(parts[i].split("="));
return results;
};
console.log(getParams());
// ES6
const getParams = () => window.location.search.slice(1).split("&").map(q => q.split("="));
@ScottKaye
ScottKaye / Preferences.sublime-settings
Last active April 1, 2016 03:20
Because Sublime keeps forgetting this fscking file, I'm saving it here.
{
"always_show_minimap_viewport": true,
"bold_folder_labels": true,
"caret_extra_bottom": 1,
"caret_extra_top": 1,
"caret_extra_width": 1,
"caret_style": "phase",
"color_scheme": "Packages/User/SublimeLinter/Oceanic Next (SL).tmTheme",
"drag_text": false,
"draw_minimap_border": true,