Cursed Nix Koans

This is a bit of an experiment.
Discuss at Discuss Cursed Nix Koans.

Submissions welcome:
Rules:

  • one post per item.
  • only koans (limit noise)
  • must be runnable.

Caused by an IRC conversation:

let
  _oldmul = __mul;
in
let
  __mul = a: b:
    if (builtins.isAttrs a)
      then (a // b)
      else (_oldmul a b);
in
  {
    set = { a = 1; } * { b = 2; };
    int = 1 * 2;
    }
3 Likes

Donated over IRC;

let
  inherit (builtins) elemAt head;

  makeitso = list: i: elemAt list (head i);
  val = makeitso [1 "to" 3];
in
  [(val[0]) (val[1]) (val[2])] 
let
  inherit (builtins) getAttr head;

  makeitso = val: val // {
    __functor = val: i: getAttr (head i) val;
    };
  val = makeitso { a = 7; b = "ate"; c = 9; };
in
  builtins.mapAttrs (n: _: val[n]) val

A slight variant:

let
  __div = x: y: y x;
in
  builtins.readDir "/etc" / builtins.attrNames / builtins.length
let
  __mul = x: y: if builtins.typeOf y != "lambda"
    then x y
    else z: x (y z);
in 
  builtins.length * builtins.attrNames * builtins.readDir "/etc"

source:[spoiler]Add an infix reverse-application operator · Issue #2118 · NixOS/nix · GitHub