This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-17
Channels
- # beginners (10)
- # boot (9)
- # cider (1)
- # cljs-dev (191)
- # clojure (77)
- # clojure-russia (4)
- # clojure-serbia (10)
- # clojure-spec (19)
- # clojure-uk (4)
- # clojurescript (16)
- # core-async (9)
- # cursive (1)
- # data-science (1)
- # datomic (3)
- # emacs (42)
- # graphql (2)
- # hoplon (38)
- # jobs (1)
- # jobs-discuss (18)
- # luminus (3)
- # lumo (20)
- # off-topic (9)
- # om (51)
- # parinfer (3)
- # pedestal (8)
- # re-frame (4)
- # reagent (7)
- # rum (9)
- # sql (9)
- # untangled (81)
Is there a transducer, outside of christophe grand’s x/into, that adds elements? I have a blacklist xf that removes stuff, and want a whitelist xf.
mapcat
is a transducer, and can return 0 or more items for each call so it effectively can insert new items into a result
@lvh A transducer is just a function that, if called with a reducing function, will return a reducing function. So actually, identity
is a transducer.
Fun fact: this also works:
(reduce
((map (fn [x] x)) conj)
[] (range 3))
(transduce
(constantly conj)
nil [] (range 3))
So, after few projects with clojure, I still find the following task ridiculously inefficient compared to some other languages: figuring out the structure of some data when coming back to code no longer in my mental buffers. Any expert advice?
As an aside on it, I have found clojure.inspect/inspect-tree
quite awkward in its display paradigm, after several hours of usage still haven't gotten used to the way it redundantly details the data as you drill in. Although I perfectly understand it sits well with some smarter people, I find it horrific for drawing the structure of a piece of data without getting lost 🙂
@matan Rigth after 90% of my functions I add example invocation with example data. Example:
(defn preload-items [xs]
....)
#_(preload-items (db/items-by-filter :item/due true))
Often even multiple #_
comments. Makes it super easy to REPL. Partially eval the argument to see what is a good example of being passed. New developer cans quickly get familiar
@matan spec plus a visualizer like https://github.com/jebberjeb/specviz
also I would love to see a (or many) better inspect-tree choices 🙂
@rauh @stuarthalloway thanks. I am still waiting for 1.9 to get started with clojure.spec
I usually add tests next to non-obvious functions, but vai with-test
not as comments @rauh
That doesn't solve my itch though when data has non trivial structure.
@matan I haven’t tried https://github.com/tonsky/clojure-future-spec but you might consider that for 1.8
Yes I know about it, but have been burned by chasing changes in backported libraries before
@matan The comments aren't really test, they're examples. Without side-effects obviously.
Even a spec can be tough to read, and the way that everything is kind of a lookup in the registry doesn't make it easy to understand at first sight. So I don't think it'd help in your use case
You can eval every arg separately in your REPL on the example invokation. That gives you the data that the function expects right away.
Sometimes I wish to traverse the data, not only know the structure, in e.g. javascript, the browser console lets you do that. Maybe I should tweak inspect-tree towards more standard behavior 🙂 not really sure where in the code does it determine what/how to display for child nodes when expanded. https://github.com/clojure/clojure/blob/f572a60262852af68cdb561784a517143a5847cf/src/clj/clojure/inspector.clj#L91 I guess @alexmiller would know 🙂
@rauh admittedly I prefer tests, they live with your code. somehow people in clojure think that once they've written a function and tested it in the repl, it will work forever 🙂 maybe they don't work much in teams. But thanks for the suggestions!
@matan I don’t think it is an “individual vs. team” problem — it is more about whether you change working code, or leave it alone
@matan a better inspect-tree would be awesome! 🙂
@stuarthalloway The code of clojure inspect is quite simple, but I get lost as to where it determines what to show for a node
@matan replace it with a multimethod driven by your data
then you can have a default impl that does something plain
and open dispatch to plugging in renderers
of course once you do that, it is likely that the best way to choose a renderer is … by spec! 🙂
hi! maybe a very basic question, but what's the shortcut to exit the stacktrace view of cider? (apart from C-x K to kill buffer)
(defn destructured [& {:keys [a b]
:or {a 1 b 2} :as env}]
env)
(destructured)
=> nil
Is there a way to collect the env along with the or values so that env returns
=> {:a 1 :b 2}
@hlolli how to find out such btw? I've looked through the menus and did not find "h" next to any of the menu items..
is there maybe a "show all shortcuts that make sense in the current context" function?
Maybe there exists one, as for the stacktrace buffer, it's common in emacs when you compile, that you get a buffer that is exitable with q
like when you install a package via package-install
. Many years of using emacs and googling stackoverflow helps.
there's an emacs command that shows local keybindings
@hlolli you would need to construct the map by hand, I would use merge instead of or in that case
(defn destructured
[& {:keys [a b] :as env}]
(merge {:a 1 :b 2} env))
@noisesmith thanks, I think I'll go for merge. Was bit unintuitive behaviour I thought.
:as is meant to return your original input
yeah, usually if I use :as it's because I want to use parts of it that aren't captured in the destructure itself, later on
methods are not first class data on the vm
you need an object to put on the stack, a clojure function is an object so it works
clojure could hide this by automatically turning method names as args into anonymous functions, but instead exposes this
A static method name might correspond to numerous Method objects (differing by arity) so it's not clear what Object to use . . . so the anonymous function would be inefficient
if automatically generated? yeah
wait, is there actually such thing as a Method object? (other than a representation used by reflection...)
OK - thanks for clarifying - because if Methods were directly things my logic (and my understanding of how any of this works) kind of falls apart
that's a corner of the vm I keep intending to learn more about
using cljs + figwheel + reagent. after adding a new dependency in a namespace using require, do i need to do anything else? if i reload the page i get errors about ReactDOM missing, despite everything being ok before the page load.
if you changed project.clj to add a new dep, that often requires a clean restart of figwheel in my experience
yeah, that's how I typically do it
because figwheel uses your project.clj to start up, and I'm not sure it can hot-reload properly when it changes
I admit, a lot of my workflow around figwheel is voodoo / habit
i must have something else borked. i've had reagent anyway, was working until i reloaded the page
removing react-datetime from project.clj seemed to fix it. maybe it was clobbering something in reagent
OK, I think I've lost my mind with some core.async
fun, but I've narrowed down the problem. I have a program that's 2 threads: One does put!
to clients expecting work (it also does a heartbeat with alts!
). It gets this work from some HTTP POST
s. The other does <!!
expecting the work results. The problem is that the put!
seems to be getting eval'd after the <!!
, leading to all kinds of weird out-of-order problems. How do I force things to be eval'd immediately?
why would calling <!! in one thread and put! in another later cause any order problems?
you don't provide enough info to know what the fix is, but the actual purpose of core.async is to coordinate between threads...
That's what I'm wondering, too. (It's actually several layers deep involving Server-Sent events, but I'm trying to be brief. I wrote about it here: https://groups.google.com/forum/#!topic/pedestal-users/XPQ_OnRErDA)
so that's just the code writing to the channels - I probably would need to understand pedestal to understand the consuming side...