ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
hightower3 has quit [Ping timeout: 245 seconds]
<postmodern> also wait, what, %w[] cannot interpret \ characters properly?
<postmodern> wait, i think that's expected behavior, nm
<postmodern> boyaa, all green. soon crystal will have a port of my hexdump.rb library which can hexdump typed-data, similar to how `od` and `xxd` work
ur5us has joined #crystal-lang
postmodern has quit [Remote host closed the connection]
<FromGitter> <mwlang> thanks for the tip, @asterite
<FromGitter> <mwlang> That definitely got me going in the right direction
<FromGitter> <mwlang> Somewhat to my surprise, the app is spending a whopping 24% of it's time in `Array(Float64)@Enumerable(T)#compare_or_raise<Float64,Float64>:Int32`
<FromGitter> <mwlang> I guess I shouldn't be overly surprised, I'm building a number of arrays holding about 28k floats in the largest and about 300 floats in most of them and then looking for min and max in each to compute distributions.
<FromGitter> <mwlang> Is there a better way to repeatedly look for `min` and `max`in a set of numbers that remains fixed in length, but every iteration, push one number onto the array at one end and pop off at the other.
<FromGitter> <shootingfly> I am working for a C library, which needs some "C long double" parameter. I tried to use Float64, but failed. ⏎ ⏎ In Ruby, I can use "big_float", but how can I use in Crystal? I can't find any solution in https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/fun.html#types-in-c-bindings ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal
<FromGitter> ... -lang/crystal?at=6153d3ca7db1e3753e114c2e]
ur5us has quit [Ping timeout: 245 seconds]
notzmv has quit [Ping timeout: 246 seconds]
notzmv has joined #crystal-lang
<FromGitter> <shootingfly> I change my C library to do type conversion internally, which works.
<xyhuvud> mwlang: first answer to https://stackoverflow.com/questions/67654332/o1-javascript-circular-buffer-with-getmax-and-getmin could perhaps help you. worst case is O(n) but it would be a lot faster most of the time
hightower3 has joined #crystal-lang
<xyhuvud> shootingfly: what have you tried in crystal? That code example looks like ruby.
olbat[m] has quit [Quit: Bridge terminating on SIGTERM]
fifr[m] has quit [Quit: Bridge terminating on SIGTERM]
jhass[m] has quit [Quit: Bridge terminating on SIGTERM]
repo[m] has quit [Quit: Bridge terminating on SIGTERM]
jhass[m] has joined #crystal-lang
fifr[m] has joined #crystal-lang
olbat[m] has joined #crystal-lang
repo[m] has joined #crystal-lang
Vexatos has quit [Quit: Client Quit]
Vexatos has joined #crystal-lang
<FromGitter> <mwlang> Thanks, @xyhuvud -- that sparked the idea of just tracking min/max as I push and pop along with using Deque instead of Array. That led to finding min/max in the program taking 24% of the time to 6% and processing 1.3 million data points went from ~4 minutes (real-time) to ~35 seconds. I'll take that win any day!
<FromGitter> <mwlang> (this is just loosely benchmarking, relying on `time` at command-line and building with --release flag
<xyhuvud> if you are ok with a worst case (for sorted input) of 3 times the memory use, it might be possible to set up two queues, one for min and one for max, consisting of pairs of positions and values, and only adding (or replacing) top value(s) if more extreme than the previous value or values. And then shifting off the end when input is consumed.
<xyhuvud> That would be (amortized) linear runtime unless I'm totally off.
hightower3 has quit [Ping timeout: 245 seconds]
Peter0x44 has quit [Ping timeout: 264 seconds]
Peter0x44 has joined #crystal-lang
<FromGitter> <jrei:matrix.org> postmodern: I'd say, having a custom `PartialSlice` type?
postmodern has joined #crystal-lang
<postmodern> what's the operator to turn a UInt negative (sign extend), but still keep it a UInt? &-uint appears to just bit-negate the uint
<postmodern> hmm converting to .to_i64, negating, then forcibly converting back to .to_u64! returns an odd looking uint: printf("%b\n",(-(0xffffffff_u64.to_i64)).to_u64!) -> 1111111111111111111111111111111100000000000000000000000000000001
<postmodern> where as printf("%b\n",(-(0xffffffff_u64.to_i64))) prints -11111111111111111111111111111111
<postmodern> hmm no that's correct
<postmodern> ah ha, it's a difference between crystal's `sprintf("% .64b",-0xffffffff_i64)` and ruby's `sprintf("% .64b",-0xffffffff)`. In crystal I get back -11111111111111111111111111111111, but in ruby I get back -0000000000000000000000000000000011111111111111111111111111111111
<postmodern> the result from ruby seems more correct, as the ".64b" specifies that the String must be 64 characters long
<postmodern> wait, String::Formatter appears to implement it's own printf formatting logic? noooooo, please don't re-invent printf/sprintf
<FromGitter> <Dan-Do> I am new in C binding world. dump question. How to pass parameter declared as LibC::Char*?
<FromGitter> <Blacksmoke16> pretty sure you can just pass a string
<FromGitter> <Dan-Do> so easy :) ⏎ I thought we have to turn it into Pointer.
<FromGitter> <Blacksmoke16> some types define a `to_unsafe` method that Crystal automatically calls for you
<FromGitter> <Dan-Do> fantastic, thanks
<FromGitter> <Blacksmoke16> which in the case of `String` returns a pointer
Volk has joined #crystal-lang
Volk has quit [Changing host]
Volk has joined #crystal-lang
postmodern has quit [Read error: Connection reset by peer]
notzmv has quit [Ping timeout: 246 seconds]
notzmv has joined #crystal-lang
<FromGitter> <Blacksmoke16> if you're using https://crystal-lang.org/api/Atomic.html in the context of an `HTTP::Server`, does it internally handle 2 requests updating the value or does it also need a mutex?
<FromGitter> <Blacksmoke16> i'm assuming the former
<straight-shoota> atomic is atomic, no matter what you use it with
<FromGitter> <Blacksmoke16> 👍 perfect, that's what i figured based on the name :P
repo has quit [Ping timeout: 260 seconds]
repo has joined #crystal-lang
postmodern has joined #crystal-lang
<postmodern> i know all strings are UTF8 by default in Crystal, but is it possible to access any other character encodings?
dom96 has quit [Ping timeout: 268 seconds]
<postmodern> man, i am going to write a shard library that just defines a macro for converting NumLiterals to CharLiterals so I can enumerate through ASCII bytes easily
dom96 has joined #crystal-lang
<FromGitter> <asterite> with "access any other character encodings", what do you mean? What operation you want to do?
<postmodern> asterite, like the other weird encodings that iconv supports. UTF7, UTF16_LE, SHIFT-JIS, EBCDIC, etc
<FromGitter> <asterite> does `{% puts 123.stringify.chars %}` not work?
<postmodern> asterite, i wanted to for loop through a range of bytes and convert them to Chars, but there's no NumberLiteral#chr
Volk has quit [Remote host closed the connection]
<FromGitter> <asterite> We can add it :-)
<FromGitter> <asterite> What about: https://crystal-lang.org/api/1.1.1/String.html#new(bytes:Bytes,encoding:String,invalid:Symbol?=nil):String-class-method
<postmodern> also doesn't appear to be a NumberLiteral#to_s(16) which would be handy for using macros to generate repeatative specs
<FromGitter> <asterite> Yeah, the macro language is pretty limited. Most of the time if you need something and it's not there it means you shouldn't use a macro for that
<FromGitter> <asterite> (at least that's what I think, not everyone will agree)
<postmodern> oh thats promising. hmm but there's no way to pass an encoding to Int#chr
<FromGitter> <asterite> Right, because that goes from a unicode value to its character, there's no encoding involved there (encoding is how to convert those numbers to bytes)
<FromGitter> <asterite> I think!
ur5us has joined #crystal-lang
<postmodern> well other character encodings can have their own mappings of byte values to character
<FromGitter> <asterite> Interesting, in Ruby it seems `chr` goes from a byte value to the char
<FromGitter> <asterite> so the definition is different between Crystal and Ruby
<FromGitter> <asterite> But then I don't know how you would go from an int value to represent values more than 0..255
<FromGitter> <asterite> I guess you'd have to use that String method with a byte slice of just one byte
<postmodern> ah good idea, although i worry about the performance impact of creating single char Strings
<postmodern> (this is for my port of hexdump.rb, so it has to convert a lot of bytes to chars quickly)
postmodern has quit [Read error: Connection reset by peer]
postmodern has joined #crystal-lang
<postmodern> also submitting an issue for Int#chr(encoding)
postmodern has quit [Read error: Connection reset by peer]
postmodern has joined #crystal-lang
<postmodern> is there an easier way to coerce a value to an Array than [*value] ?
<FromGitter> <Blacksmoke16> if its just 1 value `[value]`?
<FromGitter> <Blacksmoke16> or is a `Tuple`?
<postmodern> 1 value [...] or an existing Array
<postmodern> i ended up just rewriting the method to use two overloads, one for the value the other for the array
<postmodern> what's the equivalent of StringScanner#getch ? Doesn't look like crystal's StringScanner has a next char method
<FromGitter> <Blacksmoke16> 👍 good call
<FromGitter> <Blacksmoke16> you prob want https://crystal-lang.org/api/master/Char/Reader.html
<postmodern> i also need to match given a Regex or a literal String
<FromGitter> <Blacksmoke16> https://crystal-lang.org/api/master/String.html#scan(pattern:String,&)-instance-method maybe then?
<postmodern> but i need to find every match of the regexp in the @highlight_regexp hash, once matched it advances to the next index after the match
<postmodern> if nothing matched at the index, it advances by 1
<postmodern> depending on which Regex matches, then the matched string will be highlighted with the associated ANSI styling
<postmodern> so i can't just do string.scan(Regex.union(regexes)) because i need to know which of the regexes matched
<postmodern> i guess i could rewrite that code to manually walk throgh the String
ur5us has quit [Remote host closed the connection]
ur5us has joined #crystal-lang
<postmodern> oh nice String#match accepts a pos argument
<FromGitter> <mwlang> @xyhuvud, that's not a bad idea, but I'm not sure that's going to gain me much over what I put together. https://gist.github.com/mwlang/190840269ec8e86ac52f619845aa3098
<postmodern> if you do "#{obj}..." will it call obj.to_s ?
<postmodern> or is there a method that can be called? (like Ruby's #to_str)
<FromGitter> <Blacksmoke16> The former
<postmodern> doesn't seem to be working
<postmodern> also just realized that str.match(regex,index) appears to walk the string, instead of trying to match the regex at that specific index
<FromGitter> <Blacksmoke16> Got an example of it not working
<postmodern> carc.in isn't loading.. grrr
<postmodern> Blacksmoke16: https://carc.in/#/r/bz6b
<FromGitter> <Blacksmoke16> what am i looking at?
<postmodern> oddly changing the regex to /^f/ doesn't match at any index
<FromGitter> <Blacksmoke16> sorry, i meant example of `to_s` not working
<postmodern> i'm expecting String#match(regex,pos) to try matching the regex at string[pos..] and not to incrementally try matching the regexp at pos, then pos+1, pos+2, etc
<postmodern> oh let me repo that too
<postmodern> Blacksmoke16: https://carc.in/#/r/bz6c
<FromGitter> <Blacksmoke16> you need to overload `to_s(io : IO) :nil` not `to_s : String`
<postmodern> oooooh
ur5us has quit [Ping timeout: 245 seconds]
postmodern has quit [Read error: Connection reset by peer]