Skip to content

Instantly share code, notes, and snippets.

@MageMasher
Last active July 21, 2019 02:48
Show Gist options
  • Save MageMasher/919f7c48bc0bb229c706976f4d3cbd95 to your computer and use it in GitHub Desktop.
Save MageMasher/919f7c48bc0bb229c706976f4d3cbd95 to your computer and use it in GitHub Desktop.
Self Realizing code! Code that, when given a `what-i-want` and a vector of operators and operands, will create the lisp code to compute that result.
(ns self-realizing.core
(:gen-class)
(:require [clojure.math.combinatorics :as combo]))
(defmacro ignore-errors
"Returns the result of evaluating e, or nil if it throws an exception."
[e]
`(try ~e (catch java.lang.Exception _# nil)))
(defn findr
[what-i-want fn-domain]
(for [sub (combo/subsets fn-domain)
:when (ifn? (first sub))
perm (combo/permutations sub)
:when (= what-i-want (ignore-errors (eval perm)))]
perm))
(comment
(findr 3 ['+ 1 2 3])
)
(comment
(take 1 (findr 24 ['+ '* 1 2 3 4 5 6 7 8]))
)
(comment
(take 1 (findr "hello world!" ['+ '* 'str 1 2 3 "hello" " " "world" "!"]))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment