-
-
Save zmaril/5c4fffb02bb4a144ee23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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