Fork me on GitHub
#babashka
<
2023-02-13
>
Matthew Twomey04:02:58

I want this same result:

(->> {:one "one" :two "two" :three "three"} vec flatten) 
;; => (:one "one" :two "two" :three "three")
I feel like I might be missing a single function that does this?

Matthew Twomey04:02:43

(mapcat seq {:one "one" :two "two" :three "three"}) also does it - but still seems like it should be more simple.

dpsutton04:02:53

iterleave its keys and values. but be aware that you are going from an unordered structure to an ordered one. The way you "see" the map or think it should order might not be what you end up with.

Matthew Twomey04:02:43

well, what I do care about is that the pairs stay next to each other. I don’t care about the order of the pairs themselves.

dpsutton04:02:09

good. then (defn as-seq [m] (interleave (keys m) (vals m))) should do it for you

dpsutton04:02:57

although nothing really wrong with your mapcat seq solution

Matthew Twomey04:02:29

Oh yeah - ok, cool. I’m finding lots of ways to do this I see. I haven’t run into interleave yet - thanks for pointing it out!

👍 2
dpsutton04:02:24

(into [] cat {:one "one" :two "two" :three "three"})

👀 2
3
Jakub Šťastný15:02:29

I'm getting [nRepl] Connection closed unexpectedly (connection broken by remote peer) when I do cider-eval-buffer. At that point bb --nrepl-server quits with exit status 1 and no message. I'm using babashka v1.1.172 from Homebrew on the latest macOS. What's up with that?

1
borkdude15:02:21

can't repro here. perhaps you have a (System/exit 1) in your code?

Jakub Šťastný15:02:30

Haha I'm a moron. You're right. It's doing arguments check 🙈

borkdude15:02:10

You can also do:

(throw (ex-info "Arguments not right" {:babashka/exit 1}))

borkdude15:02:12

This won't exit the REPL

borkdude15:02:30

but will exit with exit code 1 if the exception isn't caught, without printing an error report

Jakub Šťastný15:02:08

Nice, I like that.

Jakub Šťastný15:02:03

Are there no structs in Babashka? Can't see it mentined in BB book, but getting Could not resolve symbol: create-struct.

borkdude15:02:06

No. What is it with people and structs lately :) Clojure has defrecord which replaces structs in 99.9% of the cases

borkdude15:02:38

Please let me know why you are trying to use structs. There's been a flux of people lately and it's mystery where this is coming from

Jakub Šťastný15:02:20

No good reason: I'm rather new the Clojure and I'm still confused between vectors/iseqs, structs/records and the likes.

Jakub Šťastný15:02:30

So if you say use record, that's fair enough for me.

borkdude15:02:44

use a normal map, not even a record, unless you have a reason not to

☝️ 1
borkdude15:02:48

Where did you read about structs? I'm trying to detect the "beginner's source" that tries to teach this to people. It's not a co-incidence anymore that people are asking about this in various places

😂 2
Jakub Šťastný15:02:54

I think I want something more "formal" than a record. There's a lot of data and all the vectors and maps are getting way too complex, so I wanted like a custom type.

Jakub Šťastný15:02:45

Well coming from OOP languages you go like "OK, if there ain't no objects, then what would a custom type be?" and knowing structs from other languages, you go like oh yeah, that must be it.

borkdude15:02:03

ah, so it wasn't a book or blog, or something like this?

mike_ananev15:02:02

Hi. Where I can find bb builds with loom virtual threads? Any plans to build bb with loom in next releases?

Rupert (All Street)15:02:57

Out of interest what's your usecase with loom threads? You can have 10,000s of Java threads already and with core.async you can have millions of go blocks.

mike_ananev16:02:38

I'm working on library of SWIM implementation protocol in JVM and every UDP packet is processed in Virtual Thread. I want to make this library bb compatible and I need to test it with Loom threads. core.async has limits of using I/O in go blocks and limited by OS threads. Loom Virtual Threads is free of that limits. I can make 100K I/O parallel operations without any problems.

Rupert (All Street)16:02:10

Thanks, sounds good. I can see that loom threading model would help simplify the design of protocols like that.

lispyclouds16:02:58

we have this branch https://github.com/babashka/babashka/tree/jdk19-loom and tried out experimenting with it with some good results. but we are waiting on some more issues to resolve before moving to jdk 19+

lispyclouds16:02:19

i can try triggering the ci on that branch again to produce some binaries, but note that the branch isnt upto date

lispyclouds16:02:22

@U04V15CAJ should i get the loom branch upto date? have some newer CI builds?

borkdude16:02:06

You should be able to try the jdk19-loom branch and compile locally. For some reason there was a failing Windows test which I haven't dug into. There is GraalVM 19 22.3.1 (previous I tried was 22.3.0)

borkdude16:02:14

@U7ERLH6JX Feel free to make the branch up to date, preferably via a PR to the jdk-19 branch

1
borkdude16:02:33

If people want to speed up progress around getting this released into bb investigating the Windows issue should help too

mike_ananev20:02:29

I made bb build from https://github.com/babashka/babashka/tree/jdk19-loom branch using GraalVM 19 22.3.1. Now it works!

🎉 2
mike_ananev20:02:47

I made a build with spec flag export BABASHKA_FEATURE_SPEC_ALPHA=true. Why s/and is not supported? Is there workaround?

borkdude20:02:53

@U097654L8 You don't need this anymore. spec is included with bb in the latest version. before those you have to include this dependency: https://github.com/babashka/spec.alpha

borkdude20:02:32

This is from 1.0.167 (2022-11-30) and newer

mike_ananev20:02:50

I made a build with loom v1.0.165. Is there any bb branches with loom for newer versions of bb?

mike_ananev20:02:29

I need Loom and spec together

lispyclouds20:02:57

Like i mentioned before, that branch isn’t upto date. You can rebase master on it maybe

mike_ananev20:02:20

I'll try. Thanks.

borkdude20:02:42

PR welcome to that branch, as mentioned. Will help move things forward.

lispyclouds20:02:37

@U097654L8 if you’re already on it, might as well raise a PR if it works 😄 i can do it in a bit as well

mike_ananev20:02:30

Well I made rebase loom branch on 1.0.174-SNAPSHOT and Loom works. But I still struggle with spec. s/and still could not be resolved

borkdude21:02:47

you should not compile it with that spec flag you mentioned earlier

borkdude21:02:23

if you did, try: (require [clojure.spec.alpha :as s] :reload)

mike_ananev21:02:38

Ah, I will recompile without this flag. Thanks!

mike_ananev21:02:55

It works! Thank you!

👍 1
Jakub Šťastný20:02:40

I'm not sure whether this has to do with babashka or something else, but I'm running babashka, so I'll try here. I have a custom text representation of my type: (defmethod print-method QID [qid out] (.write out (str "m[QID: " (.toString qid) "]"))) When I put (println (QID. 4 0 0 1)) into my script and run it in the console, it prints out the correct custom representation. All good there. However in bb --nrepl-server this is not respected. That is, I evaluate the method definition, then I evaluate the println sexp, it returns nil as it should, but if I go to the cider buffer, where it shows standard output, it's not formatted by the custom formatter but rather by the default one. Anyway in the context of REPL, more important than the standard output is what I see as the inspect value when I evaluate a sexp in the code buffer. If I understand how things work, this requires to define print-dup in the same way as I defined the print-method. I did so, evaluated in REPL, but it's still out returning the expected formatting. What am I doing wrong?

borkdude20:02:22

Might be a bug

borkdude20:02:40

issue welcome

borkdude20:02:59

please use threads for remarks about the same topic / conversation :)

Jakub Šťastný20:02:00

Let me see what it does in normal Clojure.

Jakub Šťastný20:02:29

Oh wow it probably is a bug, it works in Clojure.

Jakub Šťastný20:02:47

Let me verify to make sure I'm not doing something stupid, but if I'm not, I'll report it.

borkdude20:02:52

I probably know what the bug is, issue welcome

borkdude21:02:07

Can you also paste the code there? It's easier to copy/paste as a test case

Jakub Šťastný21:02:46

I'm using Clojure in the REPL for now and it works there as expected. Really I just need it in the REPL, so it doesn't bother me that much, but of course it's more pleasant running Babashka REPL so I don't have to define all those constants that Clojure doesn't have defined, fs, str etc.

borkdude21:02:28

Thanks. I don't get why there are two snippets instead of just one?

borkdude21:02:59

A little bit more explanation in the issue might be good. There's just screenshots without any guiding text how to repro. I have this code, merged the snippet into one snippet:

(defprotocol Version (stringify [this] "Get string representation"))
(deftype QID [a b c d] Version (stringify [_] "test"))
(defmethod print-method QID [object out]
  (.write out (stringify object)))

(println "HERE")
(println (.stringify (QID. 4 0 0 1)))
(println (.toString (QID. 4 0 0 1)))
(println (QID. 4 0 0 1))
And here is the difference between bb and clj:
borkdude@m1 ~/dev/greglook-clj-multiformats (babashka) $ bb /tmp/dude.clj
HERE
nil
nil
test
borkdude@m1 ~/dev/greglook-clj-multiformats (babashka) $ clj -M /tmp/dude.clj
HERE
test
user.QID@7c8326a4
test
Is there anything more I need to know? I also see an NPE in the issue, how to repro that?

Jakub Šťastný21:02:02

The other one is probably not 100% related (it doesn't have print-method defined, but also behaves weird, so I thought it might be possibly somewhat related, I don't know, take a look and see.

Jakub Šťastný21:02:55

I think you don't need to know anything more.

borkdude21:02:58

in your screenshot I see a NullPointerException

borkdude21:02:50

so, this works:

(println (stringify (QID. 4 0 0 1)))

borkdude21:02:03

instead of calling (.stringify ...)

borkdude21:02:31

and instead of .toString, str works:

(println (str (QID. 4 0 0 1)))

borkdude21:02:37

Those are probably more idiomatic Clojure ways of doing the same and what bb is optimized for

borkdude21:02:59

nonetheless the difference is interesting and should be fixed

Jakub Šťastný21:02:02

OK, it does. That makes me even more perplexed. Isn't a method supposed to be called with a dot?

Jakub Šťastný21:02:15

Hahaha I'm sure about that 😄

borkdude21:02:17

a protocol method can just be called via the protocol var

borkdude21:02:59

user=> (defprotocol Foo (foo [_]))
Foo
user=> #'foo
#'user/foo

borkdude21:02:32

So foo is a proper var. That there is a generated Java method under the hood is not something you should be concerned with I'd say

borkdude21:02:37

In ClojureScript (.stringify ...) won't even work

Jakub Šťastný21:02:01

Right. The way to access the generated Java method is via the dot notation? And I shouldn't be doing that then you say. Just leaving the dot out then?

Jakub Šťastný21:02:34

Really appreciate your help and your work on Babashka! Such an awesome project!

thanks 1
borkdude21:02:35

yes, just call the protocol method like you named it

👍 1