Skip to content

Instantly share code, notes, and snippets.

View marksto's full-sized avatar
🎷
🎼🎶🎵

Mark Sto marksto

🎷
🎼🎶🎵
View GitHub Profile
@marksto
marksto / TailCallJava8.java
Last active May 3, 2021 14:08
Tail call recursion for Java 8. And although you may call it trampolining, it does the job of a factorial calculation in Java 8. (One may also implement this in good ol' Java 6 with anonymous local classes. This should be less memory-efficient and this won't leverage the 'java.lang.invoke' package at all. Nonetheless, it will do the job.)
package marksto.lang;
import java.math.BigInteger;
import java.util.stream.Stream;
public class TailCallJava8 {
@FunctionalInterface
public interface TailCall<T> {
@marksto
marksto / GeneralizedBiDiList.java
Last active July 16, 2019 15:21
Reverse a generalized version of bidirectional list (with 'aux, not just 'previous') in Java.
package marksto.lang;
import java.util.UUID;
public class GeneralizedBiDiList {
static class Node {
String listId;
int value;
Node next;
@marksto
marksto / BrickPileProblem.java
Last active July 25, 2019 04:04
Brick pile (pyramid) problem solvers.
package marksto.algorithms;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import java.util.HashMap;
@marksto
marksto / RecursiveBrickPileSolver.java
Created July 17, 2019 23:26
The efficient recursive solution extracted from the BrickPileProblem.java (https://gist.github.com/marksto/ec0fb1d727f31b7b6db7cf7fed97f56e).
private static class RecursiveBrickPileSolver extends BrickPileSolver {
// NOTE: The 'ConcurrentHashMap' implementation should be used instead
// in case this object is to be shared between multiple threads.
final Map<Integer, double[]> cache = new HashMap<>();
public RecursiveBrickPileSolver(double brickMass) {
super(brickMass);
}
@marksto
marksto / profiles.clj
Last active July 10, 2022 17:04
My local ~/.lein/profiles.clj
{:debug {:debug true
:dependencies [[org.clojure/tools.namespace "0.2.11"]
[clj-commons/spyscope "0.1.48"]]
:injections [(require '(clojure.tools.namespace repl find))
; try/catch to workaround an issue where `lein repl` outside a project dir
; will not load reader literal definitions correctly:
(try (require '[spyscope.core :as spy])
(catch RuntimeException e))
(prn (into {} (System/getProperties)))]}
@marksto
marksto / cljs-debug-macros.clj
Created April 11, 2021 10:29 — forked from michaelsbradleyjr/cljs-debug-macros.clj
Macros and functions which facilitate the development and debugging of other macros and their supporting functions within a ClojureScript project.
(ns my-cljs.macros.debug
(:require [cljs.analyzer :as cljs]
clojure.walk))
(declare ap
cljs-macroexpand*
cljs-macroexpand-1*
cljs-macroexpand-all*
cljs-macroexpand
@marksto
marksto / fizz-buzz.cljc
Last active January 23, 2024 14:13
The One and Only Truely Functional FizzBuzz (in Clojure)
(ns fizz-buzz
"Approaching the infamous 'FizzBuzz' code problem with cyclic sequences and declarative rules")
;; SOLUTION
;; Core ideas:
;; 1. Note that the logic is essentially sequential and cyclical.
;; 2. Delay the actual computation (execution) by opting for lazy sequences.
;; 3. Separate control flow and logic, extracting the latter into a ruleset data structure.