This file contains hidden or 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 static java.lang.Math.*; // abs, atan2, cos, hypot, sin | |
| /// Since this interface is sealed, the only allowed subtypes are the ones specified | |
| /// below: Cartesian and Polar. | |
| public sealed value interface Coordinates { | |
| /// value classes: flat representation in memory, no identity, has the | |
| /// flexibility of a class but the performance of a primitive type | |
| value record Cartesian(double x, double y) implements Coordinates { | |
| /// Convert these Cartesian coordinates into polar coordinates. | |
| public Polar polar() -> new Polar(hypot(x, y), atan2(y, x)); |
This file contains hidden or 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
| Comparison comparing<in Value>(Comparison(Value, Value)* comparators)(Value x, Value y) | |
| => comparators.spread(identity)(x, y).find(not(equal.equals)) else equal; |
This file contains hidden or 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 java.util.function.Function; | |
| import java.util.function.UnaryOperator; | |
| class Factorial { | |
| static <T, R> Function<T, R> fix(UnaryOperator<Function<T, R>> operator) { | |
| return operator.apply(argument -> fix(operator).apply(argument)); | |
| } | |
| static Function<Integer, Integer> factorial = fix(f -> n -> n == 0 ? 1 : n * f.apply(n - 1)); | |
| } |
This file contains hidden or 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
| -- |Enumerate restricted partitions of a set into a sequence of sets of subsets. | |
| partition :: [(Int, Int)] -- ^ The i_th pair (k, n) specifies the number n of subsets in the i_th set and the size k of those subsets | |
| -> [a] -- ^ The set to partition (the elements are assumed to be pairwise distinct) | |
| -> [([[[a]]], [a])] -- ^ The partitions, each paired with the leftover elements from the original set | |
| partition [] xs = [([], xs)] | |
| partition _ [] = [] | |
| partition [(0, 1)] xs = [([[[]]], xs)] | |
| partition [(1, 1)] (x : xs) = | |
| ([[[x]]], xs) : [(ysss, x : zs) | (ysss, zs) <- partition [(1, 1)] xs] | |
| partition [(k, 1)] (x : xs) = |
This file contains hidden or 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 (inits, permutations) | |
| queens :: Int -> [[(Int, Int)]] | |
| queens n = filter p $ liftA2 map zip permutations $ take n [0 ..] | |
| where p x = and $ zipWith f x $ inits x | |
| f (a, b) = all $ \(c, d) -> abs (a - c) /= abs (b - d) |
This file contains hidden or 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
| "The datatype." | |
| shared interface Expression { | |
| "The initial operation." | |
| shared formal Integer evaluate(); | |
| } | |
| "The initial case of the datatype." | |
| shared interface Literal satisfies Expression { | |
| "The value of the literal expression." | |
| shared formal Integer literal; |
This file contains hidden or 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
| x = fst $ fromJust $ find (uncurry (==)) $ liftA2 ($) zip tail $ iterate cos 1 |
This file contains hidden or 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 (nubBy) | |
| primes = nubBy (((> 1) .) . gcd) [2..] |
This file contains hidden or 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 Control.Monad (guard) | |
| import Data.Functor ((<$)) | |
| import Data.List (unfoldr) | |
| import Data.Tuple (swap) | |
| digits :: Integer -> [Integer] | |
| digits = reverse . unfoldr (liftA2 (<$) (swap . (`divMod` 10)) (guard . (> 0))) |
This file contains hidden or 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
| mapEveryNth n f = zipWith ($) $ tail $ cycle $ f : replicate (n - 1) id |
NewerOlder