Skip to content

Instantly share code, notes, and snippets.

View MajickTek's full-sized avatar

Majick Tek MajickTek

  • Majick Tek
  • The Internet
View GitHub Profile
@MajickTek
MajickTek / ranom_walk.pde
Created April 2, 2024 08:03
A simple "terrain" generator using a random walk algorithm, in Processing (Java)
//adapted from https://codereview.stackexchange.com/questions/64398/random-walk-terrain-generator
int blockX = 200;
int blockY = 200;
int sizeX = 2;
int sizeY = 2;
int minX = 0;
int maxX=400;
@MajickTek
MajickTek / GenericArray.java
Created March 13, 2024 08:54
Proof of concept for typesafe generic arrays in Java
@FunctionalInterface
interface ArraySupplier<E> {
E[] get(int length);
}
class GenericArray<E> {
private final ArraySupplier<E> supplier;
private E[] array;
GenericArray(ArraySupplier<E> supplier) {
@MajickTek
MajickTek / fastdeleter.bat
Created March 9, 2024 08:21
A fast way to delete large folders on Windows
rem just drag the large folder onto this bat file, or manually supply it as an argument
del /f/s/q %1 > nul
rmdir /s/q %1
@MajickTek
MajickTek / Event.java
Last active March 2, 2024 18:10
Minimal Event Bus in Java
public interface Event {
EventBus getEventBus();
void setEventBus(EventBus eventBus);
}
@MajickTek
MajickTek / tilt.js
Created February 28, 2024 08:37
Tilt 3D
//Add this as a bookmark
//javascript:void function(b,p)%7Bfunction l(k,c,b,e,g,d,f)%7Breturn"<div style%3D%27position:absolute%3B-webkit-transform-origin: 0 0 0%3B"%2B("background:"%2Bf%2B"%3B")%2B("width:"%2Be%2B"px%3B height:"%2Bg%2B"px%3B")%2B("-webkit-transform:"%2B("translate3d("%2Bk%2B"px,"%2Bc%2B"px,"%2Bb%2B"px)")%2B("rotateX(270deg) rotateY("%2Bd%2B"deg)")%2B"%3B")%2B"%27></div>"%7Dfunction o(k,c,d,f)%7Bfor(var j%3Dk.childNodes,n%3Dj.length,m%3D0%3Bm<n%3Bm%2B%2B)%7Bvar a%3Dj%5Bm%5D%3Bif(1%3D%3D%3Da.nodeType)%7Ba.style.overflow%3D"visible"%3Ba.style.WebkitTransformStyle%3D"preserve-3d"%3Ba.style.WebkitTransform%3D"translateZ("%2B(b%2B(n-m)*q).toFixed(3)%2B"px)"%3Bvar h%3Dd,i%3Df%3Ba.offsetParent%3D%3D%3Dk%26%26(h%2B%3Dk.offsetLeft,i%2B%3Dk.offsetTop)%3Bo(a,c%2B1,h,i)%3Be%2B%3Dl(h%2Ba.offsetLeft,i%2Ba.offsetTop,(c%2B1)*b,a.offsetWidth,b,0,g%5Bc%25(g.length-1)%5D)%3Be%2B%3Dl(h%2Ba.offsetLeft%2Ba.offsetWidth,i%2Ba.offsetTop,(c%2B1)*b,a.offsetHeight,b,270,g%5Bc%25(g.length-1)%5D)%3Be%2B%3Dl(h%2Ba.offsetLe
@MajickTek
MajickTek / Observable.java
Created January 24, 2024 06:38
Observer Pattern
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/*
* Slightly improved over the original JDK implementation.
* Still not as flexible as some other options
*/
public class Observable {
private boolean changed=false;
import java.util.function.Function;
// https://en.wikipedia.org/wiki/Result_type
public sealed interface Result<LEFT, RIGHT> permits Result.Success, Result.Failure {
// Both functions have a common result supertype
// e.g. `T` can be a `Result<X,Y>` or a resolved type like a `String` / `Request`
<T, L2 extends T, R2 extends T> T either(Function<LEFT, L2> left, Function<RIGHT, R2> right);
default <T> T then(Function<Result<LEFT, RIGHT>, T> function) {
@MajickTek
MajickTek / Cycler.java
Last active December 2, 2023 00:00
This is an example of a "cycler", or loopable iterator. Tested in Java17 but should work in any version 8+
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Cycler<E> implements Iterable<E> {
final List<? extends E> list;
@MajickTek
MajickTek / Option.java
Last active September 21, 2023 02:23
Option monad implementation in Java
import java.util.Objects;
import java.util.stream.Stream;
/**
Example usage:
Option<String> original = Option.some("hello, world!");
String finalstr = original.filter(s -> s.contains(",")).map(s -> s.replace(",", "")).reduce("the input string was null");
System.out.println(finalstr); // should print "hello world!"
**/
@MajickTek
MajickTek / BackwardsBoundedRange.java
Last active June 17, 2023 07:56
Some range classes in Java
import java.util.*;
public class BackwardsBoundedRange implements Iterable<Integer>{
private int end, limit;
public BackwardsBoundedRange(int end, int limit) {
if(end > limit) throw new UnsupportedOperationException("BackwardsBoundedRange: end cannot be larger than limit");
this.end = end;
this.limit = limit;
}