Skip to content

Instantly share code, notes, and snippets.

View DarkSeraphim's full-sized avatar

Mark Hendriks DarkSeraphim

View GitHub Profile
Move right and request number
>
Get char
,---- ---- --
[
++++++++++
>[-]>[-]>[-]<<< Reset next two slots
> ++ ++++ ++++ <
<[->>>+<<<] Copy number to next two slots
>>
@DarkSeraphim
DarkSeraphim / snake.hs
Created January 28, 2019 16:08
Partial implementation of snake logic in Haskell
move :: [(Int, Int)] -> Int -> Int -> [(Int, Int)]
move snake x y =
let (hx, hy) = head snake
nx = hx + x
ny = hy + y
n = (nx, ny)
in (n : (init snake), last snake)
isWall' :: Int -> Int -> (Int, Int) -> Bool
isWall w h (x, y)
@DarkSeraphim
DarkSeraphim / index.js
Last active January 18, 2019 14:13
Node.js destructuring vs proxy for object combinations
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
function createObject(start, end, smallData) {
let obj = {};
for (let cur = start; cur < end; cur++) {
obj['prop' + cur] = 'value' + cur;
}
return obj;
}
@DarkSeraphim
DarkSeraphim / emojis.js
Created November 12, 2018 22:34
Utility to map Discord unicode emojis from a tag to their unicode character
const discordEmoji = require("discord-emoji")
module.exports = discordEmoji;
module.exports.emojis =
Object.values(discordEmoji)
.reduce((map, emojis) => {
return Object.entries(emojis).reduce((map, [name, unicode]) => {
return map.set(name, unicode);
}, map);
}, new Map());
class Interruptable {
constructor() {
this.cancelled = false;
}
cancel() {
this.cancelled = true;
if (this._timer) {
clearTimeout(this._timer);
if (!this._reject) {
@DarkSeraphim
DarkSeraphim / Config.java
Created April 9, 2018 12:56
Configuration file
public class Config extends YamlConfiguration {
private final File file;
public Config(String name)
throws IOException, InvalidConfigurationException {
this.file = new File(name);
load(file);
loadDefaults(file.toPath());
}
4603 is my real discriminator
@DarkSeraphim
DarkSeraphim / ReflectionResolver.java
Last active March 31, 2018 17:57
ReflectionResolver fanciness
public class ReflectionResolver {
public static Field resolveField(Class<?> cls, Predicate<Field> filter) {
resolveField(cls, filter, ONE_OR_ERROR);
}
public static Method resolveMethod(Class<?> cls, Predicate<Method> filter) {
resolveMethod(cls, filter, ONE_OR_ERROR);
}
@DarkSeraphim
DarkSeraphim / FileManagement.md
Created March 5, 2018 01:25
File management

File management with node.js

Files, they're used as a basic persistence layer by a lot of people, but it often gets fuzzy when they're accessed in multiple different places.

The problem

Let's take a simple bank implementation as example, where we have one file getting our balance, and one file is used for transactions (e.g. payments). Our data is stored in a file called bank.json:

// File: balance.js
// Nice shortcut node! Now I don't need to read my file and parse it manually!
const accounts = require('bank.json');
@DarkSeraphim
DarkSeraphim / PlayerCountPoller.java
Created February 27, 2018 19:55
Grabs the player count (on request), and allows outside access through a public getter.
public class PlayerCountPoller implements PluginMessageListener {
private int count;
public void requestUpdate() {
List<? extends Player> players = Bukkit.getOnlinePlayers();
if (players.isEmpty()) {
return;
}
Player player = players.get(0); // Just grab the first