Skip to content

Instantly share code, notes, and snippets.

View gdejohn's full-sized avatar

Griffin DeJohn gdejohn

View GitHub Profile
@gdejohn
gdejohn / fib.hs
Last active March 15, 2018 04:41
Point-free nth Fibonacci number
import Control.Arrow ((&&&))
fib = fst . (iterate (snd &&& uncurry (+)) (0, 1) !!)
shared interface Tree<out Element> of EmptyTree | NonemptyTree<Element>
satisfies Collection<Element> given Element satisfies Object {
shared formal Element? element;
shared formal [NonemptyTree<Element>*] children;
clone() => this;
}
import ceylon.math.float {random}
{Integer*}? sample(Integer k, Range<Integer> range) {
value lowerBound = smallest(range.first, range.last);
return 0 <= k <= range.size then unfold([0, 0],
([Integer, Integer] seed) {
value [chosen, visited] = seed;
for (i in visited..(range.size)) {
if (random() < (k - chosen).float / (range.size - i)) {
return [i + lowerBound, [chosen + 1, i + 1]];
import ceylon.collection {ArrayList}
import ceylon.math.float {random}
shared void run() {
value brackets = String(shuffle("[]".repeat(3)));
print(brackets + " " + (balanced(brackets) then "OK" else "NOT OK"));
}
Boolean balanced(String brackets)
=> brackets.scan(0)((x, y) => x + (y == '[' then 1 else -1)).every(0.notLargerThan);
{Value+}(*Arguments) applyAll<out Value, in Arguments>(Value(*Arguments)+ functions)
given Arguments satisfies Anything[]
=> flatten(functions.spread(curry(apply<Value, Arguments>)));
choose :: Int -> [a] -> [[a]]
choose 0 _ = [[]]
choose _ [] = []
choose k (x:xs) | k > 0 = map (x:) (choose (k - 1) xs) ++ choose k xs
| otherwise = []
@gdejohn
gdejohn / Poker.hs
Last active February 28, 2024 12:32
Calculate exact equity for Texas hold 'em by exhaustive enumeration.
module Poker (Rank(..), Suit(..), Card, rank, suit, Hand, hand, equity) where
import Control.Applicative ((<**>))
import Data.Function (on)
import Data.List ((\\), foldl1', group, sortOn)
import Data.Ord (Down(Down))
import Data.Ratio ((%))
data Rank = Two
| Three
@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.
@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 . (>))
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 [];