-
Notifications
You must be signed in to change notification settings - Fork 5
Temporary v5.api with side-effect free set-options! and merge-options! #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
onbreath
wants to merge
1
commit into
main
Choose a base branch
from
merge-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,12 +153,85 @@ is wrapped in a vector first\" | |
|
|
||
| " (known-kinds all-kinds))) | ||
|
|
||
| (defn api-ns-v5 [all-kinds] | ||
| (str "(ns scicloj.kindly.v5.api | ||
| \"See the kind namespace\") | ||
|
|
||
| (defn attach-meta-to-value | ||
| [value m] | ||
| (if (instance? clojure.lang.IObj value) | ||
| (vary-meta value merge m) | ||
| (attach-meta-to-value | ||
| [value] | ||
| (update m :kindly/options assoc :wrapped-value true)))) | ||
|
|
||
| (defn attach-kind-to-value | ||
| [value kind] | ||
| (attach-meta-to-value value {:kindly/kind kind})) | ||
|
|
||
| (defn deep-merge | ||
| \"Recursively merges values with support for control metadata. | ||
| Merge rules: | ||
| - Maps are merged recursively | ||
| - Non-maps except nil replace previous value | ||
| - ^:replace on a map replaces previous value | ||
| Examples: | ||
| (deep-merge {:a 1} {:a 2}) => {:a 2} | ||
| (deep-merge {:a 1} ^:replace {:b 2}) => {:b 2}\" | ||
| ([] {}) | ||
| ([a] a) | ||
| ([a b] | ||
| (cond | ||
| (:replace (meta b)) b | ||
| (and (map? a) (map? b)) (merge-with deep-merge a b) | ||
| (and (map? a) (nil? b)) a | ||
| :else b)) | ||
| ([a b & more] | ||
| (reduce deep-merge (deep-merge a b) more))) | ||
|
|
||
| (defn hide-code | ||
| \"Annotate whether the code of this value should be hidden\" | ||
| ([value] | ||
| (hide-code value true)) | ||
| ([value bool] | ||
| ;; Will change when Clay is updated | ||
| (attach-meta-to-value value {:kindly/hide-code bool}))) | ||
|
|
||
| (defn set-options! | ||
| \"Replaces *options* with options\" | ||
| [options] | ||
| (vary-meta options merge {:kindly/merge-options true})) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be |
||
|
|
||
| (defn merge-options! | ||
| \"Mutates *options* with the deep merge of options\" | ||
| [options] | ||
| (vary-meta options merge {:kindly/merge-options true})) | ||
|
|
||
| (defn consider | ||
| \"Add metadata to a given value. | ||
| A values which cannot have metadata | ||
| (i.e., is not an instance of `IObj`) | ||
| is wrapped in a vector first\" | ||
| [value m] | ||
| (cond (keyword? m) (attach-kind-to-value value m) | ||
| (fn? m) (consider value (m)) | ||
| (map? m) (attach-meta-to-value value m))) | ||
|
|
||
| (defn check | ||
| \"Add a generated test using `:kind/test-last`\" | ||
| [& args] | ||
| (consider args :kind/test-last)) | ||
|
|
||
| " (known-kinds all-kinds))) | ||
|
|
||
| (defn -main [& args] | ||
| (let [all-kinds (read-kinds)] | ||
| (->> (kind-ns all-kinds) | ||
| (spit (io/file "src" "scicloj" "kindly" "v4" "kind.cljc"))) | ||
| (->> (api-ns all-kinds) | ||
| (spit (io/file "src" "scicloj" "kindly" "v4" "api.cljc"))))) | ||
| (spit (io/file "src" "scicloj" "kindly" "v4" "api.cljc"))) | ||
| (->> (api-ns-v5 all-kinds) | ||
| (spit (io/file "src" "scicloj" "kindly" "v5" "api.cljc"))))) | ||
|
|
||
| (comment | ||
| (-main)) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| (ns scicloj.kindly.v5.api | ||
| "See the kind namespace") | ||
|
|
||
| (defn attach-meta-to-value | ||
| [value m] | ||
| (if (instance? clojure.lang.IObj value) | ||
| (vary-meta value merge m) | ||
| (attach-meta-to-value | ||
| [value] | ||
| (update m :kindly/options assoc :wrapped-value true)))) | ||
|
|
||
| (defn attach-kind-to-value | ||
| [value kind] | ||
| (attach-meta-to-value value {:kindly/kind kind})) | ||
|
|
||
| (defn deep-merge | ||
| "Recursively merges values with support for control metadata. | ||
| Merge rules: | ||
| - Maps are merged recursively | ||
| - Non-maps except nil replace previous value | ||
| - ^:replace on a map replaces previous value | ||
| Examples: | ||
| (deep-merge {:a 1} {:a 2}) => {:a 2} | ||
| (deep-merge {:a 1} ^:replace {:b 2}) => {:b 2}" | ||
| ([] {}) | ||
| ([a] a) | ||
| ([a b] | ||
| (cond | ||
| (:replace (meta b)) b | ||
| (and (map? a) (map? b)) (merge-with deep-merge a b) | ||
| (and (map? a) (nil? b)) a | ||
| :else b)) | ||
| ([a b & more] | ||
| (reduce deep-merge (deep-merge a b) more))) | ||
|
|
||
| (defn hide-code | ||
| "Annotate whether the code of this value should be hidden" | ||
| ([value] | ||
| (hide-code value true)) | ||
| ([value bool] | ||
| ;; Will change when Clay is updated | ||
| (attach-meta-to-value value {:kindly/hide-code bool}))) | ||
|
|
||
| (defn set-options! | ||
| "Replaces *options* with options" | ||
| [options] | ||
| (vary-meta options merge {:kindly/merge-options true})) | ||
|
|
||
| (defn merge-options! | ||
| "Mutates *options* with the deep merge of options" | ||
| [options] | ||
| (vary-meta options merge {:kindly/merge-options true})) | ||
|
|
||
| (defn consider | ||
| "Add metadata to a given value. | ||
| A values which cannot have metadata | ||
| (i.e., is not an instance of `IObj`) | ||
| is wrapped in a vector first" | ||
| [value m] | ||
| (cond (keyword? m) (attach-kind-to-value value m) | ||
| (fn? m) (consider value (m)) | ||
| (map? m) (attach-meta-to-value value m))) | ||
|
|
||
| (defn check | ||
| "Add a generated test using `:kind/test-last`" | ||
| [& args] | ||
| (consider args :kind/test-last)) | ||
|
|
||
| (def known-kinds | ||
| "A set of common visualization requests" | ||
| #{ | ||
| ;; simple behaviours | ||
| :kind/println | ||
| :kind/pprint | ||
| :kind/hidden | ||
| ;; web dev | ||
| :kind/html | ||
| :kind/hiccup | ||
| :kind/reagent | ||
| :kind/scittle | ||
| :kind/emmy-viewers | ||
| ;; data visualization formats | ||
| :kind/graphviz | ||
| :kind/mermaid | ||
| :kind/md | ||
| :kind/tex | ||
| :kind/code | ||
| :kind/edn | ||
| :kind/vega | ||
| :kind/vega-lite | ||
| :kind/echarts | ||
| :kind/cytoscape | ||
| :kind/plotly | ||
| :kind/htmlwidgets-plotly | ||
| :kind/htmlwidgets-ggplotly | ||
| :kind/video | ||
| :kind/audio | ||
| :kind/observable | ||
| :kind/highcharts | ||
| ;; specific types | ||
| :kind/image | ||
| :kind/dataset | ||
| :kind/smile-model | ||
| ;; clojure specific | ||
| :kind/var | ||
| :kind/test | ||
| ;; plain structures | ||
| :kind/seq | ||
| :kind/vector | ||
| :kind/set | ||
| :kind/map | ||
| ;; other recursive structures | ||
| :kind/table | ||
| :kind/portal | ||
| ;; meta kinds | ||
| :kind/fragment | ||
| :kind/fn | ||
| :kind/test-last}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we stick with v4? Is it necessary to move to v5?