Fork me on GitHub
#clojure-uk
<
2017-04-20
>
thomas07:04:41

morning 😼 mogge

maleghast08:04:45

Hello All πŸ™‚

jimmy08:04:22

hi all πŸ˜„

Rachel Westmacott09:04:40

rcfotd:

-------------------------
clojure.core/get-method
([multifn dispatch-val])
  Given a multimethod and a dispatch value, returns the dispatch fn
  that would apply to that value, or nil if none apply and no default

dominicm09:04:42

I used that this morning, oddly enough

Rachel Westmacott09:04:41

really? it seems pretty niche

dominicm09:04:58

It is πŸ˜›. Quite a coincidence.

madstap14:04:17

Not sure how nice the solution is, feedback welcome

yogidevbear14:04:29

@madstap That's awesome πŸ™‚ Thanks for taking the time to look into it πŸ‘

yogidevbear14:04:08

I feel like a #friday-puzzler channel in this slack group could be really interesting (esp. with so many active members)

dominicm14:04:27

Oh man, I never did it.

Rachel Westmacott14:04:42

(defn f [ns total]
  (get (group-by #(reduce + %)
                 (mapcat clojure.math.combinatorics/combinations 
                         (repeat ns) 
                         (range 1 (inc (count ns)))))
       total))
?

yogidevbear14:04:30

@madstap so when looking at the results of (f [1 2 3 4 5] 5) and getting #{{3 1, 2 1} {4 1, 1 1} {5 1}}, am I reading the results correctly that it's one instance of 3 (`#{{3 1,`...) plus one instance of 2 (...`2 1},`...) equals 5?

yogidevbear14:04:11

@peterwestmacott If I want to include the combinations namespace within a lein repl, do you know what the command is?

Rachel Westmacott14:04:37

(require 'clojure.math.combinatorics)

Rachel Westmacott14:04:43

but you maybe want to alias it to something: (require '[clojure.math.combinatorics :as combos])

dominicm14:04:50

That's pretty cool.

yogidevbear15:04:28

FileNotFoundException Could not locate clojure/math/combinatorics__init.class or clojure/math/combinatorics.clj on classpath. clojure.lang.RT.load (RT.java:456)

madstap15:04:30

I think he means that it's not included in vanilla clojure

Rachel Westmacott15:04:55

you might need this in your project.clj: [org.clojure/math.combinatorics "0.1.4"]

yogidevbear15:04:21

Yeah, I'm trying to get it as an entire script I can drop into a repl for simplicity sake for demoing to people that haven't got an environment set up

madstap15:04:35

I think you might be able to set it up with klipse in the browser

dominicm15:04:41

Supports cljs. I wonder if you couβ€” that ^

Rachel Westmacott15:04:58

(eval (slurp "raw.githubusercontent.com..."))?

madstap15:04:11

@peterwestmacott That just returns a string of the file...

madstap15:04:26

And if I do read-string, it just reads the first form

Rachel Westmacott15:04:28

in my haste I forgot the call to read-string

Rachel Westmacott15:04:26

the point is that there will be a way to load the ns from a URL - but it probably isn’t what you actually want to do

madstap15:04:38

But if I wanted to do that hack, how do I read all the forms to pass to eval? read-string just reads the first form

Rachel Westmacott15:04:03

I'm not sure its going to work - the file you need is a cljc file and it doesn't seem to read nicely.

madstap15:04:23

RIght, reader conditionals...

Rachel Westmacott15:04:42

how much setup are you prepared to accomodate?

Rachel Westmacott15:04:04

if they have a repl running, did they install leiningen?

Rachel Westmacott15:04:06

if you can slurp/spit the content to the right place (somewhere on the classpath, with the right filename for the namespace) then you can require it as normal.

dominicm16:04:59

If you use boot it's just boot -d org.clojure/math.combinatorics:0.1.4 --file main.clj πŸ˜‰

dominicm16:04:20

#!/usr/bin/env boot
(set-env! :dependencies '[[org.clojure/math.combinatorics "0.1.4"]])
(require 'clojure.math.combinatorics)

(defn variations
  [numbers sum]
  (filter #(= sum (apply + %)) (clojure.math.combinatorics/subsets numbers)))

(defn -main [& args]
  (prn (variations [1 2 2 3 5] 5)))
Save this as "anything.clj" and either do boot -f anything.clj or chmod +x anything.clj followed by ./anything.clj πŸ™‚

dominicm16:04:29

boot is great for scripts

dominicm16:04:25

I have a feeling I missed something given @peterwestmacott's solution being different to mine, that I've missed something. Not sure though πŸ™‚

yogidevbear16:04:15

Thanks Dominic πŸ‘ I'll give it a go later and share the solution on the puzzle gist if you're okay with me sharing πŸ™‚

dominicm16:04:12

yeah go ahead.

maleghast18:04:32

@jonpither I really enjoyed the article, thanks for the link earlier...

maleghast18:04:44

@jonpither Hmmm... I can't find it...

jonpither18:04:54

hmm, prob missed the target

maleghast18:04:39

Oh well 😞