This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Arrow ((&&&)) | |
fib = fst . (iterate (snd &&& uncurry (+)) (0, 1) !!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{Value+}(*Arguments) applyAll<out Value, in Arguments>(Value(*Arguments)+ functions) | |
given Arguments satisfies Anything[] | |
=> flatten(functions.spread(curry(apply<Value, Arguments>))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
choose :: Int -> [a] -> [[a]] | |
choose 0 _ = [[]] | |
choose _ [] = [] | |
choose k (x:xs) | k > 0 = map (x:) (choose (k - 1) xs) ++ choose k xs | |
| otherwise = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . (>)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; |