## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• 10:15
oprypin review_requested #11162
• 10:15
oprypin review_requested #11094
• 10:14
oprypin review_requested #11242
• 10:13
oprypin review_requested #11242
• 10:13
oprypin review_requested #11268
• 10:12
oprypin review_requested #11450
• 06:58
HertzDevil labeled #11538
• 06:58
HertzDevil labeled #11538
• 06:58
HertzDevil labeled #11538
• 06:58
HertzDevil opened #11538
• 03:59
HertzDevil labeled #8413
• 03:59
HertzDevil labeled #8413
• 03:59
HertzDevil labeled #8413
• 03:57
HertzDevil closed #6510
• 03:55
HertzDevil labeled #6373
• 03:54
HertzDevil labeled #7305
• 03:54
HertzDevil labeled #7305
• 03:47
HertzDevil labeled #7455
• 03:47
HertzDevil labeled #7457
• 03:47
HertzDevil labeled #7457
George Dietrich
@Blacksmoke16
at which point you could prob just do like
element_wise &.round
but this is probably a good use case for macros
as the methods are all the same
mfiano
@mjfiano:matrix.org
[m]
with elt yield worked, but not with &.round...had to be { round }
George Dietrich
@Blacksmoke16
the former should work if you just do yield elt
iirc with .. yield is kinda buggy in some cases
so :shrug:
mfiano
@mjfiano:matrix.org
[m]
Pretty nifty, it works
This will do until I macroize things, thanks
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I'm not quite sure how I'd call element_wise for this one though, as it takes a second argument:
def copysign(other : self)
x = Math.copysign @x, other.x
y = Math.copysign @y, other.y
V2.new x, y
end
George Dietrich
@Blacksmoke16
prob just keep it as is?
mfiano
@mjfiano:matrix.org
[m]
There's a bunch like that. They all have the same pattern, just additional args
George Dietrich
@Blacksmoke16
id say just go with a macro imo
even if you have a helper private method its still just boilerplate
mfiano
@mjfiano:matrix.org
[m]
I'm feeling a little intimidated by Crystal macros still
Spent last night trying to figure out the basics from reading the docs...couldn't apply it to my own code
George Dietrich
@Blacksmoke16
{% for m in ["round", "abs"] %}
def {{m.id}}
v = dup
v.each_with_index { |elt, i| v[i] = elt.{{m.id}} }
v
end
{% end %}
assumes the method name is the same as the method called on the element, but could easily work around that if thats not what you want
mfiano
@mjfiano:matrix.org
[m]
In my case, I'd only want to generate the method body, not the whole method, supplying the name of the method called on the element
and therefor without a hard-coded list of strings too
George Dietrich
@Blacksmoke16
why would it matter?
mfiano
@mjfiano:matrix.org
[m]
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
George Dietrich
@Blacksmoke16
hmm
i have an idea
mfiano
@mjfiano:matrix.org
[m]
This is intended to be included on all 3 of my vector types. I may split it up into 2 mixins, for the methods that don't make sense for matrices
George Dietrich
@Blacksmoke16
private macro element_wise(name, &)
def {{name.id}}
out = dup
each_with_index { |elt, idx| out[idx] = {{yield}} }
out
end
end

element_wise fract do
elt - elt.floor
end
I think that should work
or even element_wise frac { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
Idea is to try to get rid of all references to @x, @y, etc so it's DRY for the related types
George Dietrich
@Blacksmoke16
also fwiw out is a keyword so prob wouldn't be a bad idea to pick something else
technically works, but going to mess up highlighting
mfiano
@mjfiano:matrix.org
[m]
I mean, that doesn't save any code by expanding into a toplevel definition, and imho isn't as easy to scan the file
George Dietrich
@Blacksmoke16
:thinking: how do you figure that doesn't save any code?
mfiano
@mjfiano:matrix.org
[m]
re: out: ok
9-11 are 3 lines. so is 24-26 in mine?
George Dietrich
@Blacksmoke16
def sign
element_wise elt.sign.to_f
end

def copysign(other : self)
element_wise Math.copysign elt, other[idx]
end

def fract
element_wise elt - elt.floor
end
Gets turned into:
element_wise sign { elt.sign.to_f }
element_wise copysign { Math.copysign elt, other[idx] }
element_wise fract { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
I could just as well do that with mine since they're short, albeit a bit differently
The benefit is I instantly know they are defs, and syntax highlighting gives my brain less to process when quickly scanning a source file
Correct me if I'm wrong :)
George Dietrich
@Blacksmoke16
fair enough
mfiano
@mjfiano:matrix.org
[m]
But I'll think about it. You have given me nothing but good suggestions this week and I am a bit tired now and about to quit, so we'll see. Thank you
George Dietrich
@Blacksmoke16
o/
on an unrelated note, https://play.crystal-lang.org/#/r/blio is this output expected?
looks to be a regression in 0.31.0