<FromGitter>
<watzon> Ok I need to figure out a better way to handle the `encode` and `decode` methods which accept an IO. Right now I'm making a `UInt8[4096]` in both and then reading from or writing to it, but a) it seems kind of wasteful and b) it won't handle cases in which there are more than 4096 bytes (which I can't see ever happening, but it still just feels like bad practice to have an arbitrary ceiling. Any ideas?
<FromGitter>
<watzon> Thanks @HertzDevil, needed another set of eyes on that one
<FromGitter>
<HertzDevil> you'll need to deallocate `bytes` yourself
<FromGitter>
<HertzDevil> with `GC.free`
<FromGitter>
<watzon> π
<FromGitter>
<HertzDevil> well maybe the gc will eventually collect it, but it shouldn't hurt
<FromGitter>
<watzon> Any ideas on the `decode` method that takes an IO? I could do `IO#gets_to_end` to get a String and then get the underlying slice, but that feels dirty. It doesn't seem like the base IO class gives any access to the underlying pointer though.
<FromGitter>
<watzon> Like this is the best I can think of, but it feels hacky https://carc.in/#/r/a5dw
<FromGitter>
<watzon> Doing some digging into the `IO` class it looks like it has an `encoder` and `decoder` which store the actual pointer data, and those aren't publicly accessible. So idk what else to do.
<FromGitter>
<naqvis> > Ali: not a bug. When you pass self in initialize the object isn't yet initialized. What if that method where you pass self accessed bar? Boom! β Thanks @asterite .
<FromGitter>
<HertzDevil> `#gets_to_end` is okay, i think a bigger question is whether it's useful to always consume the rest of the io
<FromGitter>
<HertzDevil> because `import` and `export` do not include the byte length in the data itself
<FromGitter>
<HertzDevil> or sign, for that matter
<FromGitter>
<HertzDevil> so to preserve bigints in a round trip you need at least `sizeof(LibC::SizeT)` extra bytes to represent that byte length
<FromGitter>
<HertzDevil> you might tuck the sign in the most significant bit, or write/read one more
<FromGitter>
<HertzDevil> now the problem is, whatever you choose it won't be a canonical byte representation of the bigint
<FromGitter>
<HertzDevil> unlike the primitive integer types
<FromGitter>
<HertzDevil> so defining them in `IO::ByteFormat` is actually not a good idea
sagax has joined #crystal-lang
Vexatos has quit [Quit: ZNC Quit]
<FromGitter>
<watzon> What would the alternative be though?
Vexatos has joined #crystal-lang
<FromGitter>
<watzon> I guess we could just have `BigInt.from_slice` and `BigInt#to_slice`. Idk how @asterite and the rest of the core team would feel about that though. The main thing I struggle with aesthetically there is that it would either need its own Endian enum, or it would need to use the IO::ByteFormat endian modules to differentiate the endianness. Either way it doesnβt flow well imo.
yxhuvud has quit [Read error: Connection reset by peer]
yxhuvud has joined #crystal-lang
hightower4 has quit [Remote host closed the connection]
<frojnd>
Can someone help me out with building a json.. I still got some time to fiddle with crystal. I want my json to look like in this example: https://app.quicktype.io?share=ivPdBjWa8kpSO6QM42xS this tool made 2 classes but I don't know how can I add new dates, verses to the json. Any more knowladgable can take a look at this example and give me an actual example for 2 new dates, verses?
<kevinsjoberg>
Is it possible to deconstruct a named tuple passed to a block?
<FromGitter>
<jrei:matrix.org> **is not working?
<FromGitter>
<asterite> not possible
<FromGitter>
<Uzay-G> Hi, if I have a class with two instance variables, and I want to basically add a method to it called `match` that either uses instance var 1 or var 2, is there a nice way to do that or should I just pass a boolean like `use_var1` or `use_var2`?
<FromGitter>
<jrei:matrix.org> hum, have they the same time?
<FromGitter>
<jrei:matrix.org> and, not possible to use two methods?
HumanG33k has quit [Ping timeout: 260 seconds]
HumanG33k has joined #crystal-lang
HumanG33k has quit [Max SendQ exceeded]
<FromGitter>
<Uzay-G> hm ig i could do that ye using some macro or smth would probably be ugly
HumanG33k has joined #crystal-lang
<raz>
if you think something's supposed to hurt, you're less likely to notice if you're doing it wrong. ;)
holst has joined #crystal-lang
<holst>
I feel stupid, but after initializing a new app I am presented with a skel file that outlines a Module, all good,
<holst>
but where do I put a function?
<FromGitter>
<Blacksmoke16> doesnt it have a comment that says like `# put code here`?
<FromGitter>
<Blacksmoke16> yea, `# TODO: Put your code here`
<FromGitter>
<Blacksmoke16> within the module
<holst>
Error: undefined method 'sqr' for Docker::Deploy:Module
<FromGitter>
<Blacksmoke16> can you share the code?
<holst>
the code for sqr is simply: def foo x \n x*x \n end
<FromGitter>
<Blacksmoke16> but the method is called `foo`?
<FromGitter>
<Blacksmoke16> also pretty sure method arguments need `()`, i.e. `def foo(x)`
<holst>
the problem is that it is not a method its not defined in a class
<FromGitter>
<Blacksmoke16> when calling it within the module itself its looking for a method in the scope of the module, but it doesnt find it. Using `self.sqr` makes it similar to a class method so its available in the module
<FromGitter>
<Blacksmoke16> depending on what you're doing you can probably just do stuff outside of it, i.e. for this little testing stuff
<holst>
self.sqr did not work, thankfully
<holst>
(I did not like the syntax)
<holst>
There should not be an Instance of a module ;-)
HumanG33k has quit [Remote host closed the connection]
<FromGitter>
<Blacksmoke16> right, it looks for it in the module scope, doesnt find it, so then checks the top level
<holst>
Sure thing, but I am not sure its a good idea if it makes the barrier for newbies harder, not being able to trivially define a function is not good
<holst>
is it wrong to have the "main" code inside the module body?
<FromGitter>
<Blacksmoke16> if all you're wanting to do with Crystal is mess around with it for learning purposes, you can just create a file called `app.cr` and just define everything in that file w/o needing a module or anything
<holst>
where should I put the code I usually put inside my __main__.py file
<holst>
I should write a blog later, "Crystal lang for Python programmers" ;-)
<raz>
holst: not wrong but i think rather uncommon. you probably want a class inside your module (or just make the module a class if you don't need an outer namespace)
<FromGitter>
<Blacksmoke16> lets take a step back, are you planning on releasing this code as a shard?
<FromGitter>
<Blacksmoke16> or just messing around for learning purposes?
<FromGitter>
<HertzDevil> > There should not be an Instance of a module ;β -) β β that's not what `self.sqr` means though
<holst>
I am not sure what a shard is, I am making an application, CLI application. I do not expect to expose any library like functionality (yet)
<holst>
I am using shards though
<FromGitter>
<Blacksmoke16> a shard is like a python package
<FromGitter>
<Blacksmoke16> like an external dependency and/or something you can install via pip
<holst>
even if its just a CLI application with no API per se?
<FromGitter>
<Blacksmoke16> yea, i assume you used the `app` option when generating the project?
<FromGitter>
<Blacksmoke16> in that case `app` projects arent something that is used as a dependency. However the app itself is still a shard, i.e. has a version, dependencies of its own etc
<FromGitter>
<Blacksmoke16> given you're making a CLI
<raz>
yup, cli building, option parsing etc. is a science in itself
<raz>
if you're only starting out then i'd suggest to defer that part to a bit later until you understand the language better (the type coercion etc. in most opt parsers tends to be a bit more on the advanced side)
<FromGitter>
<Blacksmoke16> going back to your question, the code would go in the file that matches your app name
<FromGitter>
<Blacksmoke16> i.e. `src/my_app.cr`
<FromGitter>
<Blacksmoke16> *how* exactly to structure things is kinda up to you. However for CLI related things I think the general agreement is its better to have your main file for the actual domain logic and a dedicated one for the CLI part of it
<FromGitter>
<Blacksmoke16> some people also like to have another directory under `src` that holds all the logic, with the main file just requiring all of it
<holst>
"extend self" seems to be what I was missing
<FromGitter>
<Blacksmoke16> thats essentially syntactic sugar to just doing `def self.name`
<holst>
Docker::Deploy.run works if I have it, so thats good. I just needed to wrap all the code in a function (or Module method)
<holst>
thanks a lot guys for the help
<FromGitter>
<Blacksmoke16> "all the code", is this cli like a 1 command thing or something?
<holst>
yes ;-)
<holst>
its basically one command missing from the Docker CLI
<FromGitter>
<Blacksmoke16> i feel like it would have been easier to write a bash script :p
<holst>
i already have a python code that works, I am redoing it in crystal just to learn
<FromGitter>
<Blacksmoke16> π fair enough
<holst>
thats how I started, docker cli, jq and shell script
<FromGitter>
<Blacksmoke16> should checkout `oq` π
<holst>
the docker config uses yaml but the API uses json
<holst>
( I am only touching the API so far )
<holst>
but I just saw oq. another tool in this space is gron
<holst>
"Greppable JSON"
<FromGitter>
<Uzay-G> @holst I just wrote a very simple little tool in crystal that acts as cli, maybe it can help you (I'm very much of a crystal noob though π ). This is the main file: https://github.com/Uzay-G/garret/blob/main/src/garret.cr
<FromGitter>
<Morantron> hi! any tips on how to distribute a CLI made with crystal? ideally a static binary for linux and OSX thorugh github releases
<FromGitter>
<jrei:matrix.org> in Crystal/Ruby this is blocks
<straight-shoota>
the most important property of `with open('foo') as f:` is that is ensures to release the open file descriptor after the block is left
<straight-shoota>
in Crystal, this works pretty much the same
<straight-shoota>
`File.open("foo") do |f| f.read_lines end`
<FromGitter>
<Blacksmoke16> or event just `File.read_lines("foo")` π
<FromGitter>
<Blacksmoke16> even*
<FromGitter>
<jrei:matrix.org> with is like begin/rescue
<FromGitter>
<jrei:matrix.org> begin/ensure
<holst>
straight-shoota: I agree that its greatest value is not in the reduced typing effort ;-)
deavmi has quit [Ping timeout: 264 seconds]
deavmi has joined #crystal-lang
<raz>
python kinda follows a "worse is better" language design. i prefer crystals' "better is better".
<FromGitter>
<drum445> what am I supposed to do with that please, doesn't tell me what line in my code is throwing it?
deavmi has joined #crystal-lang
<FromGitter>
<erdnaxeli:cervoi.se> what is your code?
<FromGitter>
<drum445> there's a lot of code
<FromGitter>
<erdnaxeli:cervoi.se> what is the line failing
<FromGitter>
<erdnaxeli:cervoi.se> you should use `--error-trace` btw
<FromGitter>
<drum445> I did
<FromGitter>
<Blacksmoke16> looks like your array can contain a bool and you're trying to compare that with a int32
<FromGitter>
<Blacksmoke16> er `current` is an int32 and `end_value` could be an array of bools
<FromGitter>
<drum445> hmm, interesting. Thanks I'll check for it. No way to get the line no out that is throwing it?
deavmi has quit [Ping timeout: 272 seconds]
<FromGitter>
<Blacksmoke16> id just look for a place you're using a range?
<FromGitter>
<jrei:matrix.org> raz: is there a language, worse is worse? π
<oprypin>
Rust
<raz>
rust ain't bad, just super low level
<FromGitter>
<jrei:matrix.org> I like languages experimenting and bringing new features, not just syntax
<raz>
i could see a world in which rust is used for kernels, drivers and such. and crystal for the rest
<FromGitter>
<jrei:matrix.org> Even if the experiment fails :/
deavmi has joined #crystal-lang
<FromGitter>
<jrei:matrix.org> Also Rust is complex, which makes it a lot harder to implement a compiler for a custom micro-controller
<oprypin>
i thought carefully what "worse is worse" means. really it's similar to "better is better" but you can do a special spin on it
<FromGitter>
<jrei:matrix.org> What apects? Compile times?
deavmi has quit [Ping timeout: 272 seconds]
deavmi has joined #crystal-lang
deavmi has quit [Ping timeout: 272 seconds]
deavmi has joined #crystal-lang
<FromGitter>
<watzon> Ok can someone help me figure out where this 194 byte is coming from? https://carc.in/#/r/a5ky
<FromGitter>
<watzon> Feels like some witchcraft is happening
<oprypin>
watzon, this shouldnt be a String.build btw. you're working with bytes, not text
<oprypin>
and the byte, that's probably a utf-8 continuation mark?
<FromGitter>
<watzon> I've done the same with IO::Memory as well, I get the same result
<FromGitter>
<watzon> The byte in question looks like this `Γ`
<FromGitter>
<watzon> I wonder if the `.chr` was making things weird though. Ended up just doing it with an Array and a while loop and I got the correct result.
<oprypin>
watzon, im telling you again, it's the fact that you're involving a unicode string
<oprypin>
"The byte in question looks like this `Γ`" - bytes don't look like anything FYI, purely a number
<FromGitter>
<lodenos> Hey The Community <3 β Sunday Iβve speak about the macro for get the code and send at a worker β I would do something like this project in Python Dask https://dask.org β Do u know if something already in Crystal yet ? β If not I would contribut for doing the same thing in Crystal, Because I need this kind of technology in Crystal βcause I use Crystal in Production for my Client
<oprypin>
is the reason for "extra" bytes in your example
<straight-shoota>
140.chr is unicode code point U+008C with UTF-8 encoding c2 8c that is `[194, 140]`
<holst>
I have a fairly large structure (or object) that I would like to compare, with a few exceptions to a pair of fields that are always unique and should be ignored
<holst>
I cannot do s1 == s2 but rather I would need to do filter(s1, condition on method name) == filter(s2, ...)
<oprypin>
holst, can u `def ==` on it?
<holst>
its part of a library so all bets are off if I redefine a global method, but maybe locally?
<frojnd>
oprypin: I meanat for carc link I pasted... I looked at that doc before but I don't seem to get it... I still don't know how can I create desired json I pasted in url above
<oprypin>
frojnd, [Fruit.new(.......)].to_json
<frojnd>
oprypin: ok that's one fruit.. let say I have many.. how can I add afte rthe firs Fruit.new ?
<frojnd>
Let's presume I'm in the loop and I would like to add one by one...
<oprypin>
arr << Fruit.new
<oprypin>
before that you need: arr = Array(Fruit).new
<frojnd>
And at the end just to_json ?
<oprypin>
ye
<frojnd>
It expects only one argumen: `Fruit.new(pull : ::JSON::PullParser)`
<FromGitter>
<Daniel-Worrall> I really wish the reference had better search
<FromGitter>
<Daniel-Worrall> It's like it only indexes headers
<oprypin>
Daniel-Worrall, thats not true, it indexes all text
<oprypin>
Daniel-Worrall, e.g. try searching the word "formal"
<FromGitter>
<Daniel-Worrall> odd, I've always had bad results from certain searches
<FromGitter>
<Daniel-Worrall> I'll try reporting here next time it happens
<frojnd>
oprypin: yeah thank you reading it up to bottom
<oprypin>
Daniel-Worrall, was it before or after the redesign?
<FromGitter>
<Daniel-Worrall> both
<FromGitter>
<Daniel-Worrall> I know a generic report like that is a bit unhelpful, so I will definitely come back when/if I find it happening again
<FromGitter>
<Daniel-Worrall> Would it be stdlib appropriate to have some sort of Range Set type?
<FromGitter>
<Daniel-Worrall> aka interval graph
<oprypin>
Daniel-Worrall, no, never seen it in any lang
<FromGitter>
<Daniel-Worrall> We could do with some good math libs in the ecosystem
kevinsjoberg has quit [Ping timeout: 244 seconds]
kevinsjoberg has joined #crystal-lang
<FromGitter>
<Daniel-Worrall> Can we get a `Set(T)#-(other : T)` so I don't have to make a set/enumerable of the single element?
<FromGitter>
<Daniel-Worrall> We have `#delete` but it's not as intuitive
<hightower2>
Hey let's say I have a function which accepts (*args). What's any quick way to turn the args into something uniquely identifiable, like something suitable for use in a cache key? I don't care about the form of it, can be anything.
<FromGitter>
<Blacksmoke16> args.hash?
<hightower2>
just thought of it as I wrote the question. Let me check how that'd work
<hightower2>
hm, seems like it would.. awesome, thanks
<FromGitter>
<Daniel-Worrall> oprypin, example `while` gives 0 results
<Andriamanitra>
i've noticed that too, it seems as if queries with too many dont show any at all, even if they would be in the title of a page
<FromGitter>
<Daniel-Worrall> The macro needs to be a self contained valid crystal snippet
<FromGitter>
<Daniel-Worrall> You need to wrap the all_methods in {% begin %} {% end %}
<holst>
jackpot that was it thx Daniel
<holst>
btw, is there a way to use space to delimit the elements in a tuple instead of ","? i think i saw something like qw/foo bar/ to make {"Foo", "Bar"}?
<FromGitter>
<Blacksmoke16> why not just use `==` overload?
<oprypin>
[20:49:19] <holst> I have a fairly large structure (or object) that I would like to compare, with a few exceptions to a pair of fields that are always unique and should be ignored
<oprypin>
[20:49:55] <holst> I cannot do s1 == s2 but rather I would need to do filter(s1, condition on method name) == filter(s2, ...)