This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-04-12
Channels
- # beginners (47)
- # boot (5)
- # bristol-clojurians (1)
- # cider (45)
- # clara (2)
- # cljs-dev (11)
- # cljsrn (47)
- # clojure (169)
- # clojure-brasil (2)
- # clojure-dusseldorf (22)
- # clojure-finland (1)
- # clojure-italy (9)
- # clojure-nl (3)
- # clojure-poland (2)
- # clojure-russia (4)
- # clojure-spec (79)
- # clojure-uk (105)
- # clojurescript (59)
- # core-async (39)
- # cursive (31)
- # datomic (10)
- # devcards (1)
- # duct (6)
- # editors (9)
- # emacs (12)
- # figwheel (1)
- # fulcro (50)
- # java (4)
- # mount (1)
- # off-topic (47)
- # onyx (33)
- # pedestal (1)
- # protorepl (1)
- # re-frame (32)
- # reagent (45)
- # ring-swagger (6)
- # shadow-cljs (100)
- # tools-deps (6)
- # uncomplicate (27)
- # vim (3)
Just saying thanks again! Worked like a charm! http://exercism.io/submissions/02b1e7c37b99433189c25365de9bd81b Going to look deeper into some->>
👋
@funyako.funyao156 Would this work for you?
boot.user=> (s/def ::fmn (s/cat :a string? :b string? :c string? :kws (s/* keyword?)))
:boot.user/fmn
boot.user=> (s/conform ::fmn ["a" "b" "c"])
{:a "a", :b "b", :c "c"}
boot.user=> (s/conform ::fmn ["a" "b" "c" :d :e :f])
{:a "a", :b "b", :c "c", :kws [:d :e :f]}
boot.user=> (s/conform ::fmn ["d" "e" "f" 1 2 3])
:clojure.spec.alpha/invalid
boot.user=> (s/explain ::fmn ["d" "e" "f" 1 2 3])
In: [3] val: 1 fails spec: :boot.user/fmn at: [:kws] predicate: keyword?
nil
boot.user=>
@loganpowell Cool! You could use (assert ...)
in str-2-seq
instead of the if
to perhaps simplify things there (now that you're catching assertions):
(defn str-2-seq [str]
(let [chars (filter #(not= \- %) (seq str))]
(assert (= 10 (count chars))
(split-at 9 chars)))
Since (split-at 9 chars)
produces [digits checkr]
as its result.
Then another little trick you can do in isbn?
is to further destructure checkr
since you know it should be a sequence of one element
(defn isbn? [isbn]
(try
(let [[digits [checkr]] (str-2-seq isbn)
nine (first-9 digits)
chkr (hdl-check checkr) ; checkr is a Character now -- no need for apply
sum (+ nine chkr)]
(and (= (mod sum 11) 0)
(> 100000 sum)))
(catch Throwable _ false)))
@seancorfield Thanks! Worked like a charm. I need to understand spec better especially about the whole "regex" thing since I'm expecting :kws
would be just another vector, not flattened out.
The conformed :kws
is a vector but s/*
matches zero or more entries in a sequence.
The regex vs collection stuff takes a bit of getting used to...
@grierson Depending on how much compute time is required by tf/parse, you might want to consider first calculating the result of tf/parse and putting the results under a new key in the maps to be sorted. Then you comparison function would just become t/after?. You can postprocess to remove the just-added key if you do not need it for anything else later. This is a general pattern called "decorate - sort - undecorate" that can be useful if keys are expensive to compute.
That is just an efficiency consideration, though. As far as correctness goes, what you wrote looks reasonable.
Hey, qq. ->
and ->>
provide a pipe of sorts. Is there a way to tee things out of the pipe temporarily and then merge it back to the pipe?
Very simple example: I have a list as an input. I want to count values of the list, create a range of 0..n, then map over this range and original list to create pairs of [i, value], then perform further operations on the list of pairs.
in short, no
but you can kind of do what you’re describing using as->
The specific steps you describe sound similar to the built-in function map-indexed. Might be worth looking at to see if it does what you want.
@alexmiller TIL, thank you 🙂 this is relevant to my interests @andy.fingerhut it’s just an example - I needed something that would be short to describe and simple to understand (thank you though, it would have been a good solution if this was the problem)
(as-> [1 2 3] c (map vector (range (count c)) c))
I have a map called my-map
which equals {:a 1 :b 2}
and I want to store all the map keys into a symbol. I tried this (def my-keys (keys my-map))
but I’m getting this error
IllegalStateException Attempting to call unbound fn: #'user/keys clojure.lang.Var$Unbound.throwArity (Var.java:45)
That was the problem, I did (def keys (keys my-map))
Not realizing that was a method until after. Thanks!!
@noisesmith that some->>
idea was pretty clever 😉
In my experience it leads to simpler code that survives refactors. Breaking try/catch based logic when editing code is common in my experience.
Is there a way to reverse desctructure? For example, instead of a b & rest
then rest & a b
?
What are some good up-to-date beginner tutorials/guides on Clojurescript apart from the Getting Started guide on the CLJS site?
@nikoszp The re-frame documentation is very good. https://github.com/Day8/re-frame
I have a symbol rest
that is equal to a sequence (3 2 1)
. When running (rseq rest)
I’m getting this error
ClassCastException clojure.lang.APersistentVector$RSeq cannot be cast to clojure.lang.Reversible clojure.core/rseq (core.clj:1573)
I’m not sure what that means? rseq
worked on a sequence of stringsrseq works on specific collection types, it doesn't work on any of the collections that print with ()
in this case, you generated something using rseq, and rseq cannot operate on its own output
you can use reverse though
I think the set of collections that rseq
can be applied to are exactly those which satisfy the reversible?
predicate.
A couple of reversible collections that the rseq
docstring doesn't mention are sorted sets and map entries.
yeah - pretty much user=> (filter reversible? ["" [] #{} {} () nil])
-> ([])
everything seqable? returns true for
which is a lot of things user=> (filter seqable? ["" [] #{} {} () nil])
=> ("" [] #{} {} () nil)
rseq performs better though
So I’d better off converting my sequences to vectors and using rseq? or is reverse fine for everything else
So, (if (reversible? s) (rseq s) (reverse s))
?
@seancorfield Hah! The definition of reverse
in ClojureScript has that built in.
if perf is the main concern, I assume using reverse directly will still be faster than vec followed by rseq