Re: s/valid? does not tell me if the data is valid as supplied

2018-02-26 Thread Simon Belak
I’m a pretty heavy user of spec including a lot of coercion. The way I’ve 
always done coercion is to enumerate all accepted shapes first and do coercion 
last. For your example this would be:

(s/def ::foo (s/and (s/or :string string?
 :kw keyword?)
 (s/conformer (fn [[tag x]]
(case tag
   :string (keyword x)
   x)

Ps. also don’t forget s/conformer can return :s/invalid to signal failure.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: grouping and mapping

2016-08-13 Thread Simon Belak
Another option (and shameless self promotion) is the rollup family of 
functions in https://github.com/sbelak/huri

Your case would be:

(rollup first identity second foos)
=> {:a ("foo" "baz"), :b ("bar")}


On Friday, August 12, 2016 at 7:10:37 PM UTC+2, Erik Assum wrote:
>
> I’ve been working on a new project in java 8 at a new client where I’ve 
> been able to play with streams. 
> In doing so I’ve come across Collectors.groupingBy which has some 
> additional features that group-by doesn’t seem to have. 
>
> Example: 
> Given 
> (def foos [[:a "foo"]  [:b "bar"]  [:a "baz"]]) 
>
> (group-by first foos) 
> ;=> {:a [[:a "foo"] [:a "baz"]], :b [[:b "bar"]]} 
>
> but I’d like the result to be 
> ;=> {:a ["FOO" "BAZ"] :b ["BAR”]} 
>
> This is solved in javas stream api by something like this: 
>
> List> = [["a", "foo"], ["b" "bar"], ["a", "baz"]]; 
>
> foos 
> .stream() 
> .collect(Collectors.groupingBy(e -> e.get(0), 
>  Collectors.mapping(e -> e.get(1).toUpperCase(), 
> Collectors.toList(; 
>
> Where the key point is that Collectors.groupingBy accepts a mapping 
> function in which to map the values being grouped. 
>
> So how would one go about writing this in Clojure? I could of-course write 
> something that mapped over the grouped map, 
> but I’d really like to write something like 
>
> (defn mapping-group-by grouping-fn mapping-fn coll) 
>
> but I have a suspicion that this already exists in some form already? 
>
> Erik. 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ANN] Clojure 1.9.0-alpha8

2016-06-29 Thread Simon Belak
Missed that s/nilable exists. Cheers. 


On 29 June 2016 at 16:04:05, Alex Miller (a...@puredanger.com) wrote:

Also, you can turn any predicate into a nil-accepting predicate with s/nilable.

user=> (s/valid? (s/nilable (s/every ::s/any)) nil)
true

On Wednesday, June 29, 2016 at 9:02:43 AM UTC-5, Alex Miller wrote:
user=> (s/explain (s/every ::s/any) nil)
val: nil fails predicate: coll?

indicates that s/every (also s/coll-of) validate using coll? as the predicate 
and coll? (like most type predicates) does not validate nil.

nil punning is more often done for sequences. Sequences with structure are 
typically speced using regex ops, which does validate on nil.

user=> (s/valid? (s/* ::s/any) nil)
true

On Wednesday, June 29, 2016 at 8:57:49 AM UTC-5, Simon Belak wrote:
map-of now conforming is fantastic news! 
One question though: why doesn't every validate when given nil for collection? 
Seems inconsistent given pervasive nil punning elsewhere and that [] validates.

s
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google 
Groups "Clojure" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/clojure/vF3RuDWuX8I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to 
clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ANN] Clojure 1.9.0-alpha8

2016-06-29 Thread Simon Belak
map-of now conforming is fantastic news! 
One question though: why doesn't every validate when given nil for 
collection? Seems inconsistent given pervasive nil punning elsewhere and 
that [] validates.

s


On Wednesday, June 29, 2016 at 12:13:25 AM UTC+2, Alex Miller wrote:
>
> Clojure 1.9.0-alpha8 is now available.
>
> Try it via
>
> - Download: 
> https://repo1.maven.org/maven2/org/clojure/clojure/1.9.0-alpha8
> - Dependency: [org.clojure/clojure "1.9.0-alpha8"]
>
> 1.9.0-alpha8 includes the following changes since 1.9.0-alpha7:
>
> The collection spec support has been greatly enhanced, with new controls 
> for conforming, generation, counts, distinct elements and collection kinds. 
> See the docs for every, every-kv, coll-of and map-of for details.
>
> instrumenting and testing has been streamlined and made more composable, 
> with powerful new features for spec and gen overrides, stubbing, and 
> mocking. See the docs for these functions in clojure.spec.test: instrument, 
> test, enumerate-ns and summarize-results.
>
> Namespaced keyword reader format, printing and destructuring have been 
> enhanced for lifting namespaces up for keys, supporting more succinct use 
> of fully-qualified keywords. Updated docs will be added to clojure.org 
> soon.
>
> Many utilities have been added, for keys spec merging, fn exercising, Java 
> 1.8 timestamps, bounded-count and more.
>
> Changelog:
>
> clojure.spec:
>
> - [changed] map-of - now conforms all values and optionally all keys, has 
> additional kind, count, gen options
> - [changed] coll-of - now conforms all elements, has additional kind, 
> count, gen options. No longer takes init-coll param.
> - [added] every - validates a collection by sampling, with many additional 
> options
> - [added] every-kv - validates a map by sampling, with many additional 
> options
> - [added] merge
> - [changed] gen overrides can now be specified by either name or path
> - [changed] fspec generator - creates a function that generates return 
> values according to the :ret spec and ignores :fn spec
> - [added] explain-out - produces an explain output string from an 
> explain-data result
> - [changed] explain-data - output is now a vector of problems with a :path 
> element, not a map keyed by path
> - [added] get-spec - for looking up a spec in the registry by keyword or 
> symbol
> - [removed] fn-spec - see get-spec
> - [added] exercise-fn - given a spec'ed function, returns generated args 
> and the return value
> - All instrument functions moved to clojure.spec.test
>
> clojure.spec.test:
>
> - [changed] instrument - previously took a var, now takes either a symbol, 
> namespace symbol, or a collection of symbols or namespaces, plus many new 
> options for stubbing or mocking. Check the docstring for more info.
> - [removed] instrument-ns - see instrument
> - [removed] instrument-all - see instrument
> - [changed] unstrument - previously took a var, now takes a symbol, 
> namespace symbol, or collection of symbol or namespaces
> - [removed] unstrument-ns - see unstrument
> - [removed] unstrument-all - see unstrument
> - [added] instrumentable-syms - syms that can be instrumented
> - [added] with-instrument-disabled - disable instrument's checking of 
> calls within a scope
> - [changed] check-var renamed to test and has a different signature, check 
> docs
> - [changed] run-tests - see test
> - [changed] run-all-tests - see test
> - [changed] check-fn - renamed to test-fn
> - [added] abbrev-result - returns a briefer description of a test
> - [added] summarize-result - returns a summary of many tests
> - [added] testable-syms - syms that can be tested
> - [added] enumerate-namespace - provides symbols for vars in namespaces
>
> clojure.core:
>
> - [changed] - inst-ms now works with java.time.Instant instances when 
> Clojure is used with Java 8
> - [added] bounded-count - if coll is counted? returns its count, else 
> counts at most first n elements of coll using its seq
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: anonymous fn or partial?

2010-10-01 Thread Simon Belak
There is a subtle difference in how fixed arguments are handled.
partial evaluates the arguments only once while fn evaluates them on
each call. For side-effects free code the former can yield better
performance. To recap:

(partial foo (baz 1 2))

==

(let [b (baz 1 2)]
  (fn [& x] (apply foo b x))

As for syntax sugar, I would like to see the #() macro to be extended
so that it curries when no implicit arguments are given:

#(foo bar baz) => #(foo bar baz &%)

whilst keeping (and emphasising) partial for cases where evaluate-once
semantics are required.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en