This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-29
Channels
- # 100-days-of-code (2)
- # bangalore-clj (1)
- # beginners (141)
- # cider (33)
- # cljs-dev (13)
- # cljsjs (7)
- # cljsrn (1)
- # clojure (88)
- # clojure-conj (3)
- # clojure-dev (24)
- # clojure-italy (11)
- # clojure-nl (4)
- # clojure-russia (1)
- # clojure-sanfrancisco (1)
- # clojure-spec (4)
- # clojure-uk (53)
- # clojurescript (65)
- # core-logic (2)
- # cursive (28)
- # datomic (33)
- # duct (2)
- # emacs (3)
- # figwheel-main (9)
- # fulcro (44)
- # hoplon (6)
- # leiningen (144)
- # mount (1)
- # nrepl (21)
- # off-topic (102)
- # onyx (2)
- # other-languages (5)
- # pathom (6)
- # planck (3)
- # portkey (1)
- # re-frame (7)
- # reagent (5)
- # reitit (17)
- # shadow-cljs (24)
- # spacemacs (16)
- # tools-deps (64)
- # uncomplicate (2)
- # vim (22)
Writing a Lambda/API Gateway fn in Clojure, I need an adapter that translates requests into Ring request/response format:
- locally I can just run Jetty+Ring
- routing is done in the Ring handler (e.g. compojure)
- on Lambda, I can set up an ANY route with path *
that forwards all requests to the Ring adapter
There are a few projects on github along those lines like https://github.com/indabamusic/ring-aws-api-gateway-lambda-adapter and https://sideshowcoder.com/2018/05/11/clojure-ring-api-gateway-lambda/ - can anyone recommend a project?
@dominicm I think portkey is far more ambitious than what I'm looking for (basically a few lines of code, https://github.com/mhjort/ring-apigw-lambda-proxy)
Does it make sense to use core.typed in 2018?
I used to be really excited for core.typed, but after writing more Clojure I'm finding that I don't really want static typing.
I think core.typed
can generate specs from tests which is pretty clever. Not really tried it though, just seen it talked about https://github.com/clojure/core.typed#runtime-spec-and-type-inference
There might be use cases for it, but honestly... core.spec
does this job really well, even better.
is there a high-order equivalent of or
? I'd like to do something like (def invalid-number? (or* zero? neg?)) ... (remove invalid-number? numbers)
I wrote https://github.com/TristeFigure/weaving for this purpose. The function you're looking for is called or|
. I don't use it much, but it's a great addition when you're writing a DSL.
My policy when writing a DSL tends to follow the Data > Function > Macro dogma (https://lispcast.com/data-functions-macros-why/) when it is large enough. But when it is a small/private DSL, I tend to favor functions over data because the obscurity implied by functions underweights the task of implementing basic conditional flow in a data-driven DSL.
One of my collegues has discovered transducers recently and now he tend to turn every simple map
function into a combination of a map
transducer and (into [])
like this
(into [] (map fn) coll)
;;instead of
(map fn coll)
I think that in our case, when coll
has around 10 to 20 values in it, such transducer usage is overkill and harms readability. What do you think? Maybe I'm overseeing something and there are other reasons to do so?The case you quote exactly, there's the benefit of putting it into a particular shape ... I might be tempted there but there isn't much difference between (into [] (map fn) coll)
and (into [] (map fn coll))
anyway.
But I generally only bother when there's a series of transformations.
There was no need to turn it into a []
. My collegue is using (into [])
because it accepts transducer as an input.
Yeah if the shape isn't needed and there's only one transformation this is overusing the tool for sure.
I agree that this is not something you should do everywhere and the seq version can even be faster (with small coll size and one op)
also there is mapv
which is pretty much the same as using (into [] (map ...))
You're lucky, I'm working on a project that exactly does what you want (hope I haven't missed anything extracting this snippet out of it).
Oh and I think sort
should be replaced with set
and =
with clojure.set/subset?
.
Also replace ns-map
with ns-interns
otherwise you'll have duplicates in the end result.
Since I know both Clojure and Ruby quite well, feel free to share your post so that I can review it. Also you might be interested in some functional combinators I wrote for Ruby. In particular have a look at these experiments that explore how Ruby's scope works. https://github.com/TristeFigure/tissage/blob/master/examples/closures.rb
What library would people use these days for easily-configurable XML parsing and editing?
I know zippers allow editing, but xpath gives a really easy way to have runtime-configurable statements like "Find all of element X where it has a child element Y with attribute Z having value A". I want to then be able to say "and delete the node (X)"
I use clojure.data.xml
for xml parsing / output, though I'm not doing any real heavy lifting.
Yeah, that's what we're using at the moment. But it really doesn't lend itself to runtime configuration of paths
With xpath you can do stuff like /elementA[/elementB/@attr = ("val1" "val2", "val3")]
to find the relevant nodes
With clojure.data.xml
as far as I have found I have to manually build functions to do that sort of thing and it's not easy to make it configurable.
Do we have syntax for having arguments to a function as an additional vector?
(defn some-fn [a b c :as input-vector] ...)
where input-vector
will be [a b c]
inside function body.For example, this works
(defn some-fn [& [a b c :as input-vector]] (println input-vector))
but allows more than 3 arguments.
Not that I can think of, built in. You can make one inside the function if you want, of course.
in other words, you want to enforce that the vector is exactly (or no more than?) length 3?
I had a function definition
(defn ->some-map
([a b c]
...)
([input-vector]
(apply ->some-map input-vector)))
I was thinking if I can get rid of the multi arity using some built-in destructing.I need the input-vector inside the 3-arity function for logging.
You mean a function that takes either 3 separate arguments, or 1 that is a vector, without defining it as a multi-arity function, and with automatic checking that number of args is 1 or 3? No, I really don't think that is built in.
And some people might suggest you be cautious about introducing such "punning" in args your functions accept, lest you confuse yourself or library clients later, but I won't go far in that direction.
I don't want to get rid of the arity, I just need the vector inside the 3-arity function to log it without constructing one myself with the three arguments. Nevermind, it's not that important. It will make it trickier to read the definition.
If your function took exactly 1 arg, and it was a vector with at least 3 elements, you could do destructuring for that 1 arg, but I don't think that will enforce exactly 3 elements in the vector, only at least 3.
user> (defn some-fn [& [a b c :as input-seq]]
(println input-seq))
#'user/some-fn
user> (some-fn 1 2 3 4)
(1 2 3 4)
nil
user>
Right.
@seancorfield It looks like the generated API docs are out of date for clojure.core.cache
- is that something you can update or is it part of some larger Clojure thing? Took me a while to figure out what was going on (the API docs don't have through-cache
and I was going a little crazy trying to figure out where it came from ๐ )
@shaun-mahood Technically it's a build infrastructure thing that's broken -- @alexmiller?
how to use :exclusions
in deps.edn ?
@octo221: like so
com.cognitect/transit-clj {:mvn/version "0.8.300" :exclusions [org.javassist/javassist]}
@shaun-mahood @seancorfield yes, itโs somewhat manual right now. But just pushed update http://clojure.github.io/core.cache/
What's a good word for a function that, like juxt, accepts multiple functions and returns a single function that accepts a vector (or sequence, I guess) and invokes each input function on its correspondent element in the sequence/vector? I find this can come up a lot in data pipelines. unoptimized version basically being (fn [& funcs] (partial map (fn [f x] (f x)) funcs))
this is a common enough usecase I wouldn't be surprised if I missed it already existing somewhere in core, too
I did consider a macro but it fails the "if you can do it without a macro, don't use a macro" test
The first rule of eval
club is: never use eval
๐
no macro necessary... you were close with the original code, but I think you should reexamine the semantics you want
but I want to augment the map with extra data that comes from applying a series of functions
I've found something similar to https://aphyr.github.io/tesser/tesser.core.html#var-facet useful a few times (but I've never actually used tesser)
{:firstname "Ghadi" :lastname "Shayban"}
;;; :full-name (fn [m] (str (:firstname m) " " (:lastname m)))
;;; .... more keys + functions
Have a look at: https://github.com/robertluo/fun-map
this is primarily for scenarios wherein you have functions that operate on one argument and you need to use them in a composition or transducer pipeline that needs to operate on tuples
thanks. come to think of it I've seen this kind of thing called "project" before for map-like structures
I'm surprised other people don't run into the usecase for mine more often - any time you're using pipelines that can only pass a single container and you're forced to instead give a vector or fixed sequence, basically