Fork me on GitHub
#clojure
<
2018-04-07
>
matan01:04:20

Other than a for, how would I make this kind of nested mapping more elegant to look at?

(defn OpenSubtitles-parse [file-path]
   (let [root (-> file-path xml-reader zip/xml-zip)]
       {:documents
           (map
              (fn [el]
                 (map
                     (fn [el] (map zip-xml/text (zip-xml/xml-> el :w)))
                     (zip-xml/xml-> el :s)))
              (zip-xml/xml-> root :document))}))

Michael Fiano01:04:27

By using threading macros

hiredman02:04:09

nah, just use for, it is great

hiredman02:04:34

or not, for would be better if you had mapcats in there

matan02:04:15

I'll see if thread-macroing it looks better..

matan02:04:34

Thanks πŸ™‚

hiredman02:04:48

I don't know that threading would be help there either

hiredman02:04:59

you might look at something like specter or a lense library, which would let you expression the transform in a more succinct way (not 100% sure about that)

matan02:04:54

This is the first time I'm using zip-xml, (or clojure's zippers at all), and it kind of begs something on top like specter, as it is kind of hard to read after being hacked together

matan05:04:57

Small humorous rant: 4 hours learning how to process huge XML files with clojure... Any idea why xml-zip is part of clojure.zip and not clojure.data.zip.xml? my ensuing code using xml zippers is barely readable in hindsight. I have found https://github.com/clojuredocs/guides/blob/master/articles/tutorials/parsing_xml_with_zippers.md at par with Scala API πŸ™‚

seancorfield05:04:21

@matan I sympathize. I've written just one XML parser with zippers and it was a mind-bending exercise. I still find the code fairly impenetrable.

matan05:04:55

@seancorfield any good game-plan for the next time around? I guess you don't parse XML much.. thanks for the sympathy anyway πŸ™‚

seancorfield05:04:15

I try to avoid parsing XML at all costs 😞

matan05:04:44

you know... if you get one, you have to parse it

matan05:04:58

I'll try avoiding places where people hand over XML files..

seancorfield05:04:04

I've never found a really elegant solution, in any technology.

seancorfield05:04:05

If you're just looking for specific parts of an XML document, maybe Enlive would help? Since then you could use selector syntax to get at the parts you wanted...

seancorfield05:04:41

But if you're trying to turn entire XML documents into data structures, there's not going to help you 😐

matan06:04:03

πŸ‘ Enlive it is, next time around

theeternalpulse06:04:59

With specter, is there an easy way to reference another path in a transform?

schmee08:04:04

paths are first-class, so you can do:

(def your-path
  (path ALL ALL :something))

(transform your-path f thing)
; combine many
(transform [:thing your-path ALL] f thing)

dominicm07:04:54

@matan I can recommend giving odin a spin, https://github.com/halgari/odin

matan09:04:06

Hey thanks @dominicm note taken

matan09:04:49

@dominicm that your idea of vacationing? 🌴 πŸ™‚

dominicm09:04:56

@matan Fiddling with my vim plugin & burning time on slack, what else is there in life?

dominicm09:04:31

I've used odin recently btw, it still works πŸ™‚

matan09:04:52

πŸ™‚ lol

matan09:04:10

You need to take real time off πŸ™‚

matan09:04:07

One day you will no longer be able to code, and it will all look more towards meaningless to you in hindsight, if you didn't leave time to do some other things

dominicm09:04:23

I'm kidding, don't worry. I very much appreciate that I need to take real time. I've barely touched my laptop this week, I spent 2 days in disneyland, and a bunch of time around that travelling there & back. I'm currently just killing some time waiting for my SO to get ready. I am trying to find new hobbies, learning Esperanto has been interesting.

matan09:04:07

πŸ™‚ enjoy your Esperanto!

matan09:04:55

For me, getting used to clojure syntax was enough language learning πŸ™‚ it has been more enticing than useful, as my coding velocity and productivity didn't experience a real surge switching to clojure, it just felt "pure" in a way

dominicm10:04:22

My brain was organized by switching to clojure, programming seems more transparent.

matan12:04:16

Me too. But productivity has not gone up. I think mainly to several factors: β€’ nils v.s. truthiness inconsistent in the language core API β€’ obscure to read code when using transducers, which are needed for stream processing and processing huge files β€’ chasing data shapes when refactoring code These all mean mediocre speed and productivity for me, so far. Even though I've become somewhat of an ace with functional programming. Community support is good though.

matan09:04:59

Anyway no commits over one year on master there..

qqq10:04:25

How do I rewrite groovy -> clojure the following two lines ? groovy: https://github.com/dkandalov/live-plugin/blob/master/src/plugin-util-groovy/liveplugin/implementation/Threads.groovy#L51-L52 the problem is that it is doing something with an ABSTRACT nested class, defined: https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/progress/Task.java#L241-L244 I have already tried: ` (com.intellij.openapi.progress.Task$Modal. nil "string" true) ` I suspect the reason this is not working yet is because this is an abstract class, and I somehow have to override the function. Advice ?

matan13:04:38

Given clojure code is data and it is homoiconic and all of that, where are the code refactoring libraries for clojure hiding? πŸ™‚ I seem to hand-refactor too much these days... is there anything notable out there, for refactoring moves of sorts? I'm using proto-repl as my editor BTW, but wouldn't care going command-line for it πŸ™‚ I've only heard of slamhound, which does something very specific (I think).

dominicm13:04:38

rewrite-clj is the general purpose library.

matan15:04:21

hey thanks!!! you should find nicer vacation targets than Disney πŸ™‚

matan15:04:45

Is it used by some prominent IDE / tools too?

dominicm16:04:03

My girlfriend always wanted to visit, it's her 21st, special exception.

dominicm16:04:58

I think refactor.el uses it. Clj-refactor.nvim and light table uses the cljs fork.

kaosko15:04:13

what's an idiomatic way to conditionally cache a result of a function call, say depending on an environment? it feels like a use case for delay but my every attempt at creating condition seems to produce some code smell. my best so far is to create a get-fn that either invokes the function or derefs the delay, but I expect there's a better construct and/or ready-made function for it, just haven't found it

schmee15:04:19

if you need more flexible caching (with eviction etc) take a look at https://github.com/clojure/core.memoize

schmee15:04:14

or if you need enterprise-grade πŸ˜„ https://github.com/ben-manes/caffeine

theeternalpulse16:04:49

I love these names, caffeine has never improved my memoization capability

johnj17:04:17

what does a star at the end of a function parameter indicates?

mfikes17:04:56

@lockdown- If often indicates that the function is a primitive variant often meant to be used by a macro, or for use in building a non-starred version.

mfikes17:04:33

(Reading your question again, @lockdown- I see I misread and thought you were referring to a star at the end of a function’s name. I don’t know what a star at the end of a parameter name might indicate.)

johnj17:04:39

yes, was referring to something like https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/try - but your answer taught something new anyway πŸ˜‰

johnj17:04:58

in the try example, could it mean zero or more?

mfikes17:04:00

Oh, in that case it is regex

danp18:04:55

Hi all, I've just cloned this git repo: https://github.com/wkf/hawk

danp18:04:58

I'm trying to run lein repl and then (in-ns 'hawk.core) but the directory structure is such that the .clj files are under src/main/clojure/hawk. I am guessing the structure is different to what I'm used to due to Java being in the repo too. Does anyone have any idea of how I can get to the hawk.core ns please?

dorab19:04:59

Did you (require 'hawk.core) before the (in-ns 'hawk.core)?

danp19:04:25

that'll be it, cheers @dorab πŸ™‚

kaosko19:04:26

@schmee thanks I did take a look at memoize. I guess my issue was more with caching conditionally, which memoize doesn't seem to solve any better

kaosko19:04:33

hmm for conditional caching well I guess I could do (cond-> (get-fn) cache? memoize), that looks pretty enough

schmee19:04:26

there are many ways to do it, a it/cond/case statement + memoize should do the trick

seancorfield19:04:11

@kaosko Maybe look at core.cache or core.memoize and the examples in the docs around those? They rely on a through function to encapsulate the conditional logic.

seancorfield19:04:58

(swap! my-cache core.cache/through-cache :some-key (fn [k] (code-to-produce-value k)))

kaosko21:04:19

@seancorfield thanks, yeah core.cache is good with proper eviction but have to stop overthinking this, especially since there was really no other reason but to try to make it prettier πŸ™‚

seancorfield22:04:32

Ah, I hadn't scrolled far enough back to see the earlier comments about core.memoize, sorry! @kaosko