Last active
August 29, 2015 14:07
-
-
Save swannodette/26e4bf19a15ca28f9ba3 to your computer and use it in GitHub Desktop.
string length constraint
This file contains 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 strlenc.core | |
(:refer-clojure :exclude [==]) | |
(:require [clojure.core.logic :as l :refer [run* fresh ==]] | |
[clojure.core.logic.protocols :as lp]) | |
(:import [])) | |
(defn -strlenc | |
([str len] | |
(reify | |
lp/IConstraintStep | |
(-step [this s] | |
(reify | |
clojure.lang.IFn | |
(invoke [_ s] | |
(let [str (lp/walk s str)] | |
(when (<= (count 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