Fork me on GitHub

I have a function that updates multiple values in a vector of maps:

(defn guard-awakes
  (update-in (update-in guard-map [guard :asleep] (fnil #(+ % slept-minutes) 0))
             [guard :minutes] (fnil #(concat % (range asleep-minute (+ asleep-minute slept-minutes))) ())))


It also creates an entry if the key doesn't exist. Is there a better way to update twice instead of this nested call to update-in?


This is an example data {{99 {:minutes (list 11 12 13), :asleep 3}} {10 {:minutes (), :asleep 8}}}


hey, I'm having some troubles with debugging clojurescript. is the preferred way using figwheel and a repl or just figwheel? I can't seem to be able to connect my emacs to a cljs repl via cider, so I'm just using figwheel for now


@nikola.kasev you could make that more readable with a threading macro (`->`).

(-> guard-map
    (update-in [guard :asleep] (fnil + 0) slept-minutes)
    (update-in [guard :minutes] (fnil concat '()) (range asleep-minute (+ asleep-minute slept-minutes))))
If you want to update twice in the same path then you could do something like:
(update guard-map
        #(-> %
             (update :asleep (fnil + 0) slept-minutes)
             (update :minutes (fnil concat '()) (range asleep-minute (+ asleep-minute slept-minutes)))))

Ali Ebadian14:12:06

how do you guys debug and find out where in the chain of functions an error i occuring?


I had this talk sent to me when I first started working in Clojure, and I think that it has been the most useful piece of knowledge in my arsenal.;t=1213s Aside from that, if I'm trying to find the source of a problem in a large chain of functions, I try to use a binary search method. i.e. add print statements in the code to try and determine where the error is occurring, but in an intelligent way. Debugging will go faster if you can eliminate large chunks of code at a time. For example, if you have 100 functions and no idea/intuition on where the error may be, then start by trying to verify if the result at the 50th function is right or wrong. This allows you to eliminate half of the functions which you were previously considering. Then repeat that until the error is found! One of the very nice aspects of having pure functions is the ability to easily verify if they are working. They are simple to test, and you can generally debug them in the REPL

Drew Verlee00:01:06

I dont have much to add to what enforser is saying. when there is complex logic i usually use a threading macro

-> start
     map a
     filter b
     reduce c
Now if something is off, i just walk down the macro and comment things and explore the output.

Drew Verlee00:01:42

if that thread is part of a larger function i pull it out in my editor and do it from there

Drew Verlee00:01:33

there are several tools that can produce input data and show data along the way

Drew Verlee00:01:42

the most powerful of the generators is specs

Drew Verlee00:01:22

but a more light weight approach (usually enough) is to just capture some actual data that was fed to that function

Drew Verlee00:01:37

IMO the trick is to not have to debug. Must of what needs to be debugged is usually a product of approaching the problem in the wrong way in the first place.


^^ No need to use fnil on concat. It's nil punned away. (concat nil [1] nil [2]) returns (1 2)


@nikola.kasev It seems like :asleep is just the count of :minutes and :minutes seems to track the minutes which the guard slept in a particular hour. You can calculate :asleep when you want by calling count on the value of :minutes. Also, a set makes more sense for :minutes.


@shin I would suggest watching to see a clojurescript workflow with figwheel. Cider should be capable of connecting to a cljs repl, but you may need to add some additional middleware. I would try #emacs or #figwheel for help getting the cljs repl setup


@trailcapital thank you! I'll check the video out first


Just remembered about this one as well:;t=2012s It goes a little more in depth, if you feel like the previously linked one was a little lacking.


@trailcapital thanks, your suggestion is much more readable than mine


Check out process-sleeps&wakes


@dpsutton haha, so you also did AoC 🙂


Some of it. These ended up taking more time and feeling more like work this year


It's been a very rewarding experience for me, I'm learning Clojure by solving the problems


with much help from the community of course


Yes it is a fun experience to do it with several people and discuss and see other approaches to the same problem


How does it compare: the code one writes for AoC and in a real-world Clojure job?


More specifically: would it be easy to work on a real-world Clojure project if I've done most of AoC?


I don't think real world is ever as clean as fun problems like aoc. There's always more error states and dirty data and special cases. But being fluent in the language is always beneficial

Kelly Innes16:12:20

However I am also still just learning so I don't know how idiomatic that is

Kelly Innes16:12:11

One thing that seems real-world-useful about AoC is that it really makes you read and interpret requirements really carefully -- missing a single word somewhere seemed like it would cause me to go down a wrong path in implementing the solution.


@kinnes FWIW, I've tried to write my AoC solutions in an idiomatic fashion, optimizing for clarity / simplicity and not so much for performance. Here are mine if interested

👍 8
Kelly Innes16:12:55

Awesome! Will take a look at those.

Lucas Barbosa17:12:13

Is it worth creating protocols/records when creating "model" objects in an application? For instance, if I have a blog web app, and I want to have a map that represents a blog post: is there any advantage to create a record representing posts instead of simply creating the functions that create and manipulate a hash-map that represents a post?

Lucas Barbosa17:12:02

It feels like the record mechanisms reduce the boilerplate, but is there any other advantage?


@lvbarbosa Start with regular maps. Switch up to protocols / records if you need them.


I often think about using protocols as a way of allowing external parties to extend my abstractions. if I control all the code, I rarely feel the need to use such things

❤️ 4

i was thinking about writing my own symbol generator, i mainly need unique IDs for storing things and retrieval. integers makes the most sense (especially for map lookups) .. are string lookups in a map about the same speed as a int? I can generate symbols like 12345 or nf_12345 and (gensym "nf_") makes that easy (the latter)... any opinions?


I would maybe back up a few steps


like, do actually need two types of ids?


(symbols + whatever key you choose for the maps), like if you were to use numbers to key the map, why not just numbers as the key and not bother with symbols at all?


key performance wise, I suspect ints would be faster, followed by keywords, then symbols, but the difference may not be that much. you may want to look at


@hiredman [thoughts] i suspect ints would be much faster for a lookup, but in a map of n size, i don't think string indexing would take much longer... i could use ints to store things and retrieve them, is there an easy way for me to generate unique ints ? i was thinking about just starting with some number and doing (inc x) to get ids but maybe there is a better way to generate a unique id integer. my guess is that in gensym there must be some sort of hash against the time that is monotonically increasing, but i'm not sure. so that might be one way. or just increment a digit and keep track of it, that is also an easy fix.

sova-soars-the-sora19:12:36 looks like overkill at the moment when i'm simply trying to put wheels on my sk8board. but i shall keep it in mind! and make a note of it in my


if is overkill then just use java.util.UUID/randomUUID for keys


UUIDs are so laborious for a URL though


i'm thinking ~7-15 digits


use a different encoding


when would I need a -> macro instead of a simple function that feeds its first argument to the reverse composition of the rest of its args which are all functions?


(feed x f g h)

Kelly Innes23:12:44

I think it's an ergonomic convenience?


the -> macro manipulates syntax, which is very different from what a function would do, but the result is often the same


the problem is, I often find the -> macro less readable, so it's not ergonomic at least to me


but for example: (-> 10 (Long/toString 16))


(Long/toString 16) is not a function value you can pass as an argument and apply


in general, when you learn new things you should expect them to be new


(feed 10 #(Long/toString % 16))


right, and that is exactly the difference


if i happen to want to add a function that takes its argument second i would need to go over and change -> to as->


this happens very often