oessessnex

joined 1 year ago
[–] oessessnex 1 points 1 year ago (1 children)

You can sort the lines alphabetically before comparing.

[–] oessessnex 2 points 1 year ago* (last edited 1 year ago)

That was pretty fun :)

My solution is in OCaml. I generate the Dyck language directly (which is basically what this is).

type color = P | B | C
type dyck = Fork of color * dyck * dyck | Nil

let fork c i n = Fork (c, i, n)

let colors = List.to_seq [P; B; C]

let color_open = function P -> "(" | B -> "[" | C -> "{"

let color_close = function P -> ")" | B -> "]" | C -> "}"

let rec to_string = function
  | Fork (c, i, n) -> color_open c ^ to_string i ^ color_close c ^ to_string n
  | Nil -> ""

let memo f =
  let t = Hashtbl.create 10 in
  let rec g x =
    try Hashtbl.find t x with
    | Not_found ->
      let y = f g x in
      Hashtbl.add t x y;
      y
  in g

let ( let* ) x f = Seq.flat_map f x

let generate' recur = function
  | 0 -> Seq.return Nil
  | n ->
    Seq.memoize @@
    let* d = Seq.take n @@ Seq.ints 1 in
    let* c = colors in
    Seq.map_product (fork c) (recur (d - 1)) (recur (n - d))

let generate = memo generate'

let () =
  let n = (int_of_string Sys.argv.(1)) / 2 in
  Seq.iter (fun d -> print_endline (to_string d)) (generate n)
[–] oessessnex 2 points 1 year ago

You can use libvirt. It's not exactly declarative but you can automate it.

[–] oessessnex 11 points 1 year ago* (last edited 1 year ago)

Nope. Monads enable you to redefine how statements work.

Let's say you have a program and use an Error[T] data type which can either be Ok {Value: T} or Error:

var a = new Ok {Value = 1};
var b = foo();
return new Ok {Value = (a + b)};

Each statement has the following form:

var a = expr;
rest

You first evaluate the "expr" part and bind/store the result in variable a, and evaluate the "rest" of the program.

You could represent the same thing using an anonymous function you evaluate right away:

(a => rest)(expr);

In a normal statement you just pass the result of "expr" to the function directly. The monad allows you to redefine that part.

You instead write:

bind((a => rest), expr);

Here "bind" redefines how the result of expr is passed to the anonymous function.

If you implement bind as:

B bind(Func[A, B] f, A result_expr) {
   return f(result_expr);
}

Then you get normal statements.

If you implement bind as:

Error[B] bind(Func[A, Error[B]] f, Error[A] result_expr) {
   switch (result_expr) {
       case Ok { Value: var a}:
           return f(a);
       case Error:
           return Error;
   }
}

You get statements with error handling.

So in an above example if the result of foo() is Error, the result of the statement is Error and the rest of the program is not evaluated. Otherwise, if the result of foo() is Ok {Value = 3}, you pass 3 to the rest of the program and you get a final result Ok {Value = 4}.

So the whole idea is that you hide the if Error part by redefining how the statements are interpreted.

[–] oessessnex 1 points 1 year ago

You can probably replace it with ImageMagick.

[–] oessessnex 27 points 1 year ago* (last edited 1 year ago)

Some people consider working on programming languages fun, so they create new ones.

[–] oessessnex 1 points 1 year ago

If you want to make games, start with a text based adventure game, something like Zork. Learn as you go, you will need console IO and data structures to represent game state, puzzles, levels. Then make a 2D game. After that you will probably be proficient enough to make anything you want.

[–] oessessnex 6 points 1 year ago
[–] oessessnex 5 points 1 year ago

Editing in (neo)vim is akin to live programming in a very terse programming language. You can also create your own commands (keybindings) and repeat snippets multiple times (macros). This is amazingly powerful if you need to convert between two formats or extract some information.

The UI is not a highlight of this editor.

It also takes years before selecting the commands becomes mostly subconscious. It is definitely a long term investment.

view more: ‹ prev next ›