Skip to content

Instantly share code, notes, and snippets.

@thedeemon
thedeemon / eu300.d
Last active February 21, 2021 18:14
import std.stdio, std.parallelism, std.range, std.algorithm;
enum L = 12;
struct V {int x,y;}
int optLen(int seed) {
int[32*32] field;
V[4] vecs = [V(1,0), V(0,-1), V(-1,0), V(0,1)];
V[L] path;
@thedeemon
thedeemon / liev.d
Last active November 22, 2020 19:54
A test of @LiVe in D
// dmd -preview=dip1021 liev.d
import std.stdio;
struct S { string name; }
@live void use(scope const S* p) { // ok
writeln(p.name);
}
void release(S *p) { } // consumes the ownership
@thedeemon
thedeemon / dn.d
Last active April 20, 2020 09:35
import std.stdio;
// secret sauce: https://dlang.org/spec/class.html#alias-this
struct DayNum {
int dn;
alias dn this; // this line makes DayNum look like an int with dn as the value
}
DayNum next(DayNum d) {
{-# LANGUAGE BangPatterns #-}
import qualified Data.Vector.Unboxed as V
import System.CPUTime
import Text.Printf
time :: IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
module Main
import Data.Vect
toVec : List a -> (n : Nat ** Vect n a)
toVec [] = (_ ** [])
toVec (x::xs) with (toVec xs)
| (k ** v) = (S k ** x :: v)
myFun : Vect n1 Int -> Vect n2 Int -> LTE n1 n2 -> Int
myFun xs ys p = foldr (+) 0 xs
@thedeemon
thedeemon / path_int.d
Last active September 26, 2019 15:23
Toy quantum mechanics with path integrals
import std.complex, std.stdio, std.random, std.parallelism, std.math : hypot;
import std.getopt, std.datetime.stopwatch, std.array, std.algorithm : max, min, reduce;
import std.format, std.range : iota;
struct Wall { double y, x1, x2; } // x1 < x2
struct Point { double x, y; }
bool intersects(Point p1, Point p2, ref Wall wall) {
if (min(p1.y, p2.y) > wall.y) return false;
if (max(p1.y, p2.y) < wall.y) return false;
ILogger {
void log(string msg);
}
class BoringLogger : ILogger {
overrride void log(string msg) {...}
}
class ColorfulLogger : ILogger {
overrride void log(string msg) {...}
open Batteries
type point = { x : int; y : int }
let f from_y to_y from_x to_x =
let a = (from_y --^ to_y/2) |> Enum.map (fun y -> {x=from_x; y=y}) in
let b = (to_y/2 --^ to_y) |> Enum.map (fun y -> {x=to_x; y=y}) in
Enum.append a b |> Array.of_enum;;
let g from_y to_y from_x to_x =
let t = to_y/2 in
Array.init (to_y - from_y) (fun i -> let y = i + from_y in if y < t then {x=from_x; y=y} else {x=to_x;y=y});;
import std.stdio, std.algorithm, std.range, std.array, std.datetime.stopwatch;
struct Point { int x, y; }
auto f(int from_y, int to_y, int from_x, int to_x) {
return chain( iota(from_y, to_y/2).map!(y => Point(from_x, y)),
iota(to_y/2, to_y) .map!(y => Point(to_x, y)) ).array;
}
auto g(int from_y, int to_y, int from_x, int to_x) {
import qualified Data.ByteString.Lazy.Char8 as L8
import Network.HTTP.Client (defaultManagerSettings, newManager)
import Network.HTTP.Simple
import Network.HTTP.Client.TLS
import System.Directory
import qualified Data.Map as M
import Data.List (sortBy)
import Control.Monad
import Control.Applicative
import System.Environment