Fork me on GitHub
#off-topic
<
2018-10-29
>
Daniel Hines13:10:24

Is there anything in vanilla JavaScript like clojure spec's ability to destructure based on a spec?

borkdude13:10:33

destructure based on a spec: example?

borkdude13:10:47

clojure.spec.alpha?

borkdude13:10:21

Do you have an example of this in clj/cljs?

Daniel Hines13:10:47

Let me find one...

Daniel Hines13:10:11

Towards the end of the section, it shows how to use s/conform to pull destructure data according to a spec.

borkdude13:10:01

ah that kind of destructuring. I hoped there would be an integration with let or fn destructuring and spec I wasn’t aware of 🙂

☝️ 4
borkdude13:10:39

someone I know has made a spec-ish implemenation for node: https://github.com/mrijk/speculaas/tree/master/lib

borkdude13:10:11

there may be others around?

Daniel Hines13:10:55

That's cool, actually. I'm not running in node though. I'm hoping for something that lets me leverage JSONSchema, because that would work well with existing tooling. But I'll still check that lib out for future reference!

Daniel Hines17:10:36

Wow, I didn't know about spec-tools. That's really cool...

valtteri13:10:57

Joi used to be my favourite lib for schemas and validation in JS https://github.com/hapijs/joi

👍 4
emccue14:10:54

Ending section got me thinking

emccue14:10:29

What are the tools in clojure for making library code not monolithic?

devo15:10:48

Additionally records and protocols can be used for a more OO-like top level API.

jaihindhreddy-duplicate16:10:24

My team wanted to find out the set of key sets our customers were sending at a particular endpoint. After some data munging I got a json file with an array of requests. Halfway through writing a script to do this in javascript, it clicked. Opened the REPL and did #(->> % (map (comp set keys)) set) Then we saw a wonky set and wanted to see how frequently it was appearing. It was one symbol change away #(->> % (map (comp set keys)) frequencies) Damn I love Clojure! clj

❤️ 20
lisphug 12
jaihindhreddy-duplicate16:10:09

The value of values is indeed immense

ljosa16:10:06

@jaihindh.reddy: It gets even better when you hook it up to a Spark cluster with Flambo, or put up a web-based REPL capable of vizard visualizations. At one point we even had the CFO learning a little clojure so he could play with the data…

bubblebobble 4
jaihindhreddy-duplicate16:10:07

Absolutely. I got the value of values philosophically when I watched Rich's talk but when I actually wrote that snippet. I felt it, viscerally. It's almost addictive, in the best of ways.

eggsyntax18:10:49

Anyone have a recommendation on the best article-length introduction to what functional programming is, and why it matters? There's so many of them out there, and I've entirely lost track of which ones are decent.

eggsyntax18:10:53

Oh, that's right, @maryrosecook wrote a really great FP intro. I'll start by recommending that, but other ideas super-welcome! https://maryrosecook.com/blog/post/a-practical-introduction-to-functional-programming

jaihindhreddy-duplicate20:10:13

John Hughes - Why functional programming matters

eggsyntax20:10:03

^ That's a terrific paper, but not the best introduction to the idea for the audience I'm thinking of. I'm looking for things that are a bit lighter.

4
ljosa20:10:38

Not an article, but hard to beat SICP.

👍 4
eggsyntax20:10:58

Truth! But for most people, I think you have to give them at least some idea of the advantages before you can convince them to read SICP 😉

hiredman20:10:45

it is hard, because the reasons for preferring functional programming aren't particularly light

hiredman20:10:13

my first thought was something like https://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf but I don't think that is light

hiredman20:10:40

http://curtclifton.net/papers/MoseleyMarks06a.pdf (out of the tarpit) is cited a lot too, but I don't think it could be called light either

eggsyntax20:10:37

Heh. Y'all are too smart to give me the answers I'm looking for 😉 I was hoping specifically to exclude "Why FP matters" and "Out of the Tarpit" by saying "article-length". It's for a fairly novice programmer, and I think he'll need to see some of the advantages before he's ready & motivated to take on the deeper stuff. I've pointed him to the Cook article I posted above, and also to http://blog.jenkster.com/2015/12/what-is-functional-programming.html

eggsyntax20:10:01

Oh, I remember enjoying that one. Thanks!

Daniel Hines01:10:18

This is really profound! I'd asked a question about functional design patterns on this channel earlier, but I think this article get's at what I was trying to ask but in a deeper, more cross-cutting way...

tristefigure20:10:59

there are also articles that do the opposite, explain FP by comparing it to git (you never change anything, you only get new versions)

👍 4
chrisulloa20:10:36

Git’s also a blockchain right?

scriptor20:10:24

depends on how you define blockchain

hiredman20:10:46

a lot will depend on the audience, some people care a lot about things like emerging trends in industry and best practices, other people are more interested in notions of correctness and elegance. case studies (git, react, hadoop, etc) are useful ways to answer the question "how is everyone building systems?" (and show that a lot of people are building systems based on immutable data and functions)

💯 4
hiredman20:10:18

but it doesn't explain kind of why that pattern is something so common that people recognize it and strive for it

scriptor20:10:54

yeah, it’d be great if case studies recorded the reasoning people undertook when deciding on using functional approaches/patterns

👍 4
scriptor20:10:07

sometimes the best we have is hindsight, though

scriptor20:10:26

like, “react’s FP approach is useful for X and Y”

scriptor20:10:58

as opposed to “way back when planning out react we initially chose this approach for X and Y”

eggsyntax20:10:13

I think it'd be a great one, but I think my audience (of 1) is too green to know much (or anything?) about design patterns. This is someone fresh out of a Java/JS bootcamp, with essentially no previous programming experience.

hiredman20:10:57

I think the best motivation for a very new beginner might be build programs that can be composed easily. Even a new programmer these days has likely run in to the problem of trying to take two programs that work correctly on their own and combine them in to one new program that works correctly. Unfortunately my go to article on that is haskell based and starts immediately mentioning category theory

👍 4
dominicm21:10:53

Would you mind sharing?

dpsutton21:10:13

I'm not sure i've ever wanted some as much as (first (filter ...))

dpsutton21:10:40

most of my some's are of the form (when (pred thing) thing)

joelsanchez21:10:55

(first (filter... should be in clojure.core

joelsanchez21:10:10

why is ffirst in clojure.core but not (filter (first..., is my question

Alex Miller (Clojure team)21:10:41

nvm, I see what you’re asking

joelsanchez21:10:58

also why mapv but not mapm

Jan K21:10:23

there's a nice library for these things https://github.com/weavejester/medley

Jan K21:10:31

it has find-first, map-kv and other things "missing" from core

Jan K21:10:40

and it's tiny

joelsanchez21:10:28

yeah I know there are libraries, I also dont mind having a utils namespace or collections namespace, but I'm genuinely curious

joelsanchez21:10:34

guess there isn't an answer

Alex Miller (Clojure team)21:10:03

b/c core will never contain every function you might possibly need

Alex Miller (Clojure team)21:10:16

the cool thing about functions is anyone can make them

dpsutton21:10:14

oh yeah. i understand. Just remarking this one seems close and never quite what i need.

Alex Miller (Clojure team)21:10:47

in general, “linear search” functions like find-first are intentionally under-represented

Alex Miller (Clojure team)21:10:13

generally it’s better to arrange your data structure so you can look up the answer using some call that is effectively cost or log time instead

Alex Miller (Clojure team)21:10:43

I’ve almost never wanted a function like find-first

joelsanchez21:10:21

when you need to lookup by ID but the order is important, neither a map nor a vector quite work, and find-first works well here

Alex Miller (Clojure team)21:10:03

well, there’s ordered-map, but in general that usually drives me back to considering why order is important and trying to remove that constraint

Alex Miller (Clojure team)21:10:32

I do think having a few more map creation/transformation functions in core would be nice

joelsanchez21:10:33

think of a menu, it has menu items, they have an order. but if you want to edit a menu item, you lookup by ID

joelsanchez21:10:41

in this case you can't remove order

Alex Miller (Clojure team)21:10:14

well, they don’t need to be in the same data structure necessarily

Alex Miller (Clojure team)21:10:25

you could have a vector of ids and a map of id->info

hiredman21:10:36

or have an explicit :index key in the map

Alex Miller (Clojure team)21:10:46

well that doesn’t help you on lookup

hiredman21:10:51

and build an index using clojure.set/index

joelsanchez21:10:01

sure, I mean, of course. im just saying, that this is why people use find-first, or at least me

borkdude21:10:41

or use re-frame, datascript, or datomic 😉

Alex Miller (Clojure team)21:10:47

right, I’m just suggesting maybe you shouldn’t want to use it :)

andy.fingerhut21:10:40

ordering-map is a vector and a key->value map, under the hood. It is so thin a layer that explicitly using a separate vector and map is not a lot more

andy.fingerhut21:10:45

Correction - I meant to say ordered-map, referring to an existing 3rd party Clojure lib's data structure of that name, linked from the Clojure cheat sheet. I easily get that one and the similar-but-different ordering-map's names mixed up.

joelsanchez21:10:53

tbh I just checked and there are just two find-first in my main project and both of them are technical debt

dpsutton21:10:25

> in general, “linear search” functions like find-first are intentionally under-represented that's a nice insight there. thanks @alexmiller

borkdude21:10:57

some stats: the codebase I’m working on has 2037 namespaces, of which 6 use (first (filter ...))

😛 4
andy.fingerhut21:10:19

2037 namespaces. That is not a typo?

dpsutton22:10:54

> grep -R "(ns " ./ | wc -l 2768

dpsutton22:10:37

and across those, grep -R "(first (filter" ./ | wc -l results in 61 occurrences. not sure how naive these are but not worth improving i'm sure

dpsutton22:10:46

just noticed that output includes some target/raw and target/prod output though

andy.fingerhut22:10:57

@eggsyntax I created a list of the works mentioned for "why functional programming?" discussed earlier today, and published them here: https://github.com/jafingerhut/jafingerhut.github.com/blob/master/notes/why-functional-programming.md Feel free to link to it, suggest additions via issues or PRs, etc.

💯 4
👏 8
eggsyntax02:10:59

Nice! Thanks 🙂

borkdude22:10:40

Darn, only:

$ find . | grep ".clj[sc]\?$" | wc -l
     473
I had some target folders and stuff in there. Did a fresh checkout now 😉

borkdude22:10:23

but now my first filter count also goes down to 5. That’s about 1% of the namespaces still

csm23:10:08

I have nothing to add, except I was writing a few (first (filter ...)) today, and wondered about the lack of a find-first in core. I almost posted something to a slack about it 🤢

andy.fingerhut23:10:58

No reason to feel nauseated about wanting to ask the question. I suspect there are a fair number of people's home-grown "util" namespaces that have such a thing, too, but linear time searching is a performance liability waiting to happen if used when the linear sequence can grow much larger later.

csm23:10:16

in my case, all the sequences are bounded to 10 items, and changing the structure of the data would mean reworking a database schema in a way I wouldn’t even know how to approach. There are cases when a linear search is the simplest, best option.

👍 4
andy.fingerhut23:10:46

Sounds like you've got a perfect use case for linear search, then.

csm23:10:12

and also I’ve been using Kotlin on a side project, and found all the built-in stuff for dealing with collections really nice, for what it is (including first)

Daniel Hines01:10:18

This is really profound! I'd asked a question about functional design patterns on this channel earlier, but I think this article get's at what I was trying to ask but in a deeper, more cross-cutting way...