Fork me on GitHub
#beginners
<
2017-11-08
>
tjmaynes05:11:51

hey i’m trying my hand at clojure. I was wondering if there was a cleaner way to capitalize a list of names? https://gist.github.com/tjmaynes/92296bb41f026b305667f3868b65e249 I’m trying to avoid stateful behavior by passing the result of one transformation to the next.

tjmaynes06:11:02

what bothers me also is that i’m partitioning the flattened list to a list of lists (of size 2). If a person includes their middle name (or potentially many other names) (for instance ((“tj” “maynes”), (“big” “e” “smalls”)), then the partition will crop the “Smalls” in “Big E Smalls” into another list.

phronmophobic06:11:31

looks pretty good

phronmophobic06:11:53

the (reduce into []) isn’t necessary

phronmophobic06:11:27

there’s a couple of ways around the partitioning issue

tjmaynes06:11:03

this may be a super noob question but: if the (reduce into []) is not necessary, then how can I capitalize over this data (["Bruce" "wayne"] ["peter" "Parker"] ["your" "dad"]) (list of vectors).

tjmaynes06:11:52

If i can remove the (reduce into []), then I don’t have a partition problem anymore

phronmophobic06:11:00

oh sorry, I had made another change that made it unnecessary

phronmophobic06:11:17

I had changed the step before to use mapcat

phronmophobic06:11:24

which combines the two steps

phronmophobic06:11:48

one way to get around the partitioning issue is to skip the reduce/mapcat step

(defn capitalize-names
  [names]
  (->>  names
        (map #(->> (string/split % #"\s+" )
                   (map string/capitalize)))))

phronmophobic06:11:04

but that’s not necessarily a good way to write it

tjmaynes06:11:07

really digging this abstraction:

(map #(->> (string/split % #"\s+" )
           (map string/capitalize)))

phronmophobic06:11:51

(defn capitalize-name
  [name]
  (->> (string/split name #"\s+" )
       (map string/capitalize)))

(->> (map :real identities)
     (map capitalize-name))

phronmophobic06:11:01

I think that’s a little easier to fiture out what’s going on

phronmophobic06:11:59

I think it’s easier to think about a function that works on one item and then use map to transform a sequence of items

phronmophobic06:11:27

then to have a function that wants to receive a list of items that it will transform on a per item basis

tjmaynes06:11:50

i think i’m still getting used to that kind of thinking. hah

sundarj10:11:16

and you can easily turn the single-item function into a collection function later if you so choose by doing (partial map f) (or (fn [coll] (map f coll)))

sundarj10:11:35

sort of giving the function a new power of working on more than one item

phronmophobic06:11:42

it’s really weird (at first)

phronmophobic06:11:39

and then at some point, you can’t remember why you would do it any other way

pablore15:11:42

any spacemacs users here? I need help debugging a large process (breakpoint and inspecting). I know spacemacs comes with the Sayid debugger but I dont know how to use it. Any other debugger/editor/tools that you think is better is welcome

jumar20:11:00

@U7V9HE682 afaik, spacemacs doesn't come with Sayid out of the box - you have to add it. Most of the time, cider-debugger is sufficient. Check the first episode of my new screencast: https://curiousprogrammer.net/2017/11/05/introducing-clojure-tip-of-the-day-screencast/

pablore22:11:56

Sayid comes with spacemacs in the developer branch.

jumar07:11:23

nevertheless, I used sayid before and while it's a great tool I've found it a bit overwhelming. I've found that I don't really need it for most of my debugging/tracing use cases.

fabrao17:11:59

hello all, is there any way to replacing many string in a string? Like "Hello dot My name is Jonh dobuledot" -> "Hello . My name is Jonh :" ?

seancorfield17:11:34

@fabrao I would just thread the string through a series of (str/replace ...) calls...

fabrao17:11:41

ok, thanks

seancorfield17:11:09

(-> "Hello dot My name is Jonh doubledot" (str/replace "doubledot" ":") (str/replace "dot" "."))
user=> "Hello . My name is Jonh :"

fabrao17:11:15

humm, I got it

fabrao17:11:34

so, how to convert string to #string ?

fabrao17:11:03

isn´t that "dot" a #"dot" ?

seancorfield17:11:33

Or just use the #".." literal syntax if you have a literal.

seancorfield17:11:51

I just used strings with str/replace because that's all your example seemed to need.

fabrao17:11:26

ok, understood, thanks alot

felipebarros18:11:22

Does anybody know of a tool like reveal.js (https://github.com/hakimel/reveal.js) in CLJS? It's a PowerPoint substitute in HTML/CSS/JS.

noisesmith18:11:21

one of the main things about cljs is you can just use js things pretty easily

josh.freckleton19:11:08

in Selmer, is there a function that can return all keys found in a template? "{{a}}, {{b}}, {{c}}" ====> [:a :b :c] (I've looked through the source, nothing jumps out as being able to do this, and rolling my own parser defeats the purpose of using selmer)

noisesmith19:11:10

hack idea: reify the method that looks up a key on a map to return “foo” and store the key looked up in an atom, then check the contents of the atom after passing it to a render

noisesmith19:11:18

probably a terrible idea but might be fun

josh.freckleton19:11:07

haha, shoot. not a bad idea, but... a bit hacky for this

felipebarros19:11:31

@noisesmith When people say "easy" when talking about programming it normally means I'll have a really hard time doing that.

noisesmith19:11:11

@anantpaatra one of the main design goals with clojurescript is that you should be able to use javascript directly

noisesmith19:11:53

@anantpaatra for example, this is how you use the javascript Date class:

cljs.user=> (js/Date.)
#inst "2017-11-08T19:05:28.040-00:00"

felipebarros19:11:50

I guess my main problem right now is setting up the boilerplate code, understanding how things are tangled together. In HTML/JS it feels so clear, but in CLJS everything feels cryptic.

felipebarros19:11:11

This is why I thought a ready-made project would help me with some README instructions or whatnot.

noisesmith19:11:24

@anantpaatra the interop syntax is something you can learn in half an hour, and master in a week of regular usage easily

noisesmith19:11:32

it’s not complicated, it’s just different from what you are used to

felipebarros19:11:34

I've been in and out of CLJ/CLJS for an year and still don't. Maybe I'm just really, really dumb. Or I do something really wrong. Probably both.

noisesmith19:11:42

the problem with immediately seeking wrappers instead of using the javascript directly is that you end up using broken libraries (most wrapper libs are incomplete at least, if not broken in some corner case), and in the long run it wastes your time

noisesmith19:11:02

if you put in the effort to do interop, it’s easier than managing the hacky wrapper libs

felipebarros19:11:28

That is good advice, I'll try doing it with plain interop.

stvnmllr222:11:00

Anyone know a good resource for clojurescript, or even javascript to deal with dates between the UI and the database? Just noticed my dates come across with transit in UNIX time, but when they create a date in the browser that time is for the timezone, so ends up being a different day sometimes (in this case i only care about dates)

aconanlai22:11:40

moment.js is the standard if you need timezones

stvnmllr222:11:09

Thanks @aconanlai I'm using moment. But in this case I don't need timezones, so that's the problem.

stvnmllr222:11:41

If my browser is in a different timezone than the server, i have issues when I read it back out of the db and put on the page.

stvnmllr222:11:15

through a rest api over transit. I can think of ways to fix it, but thought there would be a standard somewhere, or convention.

aconanlai22:11:40

your browser should be storing time in a timezone-agnostic format like ISO or unix time

aconanlai22:11:27

your server**

aconanlai22:11:29

i meant, sorry

stvnmllr222:11:58

yes, unix time in the db. which probably converts to java dates before transit. so 1510185600000 (midnight in UTC) turns into Wed Nov 08 2017 19:00:00 GMT-0500 (Eastern Standard Time) http://currentmillis.com/?1510185600000

stvnmllr222:11:02

All i care about is Nov 9th

aconanlai22:11:28

so you do care about timezone

stvnmllr222:11:32

that's what i need. so i guess i can just take the date and figure out the current timezone and add the offset to get back to UTC. That's one idea.

stvnmllr222:11:56

No matter where this person opens the app, they just see Nov 9th

stvnmllr222:11:02

I want them to at least

aconanlai22:11:17

moment-timezone will take care of this for you, the vanilla JS ways are a bit janky

stvnmllr222:11:18

ok, if that is your suggestion, i'll give it a go. just was surprised I hit this with such a simple use case. My first SPA, server side rendering is easy 🙂

aconanlai22:11:18

it's something like moment().tz('America/Los_Angeles').format();

aconanlai22:11:36

have you worked with javascript dates on client-side applications before?

stvnmllr222:11:56

not much, been doing server side for a long long time

aconanlai22:11:07

1510185600000 is November 8th

aconanlai22:11:13

according to the user's browser

stvnmllr222:11:57

I know. that's my prob. I can change it server side before it hits them to be a js string i guess that is utc, but seemed strange way to do it

stvnmllr222:11:50

since i'm just querying out of postgresql, to a map, and straight to transit

aconanlai22:11:47

you want to be passing a timezone agnostic date format over to the client and formatting it from there

aconanlai22:11:14

and since you want to display a date to the user in a timezone that their computer is not necessarily on, you need to convert it

stvnmllr222:11:24

so is 1510185600000 timezone agnostic?

stvnmllr222:11:52

ok. well.. at least i sort of know I'm on the right path. Thanks for the time.