Skip to content

Instantly share code, notes, and snippets.

@zmaril
Forked from swannodette/strlenc.clj
Created October 24, 2014 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmaril/5c4fffb02bb4a144ee23 to your computer and use it in GitHub Desktop.
Save zmaril/5c4fffb02bb4a144ee23 to your computer and use it in GitHub Desktop.
(ns instagenerate.strlenc
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :as l :refer [run* fresh ==]]
[clojure.core.logic.protocols :as lp])
(:import [clojure.core.logic LCons]))
(defn lcount [l]
(loop [i 0 l l]
(println i l)
(if (and (= LCons (type l)) (.d l))
(recur (if (char? (.a l)) (inc i) i)
(.d l))
i)))
(defn -strlenc
([str len]
(reify
lp/IConstraintStep
(-step [this s]
(reify
clojure.lang.IFn
(invoke [_ s]
(let [str (lp/walk s str)]
(let [cf (if (= (type str) LCons)
lcount
count)]
(when (<= (cf str) len)
((l/remcg this) s)))))
lp/IRunnable
(-runnable? [_]
(l/ground-term? str s))))
lp/IConstraintOp
(-rator [_]
`strlenc)
(-rands [_]
[str len])
lp/IReifiableConstraint
(-reifyc [_ v r s]
`(strlenc ~str ~len))
lp/IConstraintWatchedStores
(-watched-stores [this] #{::subst}))))
(defn strlenc
[str len]
(l/cgoal (-strlenc str len)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment