This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-27
Channels
- # admin-announcements (1)
- # aleph (3)
- # beginners (72)
- # boot (67)
- # braveandtrue (1)
- # cider (17)
- # cljs-dev (8)
- # cljsfiddle (2)
- # cljsjs (1)
- # cljsrn (2)
- # clojure (19)
- # clojure-greece (1)
- # clojure-russia (42)
- # clojurescript (15)
- # core-logic (10)
- # cursive (6)
- # datomic (2)
- # hoplon (349)
- # kosmos (3)
- # lein-figwheel (3)
- # om (52)
- # onyx (3)
- # overtone (1)
- # parinfer (11)
- # proton (11)
- # protorepl (3)
- # reagent (29)
- # ring-swagger (2)
- # spacemacs (1)
- # testing (3)
- # uncomplicate (1)
I’d like to time an arbitrary function, send the time taken off via side-effect (to the SGG stack incidentally) and then return the result of the called function: Thusly:
(time-a-function '(db/get-record [1234])); => returns record
So far in my ignorance I’ve tried:
(defn time-a-function
[metric-name expression]
(def start (System/currentTimeMillis))
(def result (eval expression))
(send-to-sgg metric-name start)
result)
however, simply using eval
runs into namespace issues and I am guessing is the wrong tool for the job. Would anyone have any recommendations?First off, def
creates a global / top-level Var so you don't want to use it inside a defn
. Since you just want local bindings, you want let
there.
Second, since you want to pass in code that you want evaluated in place, you'd probably want a macro for this...
(defmacro time-a-function
[metric-name expression]
`(let [start# (System/currentTimeMillis)
result# ~expression]
(send-to-sgg ~metric-name start#)
result#))
Off the to of my head... untested...
Of course…. thank you. I believe you’ve already answered my next question about getting the start time too.
(oops, typo, edited)
Hope that at least gets you going in the right direction, even if it doesn't actually work.
Absolutely. I’ll start fiddling with it and go from there. I think I understand backticks in macros, but I’m going to have to do a bit more reading to fully understand the rest of the symbols and their applicability.
The #
is used to generate local symbols (if that helps).
It’s all good, I’ll just https://learnxinyminutes.com/docs/clojure-macros/ for a bit
Sometimes things that seem like a no-brainer can be hard in FP, and things that seem really difficult are easy.
For Haskell that’s really true. I’m finding Clojure to be not all that much of a jump from JS
One other question if I may. I’ve noticed that my use of macroexpand
works on things like ->
, but fails to expand the macros I write. Is this expected behaviour?
A macro is a macro -- yours should work the same as the ones in clojure.core.
Can you http://refheap.com an example of what you're trying that doesn't work?
Ah hell… My apologies, I’m going to have to run. It is a problem which can wait. Thank you for your help.
Hey, I'm working through Clojure for the Brave and true. Does this look right for implementing map using reduce? I first didn't use vectors and just reversed the list, but I'm not sure if this is better...
(defn map-reduce
[function data]
"An implementation of map using reduce"
(seq
(reduce (fn [x y] (conj x (function y))) [] data)))
I am seeing many problems with this... lol
@himmallright: can you link to the section of brave clojure?
It was just basically a challenge
embedded in a paragraph
I think the basic idea is that map and reduce are similar. reduce acting as a map builds a collection with f applied to each element of that initial collection
Took me a minute (too much food in me at the moment, moving slow), but I think an implementation like this is the idea:
(defn map-in-reduce [f coll]
(reduce
(fn [coll elem]
(into coll [(f elem)]))
[]
coll))
so you're basically removing reduce's generality by deciding in advance what the reduction will be.
But you always tell reduce
what the reduction will be, don’t you?
I mean (reduce + 0 (range 5))
is supplying a reduction of (fn [total elem] (+ total elem))
for example.
There’s a general principle here that you can create a more general function that can be used to implement both reduce
and map
...
It’s been a while but I think I was looking first at a function like this (defn chn [c h n l] (if (seq l) (c (h l) (chn c h n (rest l))) n))
And then a function r
that generalized the end-of-sequence test and the rest-of-sequence test… that took two more functions.
so (defn map [f l] (chn cons (comp f first) nil l))
I think and (defn reduce [f i l] (chn f first i l))
or something...
I think r
was something like this (defn r [p c h t n l] (if (p l) n (c (h l) (r p c h t n (t l)))))
I assume to some degree, so I guess a better phrasing is, is there a place to learn the convention being used?
I don’t know about conventional but those were acceptable names back in the 80’s when I was doing my postgrad work on FP… I don’t know whether I made them up or whether I got them from a book...
This was what I learned a lot of this stuff from http://www.amazon.com/Recursive-Programming-Techniques-Systems-programming/dp/0201144506
ok, I just dropped that into light table and with syntax highlighting and tabbing it reads easier
Yeah, I wish Slack had paredit mode or parinfer 😆
I think it's technically github flavored markdown? I've had triple-backticks fail on me to make block code before.
Yeah, I know about blocks of code — but want I really want is paren matching 😄 — I’m so spoiled by paredit mode in Emacs!
since it is now possible to make an interactive repl in clojurescript, somebody should make a clojure specific developer chat website that can also evaluate your code, and provide syntax highlighting and parinfer functionality. that would also make it really easy to discuss ideas with other people in a dynamic way as you can actually run some code and have them see what that does
That sort of thing is being discussed in #C0CB40N8K @adamkowalski
(or it was, I haven’t been paying attention to that channel recently)
awesome, I really think that could make learning the language much simpler
when I was learning haskell the irc channel had something called lambda bot which could eval arbitrary expressions
See also #C0J20813K I think which was one of the projects that spun out of the community discussions (and, I believe, is on the schedule for Clojure/West).
sometimes just browsing the history and seeing the ridiculously smart solutions people came up with to solve problems others had helped me learn a ton
The freenode IRC channel for Clojure has at least one bot that evaluates Clojure expressions and reports them back… I thought there was such a bot here as well…
Ah, there we go! /clj (map inc (range 5))
woah awesome
nevermind then haha, i guess I just never new about this bot
seems like not to many people use it
Because it can be annoying to constantly have evaluations posted into channels…
yeah thats true, but I feel like it can be really useful especially on a beginners channel where people might have a conceptual misunderstanding of how something works, and being able to demonstrate the way things evaluate can be exactly what somebody needs
You can go into a DM with @slackbot and use the /clj
command there if you want to experiment.
hey how do you use a npm library in clojurescript, as in on the front end not on the server
I used to use webpack for that, but now I am not sure how to go about this, I tried using lein-npm but it I am not sure how to make the modules available on the browser
No clue. Try #C03S1L9DN maybe?
sounds good
I think for now Im just going to download from source and include it into my project. I am trying to get highlight js to work but it turns out getting clojure highlighting is a little harder then I thought haha