Skip to content

Instantly share code, notes, and snippets.

View gdejohn's full-sized avatar

Griffin DeJohn gdejohn

View GitHub Profile
@gdejohn
gdejohn / Card.java
Last active February 27, 2023 21:29
straightforward five-card poker hand comparison
package poker;
public record Card(Rank rank, Suit suit) {}
@gdejohn
gdejohn / State.ceylon
Last active January 3, 2019 01:05
Deterministic finite automaton
"A state in a deterministic finite automaton."
shared interface State<in Symbol> {
"Returns [[true]] if this is an accept state, else [[false]]."
shared default Boolean accepting => false;
"Returns the next state given the next [[symbol]]."
shared default State<Symbol> transition(Symbol symbol) => trap;
"Returns [[true]] if the given [[string]] is accepted by the implicit DFA
starting at this state, else [[false]]. Accepted strings are words in the
@gdejohn
gdejohn / montePi.ceylon
Last active August 29, 2015 14:15
Monte Carlo approximation of π
Float montePi(Integer samples) => 4.0 * count({random() ^ 2 + random() ^ 2 <= 1.0}.repeat(samples)) / samples;

Keybase proof

I hereby claim:

  • I am gdejohn on github.
  • I am gdejohn (https://keybase.io/gdejohn) on keybase.
  • I have a public key whose fingerprint is 3FC3 DC5C BBCE 6B36 141A EC79 CB0E 1AE7 A8E6 DFF4

To claim this, I am signing this object:

import static java.util.Map.entry;
import static java.util.stream.IntStream.range;
import java.util.Map.Entry;
import java.util.stream.Stream;
public class FizzBuzz {
public static void main(String[] args) {
range(1, 100).mapToObj(
i -> Stream.of(entry(3, "Fizz"), entry(5, "Buzz")).filter(
@gdejohn
gdejohn / fixpoint.ceylon
Last active December 22, 2018 23:44
Calculates the fixed point of the cosine function: x such that cos(x) == x
assert (exists x = loop(1.0)(cos).paired.find(([x, y]) => x == y)?.first);
@gdejohn
gdejohn / fibonacci.ceylon
Last active February 28, 2017 11:06
Lazy, infinite, corecursive Fibonacci sequence
value fibonacci = loop([0, 1])(([x, y]) => [y, x + y]).map(Tuple.first);
Element[] quicksort<Element>({Element*} elements)
given Element satisfies Comparable<Element> =>
if (exists first = elements.first)
then [first.largerThan, first.notLargerThan]
.map(compose(quicksort<Element>, elements.rest.filter))
.interpose([first])
.reduce(concatenate<Element>)
else [];
@gdejohn
gdejohn / sort.hs
Last active April 11, 2017 14:04
Deforested tree sort, point-free one-liner
import Control.Applicative (liftA2)
import Data.List (partition)
import Data.List.Extra (list)
import Data.Tuple.Extra (both, second)
sort :: Ord a => [a] -> [a]
sort = [] `list` liftA2 (.) ((uncurry (++) .) . second . (:)) ((both sort .) . partition . (>))
@gdejohn
gdejohn / DFA.hs
Last active October 7, 2015 22:47
import Data.List (find)
-- A state in a deterministic finite automaton.
data State a = State Accept (a -> State a) | Trap
-- Indicates whether a state is an accept state.
data Accept = Accept | Reject
-- Determines whether the implicit DFA starting at the given state recognizes
-- the given string.